code refactor

This commit is contained in:
bobokun 2022-11-19 11:13:41 -05:00
parent 2d512dd439
commit ac3267688b
No known key found for this signature in database
GPG key ID: B73932169607D927
2 changed files with 158 additions and 131 deletions

View file

@ -127,7 +127,8 @@ class Config:
self.data["webhooks"] = temp self.data["webhooks"] = temp
if "bhd" in self.data: if "bhd" in self.data:
self.data["bhd"] = self.data.pop("bhd") self.data["bhd"] = self.data.pop("bhd")
self.dry_run = self.commands["dry_run"]
self.loglevel = "DRYRUN" if self.dry_run else "INFO"
self.session = requests.Session() self.session = requests.Session()
self.settings = { self.settings = {
@ -577,8 +578,6 @@ class Config:
# Empty old files from recycle bin or orphaned # Empty old files from recycle bin or orphaned
def cleanup_dirs(self, location): def cleanup_dirs(self, location):
dry_run = self.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
num_del = 0 num_del = 0
files = [] files = []
size_bytes = 0 size_bytes = 0
@ -640,23 +639,23 @@ class Config:
if empty_after_x_days <= days: if empty_after_x_days <= days:
num_del += 1 num_del += 1
body += logger.print_line( body += logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} " f"{'Did not delete' if self.dry_run else 'Deleted'} "
f"{filename} from {folder} (Last modified {round(days)} days ago).", f"{filename} from {folder} (Last modified {round(days)} days ago).",
loglevel, self.loglevel,
) )
files += [str(filename)] files += [str(filename)]
size_bytes += os.path.getsize(file) size_bytes += os.path.getsize(file)
if not dry_run: if not self.dry_run:
os.remove(file) os.remove(file)
prevfolder = re.search(f".*{os.path.basename(location_path.rstrip(os.sep))}", file).group(0) prevfolder = re.search(f".*{os.path.basename(location_path.rstrip(os.sep))}", file).group(0)
if num_del > 0: if num_del > 0:
if not dry_run: if not self.dry_run:
for path in location_path_list: for path in location_path_list:
util.remove_empty_directories(path, "**/*") util.remove_empty_directories(path, "**/*")
body += logger.print_line( body += logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} {num_del} files " f"{'Did not delete' if self.dry_run else 'Deleted'} {num_del} files "
f"({util.human_readable_size(size_bytes)}) from the {location}.", f"({util.human_readable_size(size_bytes)}) from the {location}.",
loglevel, self.loglevel,
) )
attr = { attr = {
"function": function, "function": function,

View file

@ -94,8 +94,6 @@ class Qbt:
# (Returns True if at least one of the torrent with the State is categorized as Complete.) # (Returns True if at least one of the torrent with the State is categorized as Complete.)
# first_hash = Returns the hash number of the original torrent (Assuming the torrent list is sorted by date added (Asc)) # first_hash = Returns the hash number of the original torrent (Assuming the torrent list is sorted by date added (Asc))
def get_torrent_info(torrent_list): def get_torrent_info(torrent_list):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
torrentdict = {} torrentdict = {}
t_obj_unreg = [] t_obj_unreg = []
t_obj_valid = [] t_obj_valid = []
@ -103,7 +101,9 @@ class Qbt:
settings = self.config.settings settings = self.config.settings
logger.separator("Checking Settings", space=False, border=False) logger.separator("Checking Settings", space=False, border=False)
if settings["force_auto_tmm"]: if settings["force_auto_tmm"]:
logger.print_line("force_auto_tmm set to True. Will force Auto Torrent Management for all torrents.", loglevel) logger.print_line(
"force_auto_tmm set to True. Will force Auto Torrent Management for all torrents.", self.config.loglevel
)
logger.separator("Gathering Torrent Information", space=True, border=True) logger.separator("Gathering Torrent Information", space=True, border=True)
for torrent in alive_it(torrent_list): for torrent in alive_it(torrent_list):
is_complete = False is_complete = False
@ -111,7 +111,12 @@ class Qbt:
status = None status = None
working_tracker = None working_tracker = None
issue = {"potential": False} issue = {"potential": False}
if torrent.auto_tmm is False and settings["force_auto_tmm"] and torrent.category != "" and not dry_run: if (
torrent.auto_tmm is False
and settings["force_auto_tmm"]
and torrent.category != ""
and not self.config.dry_run
):
torrent.set_auto_management(True) torrent.set_auto_management(True)
try: try:
torrent_name = torrent.name torrent_name = torrent.name
@ -200,15 +205,13 @@ class Qbt:
return self.client.torrents.info(**params) return self.client.torrents.info(**params)
def category(self): def category(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
num_cat = 0 num_cat = 0
def update_cat(new_cat, cat_change): def update_cat(new_cat, cat_change):
nonlocal dry_run, torrent, num_cat nonlocal torrent, num_cat
tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")]) tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")])
old_cat = torrent.category old_cat = torrent.category
if not dry_run: if not self.config.dry_run:
try: try:
torrent.set_category(category=new_cat) torrent.set_category(category=new_cat)
if torrent.auto_tmm is False and self.config.settings["force_auto_tmm"]: if torrent.auto_tmm is False and self.config.settings["force_auto_tmm"]:
@ -216,20 +219,20 @@ class Qbt:
except Conflict409Error: except Conflict409Error:
e = logger.print_line( e = logger.print_line(
f'Existing category "{new_cat}" not found for save path {torrent.save_path}, category will be created.', f'Existing category "{new_cat}" not found for save path {torrent.save_path}, category will be created.',
loglevel, self.config.loglevel,
) )
self.config.notify(e, "Update Category", False) self.config.notify(e, "Update Category", False)
self.client.torrent_categories.create_category(name=new_cat, save_path=torrent.save_path) self.client.torrent_categories.create_category(name=new_cat, save_path=torrent.save_path)
torrent.set_category(category=new_cat) torrent.set_category(category=new_cat)
body = [] body = []
body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), loglevel) body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), self.config.loglevel)
if cat_change: if cat_change:
body += logger.print_line(logger.insert_space(f"Old Category: {old_cat}", 3), loglevel) body += logger.print_line(logger.insert_space(f"Old Category: {old_cat}", 3), self.config.loglevel)
title = "Moving Categories" title = "Moving Categories"
else: else:
title = "Updating Categories" title = "Updating Categories"
body += logger.print_line(logger.insert_space(f"New Category: {new_cat}", 3), loglevel) body += logger.print_line(logger.insert_space(f"New Category: {new_cat}", 3), self.config.loglevel)
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel) body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel)
attr = { attr = {
"function": "cat_update", "function": "cat_update",
"title": title, "title": title,
@ -258,14 +261,14 @@ class Qbt:
update_cat(new_cat, True) update_cat(new_cat, True)
if num_cat >= 1: if num_cat >= 1:
logger.print_line(f"{'Did not update' if dry_run else 'Updated'} {num_cat} new categories.", loglevel) logger.print_line(
f"{'Did not update' if self.config.dry_run else 'Updated'} {num_cat} new categories.", self.config.loglevel
)
else: else:
logger.print_line("No new torrents to categorize.", loglevel) logger.print_line("No new torrents to categorize.", self.config.loglevel)
return num_cat return num_cat
def tags(self): def tags(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
num_tags = 0 num_tags = 0
ignore_tags = self.config.settings["ignoreTags_OnUpdate"] ignore_tags = self.config.settings["ignoreTags_OnUpdate"]
if self.config.commands["tag_update"]: if self.config.commands["tag_update"]:
@ -277,14 +280,14 @@ class Qbt:
if tracker["tag"]: if tracker["tag"]:
num_tags += len(tracker["tag"]) num_tags += len(tracker["tag"])
body = [] body = []
body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), loglevel) body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), self.config.loglevel)
body += logger.print_line( body += logger.print_line(
logger.insert_space( logger.insert_space(
f'New Tag{"s" if len(tracker["tag"]) > 1 else ""}: {", ".join(tracker["tag"])}', 8 f'New Tag{"s" if len(tracker["tag"]) > 1 else ""}: {", ".join(tracker["tag"])}', 8
), ),
loglevel, self.config.loglevel,
) )
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel) body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel)
body.extend( body.extend(
self.set_tags_and_limits( self.set_tags_and_limits(
torrent, torrent,
@ -310,40 +313,43 @@ class Qbt:
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
if num_tags >= 1: if num_tags >= 1:
logger.print_line(f"{'Did not update' if dry_run else 'Updated'} {num_tags} new tags.", loglevel) logger.print_line(
f"{'Did not update' if self.config.dry_run else 'Updated'} {num_tags} new tags.", self.config.loglevel
)
else: else:
logger.print_line("No new torrents to tag.", loglevel) logger.print_line("No new torrents to tag.", self.config.loglevel)
return num_tags return num_tags
def set_tags_and_limits(self, torrent, max_ratio, max_seeding_time, limit_upload_speed=None, tags=None, restore=False): def set_tags_and_limits(self, torrent, max_ratio, max_seeding_time, limit_upload_speed=None, tags=None, restore=False):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
body = [] body = []
# Print Logs
if limit_upload_speed: if limit_upload_speed:
if limit_upload_speed == -1: if limit_upload_speed == -1:
body += logger.print_line(logger.insert_space("Limit UL Speed: Infinity", 1), loglevel) body += logger.print_line(logger.insert_space("Limit UL Speed: Infinity", 1), self.config.loglevel)
else: else:
body += logger.print_line(logger.insert_space(f"Limit UL Speed: {limit_upload_speed} kB/s", 1), loglevel) body += logger.print_line(
logger.insert_space(f"Limit UL Speed: {limit_upload_speed} kB/s", 1), self.config.loglevel
)
if max_ratio or max_seeding_time: if max_ratio or max_seeding_time:
if (max_ratio == -2 or max_seeding_time == -2) and not restore: if (max_ratio == -2 or max_seeding_time == -2) and not restore:
body += logger.print_line(logger.insert_space("Share Limit: Use Global Share Limit", 4), loglevel) body += logger.print_line(logger.insert_space("Share Limit: Use Global Share Limit", 4), self.config.loglevel)
elif (max_ratio == -1 or max_seeding_time == -1) and not restore: elif (max_ratio == -1 or max_seeding_time == -1) and not restore:
body += logger.print_line(logger.insert_space("Share Limit: Set No Share Limit", 4), loglevel) body += logger.print_line(logger.insert_space("Share Limit: Set No Share Limit", 4), self.config.loglevel)
else: else:
if max_ratio != torrent.max_ratio and (not max_seeding_time or max_seeding_time < 0): if max_ratio != torrent.max_ratio and (not max_seeding_time or max_seeding_time < 0):
body += logger.print_line(logger.insert_space(f"Share Limit: Max Ratio = {max_ratio}", 4), loglevel) body += logger.print_line(
logger.insert_space(f"Share Limit: Max Ratio = {max_ratio}", 4), self.config.loglevel
)
elif max_seeding_time != torrent.max_seeding_time and (not max_ratio or max_ratio < 0): elif max_seeding_time != torrent.max_seeding_time and (not max_ratio or max_ratio < 0):
body += logger.print_line( body += logger.print_line(
logger.insert_space(f"Share Limit: Max Seed Time = {max_seeding_time} min", 4), loglevel logger.insert_space(f"Share Limit: Max Seed Time = {max_seeding_time} min", 4), self.config.loglevel
) )
elif max_ratio != torrent.max_ratio and max_seeding_time != torrent.max_seeding_time: elif max_ratio != torrent.max_ratio and max_seeding_time != torrent.max_seeding_time:
body += logger.print_line( body += logger.print_line(
logger.insert_space(f"Share Limit: Max Ratio = {max_ratio}, Max Seed Time = {max_seeding_time} min", 4), logger.insert_space(f"Share Limit: Max Ratio = {max_ratio}, Max Seed Time = {max_seeding_time} min", 4),
loglevel, self.config.loglevel,
) )
# Update Torrents # Update Torrents
if not dry_run: if not self.config.dry_run:
if tags: if tags:
torrent.add_tags(tags) torrent.add_tags(tags)
if limit_upload_speed: if limit_upload_speed:
@ -366,8 +372,6 @@ class Qbt:
return body return body
def tag_nohardlinks(self): def tag_nohardlinks(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
num_tags = 0 # counter for the number of torrents that has no hard links num_tags = 0 # counter for the number of torrents that has no hard links
del_tor = 0 # counter for the number of torrents that has no hard links and \ del_tor = 0 # counter for the number of torrents that has no hard links and \
# meets the criteria for ratio limit/seed limit for deletion # meets the criteria for ratio limit/seed limit for deletion
@ -405,9 +409,13 @@ class Qbt:
if "noHL" not in torrent.tags: if "noHL" not in torrent.tags:
num_tags += 1 num_tags += 1
body = [] body = []
body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), loglevel) body += logger.print_line(
body += logger.print_line(logger.insert_space("Added Tag: noHL", 6), loglevel) logger.insert_space(f"Torrent Name: {torrent.name}", 3), self.config.loglevel
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel) )
body += logger.print_line(logger.insert_space("Added Tag: noHL", 6), self.config.loglevel)
body += logger.print_line(
logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel
)
body.extend( body.extend(
self.set_tags_and_limits( self.set_tags_and_limits(
torrent, torrent,
@ -454,11 +462,13 @@ class Qbt:
num_untag += 1 num_untag += 1
body = [] body = []
body += logger.print_line( body += logger.print_line(
f"Previous Tagged noHL Torrent Name: {torrent.name} has hard links found now.", loglevel f"Previous Tagged noHL Torrent Name: {torrent.name} has hard links found now.", self.config.loglevel
)
body += logger.print_line(logger.insert_space("Removed Tag: noHL", 6), self.config.loglevel)
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel)
body += logger.print_line(
f"{'Not Reverting' if self.config.dry_run else 'Reverting'} share limits.", self.config.loglevel
) )
body += logger.print_line(logger.insert_space("Removed Tag: noHL", 6), loglevel)
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel)
body += logger.print_line(f"{'Not Reverting' if dry_run else 'Reverting'} share limits.", loglevel)
restore_max_ratio = tracker["max_ratio"] restore_max_ratio = tracker["max_ratio"]
restore_max_seeding_time = tracker["max_seeding_time"] restore_max_seeding_time = tracker["max_seeding_time"]
restore_limit_upload_speed = tracker["limit_upload_speed"] restore_limit_upload_speed = tracker["limit_upload_speed"]
@ -468,7 +478,7 @@ class Qbt:
restore_max_seeding_time = -2 restore_max_seeding_time = -2
if restore_limit_upload_speed is None: if restore_limit_upload_speed is None:
restore_limit_upload_speed = -1 restore_limit_upload_speed = -1
if not dry_run: if not self.config.dry_run:
torrent.remove_tags(tags="noHL") torrent.remove_tags(tags="noHL")
body.extend( body.extend(
self.set_tags_and_limits( self.set_tags_and_limits(
@ -503,11 +513,13 @@ class Qbt:
if torrent["content_path"].replace(root_dir, root_dir) == tdel_dict[t_name]: if torrent["content_path"].replace(root_dir, root_dir) == tdel_dict[t_name]:
tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")]) tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")])
body = [] body = []
body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), loglevel) body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), self.config.loglevel)
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel) body += logger.print_line(
logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel
)
body += logger.print_line( body += logger.print_line(
logger.insert_space("Cleanup: True [No hard links found and meets Share Limits.]", 8), logger.insert_space("Cleanup: True [No hard links found and meets Share Limits.]", 8),
loglevel, self.config.loglevel,
) )
attr = { attr = {
"function": "cleanup_tag_nohardlinks", "function": "cleanup_tag_nohardlinks",
@ -523,61 +535,60 @@ class Qbt:
if t_count > 1 and ("" in t_msg or 2 in t_status): if t_count > 1 and ("" in t_msg or 2 in t_status):
del_tor += 1 del_tor += 1
attr["torrents_deleted_and_contents"] = False attr["torrents_deleted_and_contents"] = False
if not dry_run: if not self.config.dry_run:
self.tor_delete_recycle(torrent, attr) self.tor_delete_recycle(torrent, attr)
body += logger.print_line( body += logger.print_line(
logger.insert_space("Deleted .torrent but NOT content files.", 8), loglevel logger.insert_space("Deleted .torrent but NOT content files.", 8),
self.config.loglevel,
) )
else: else:
del_tor_cont += 1 del_tor_cont += 1
attr["torrents_deleted_and_contents"] = True attr["torrents_deleted_and_contents"] = True
if not dry_run: if not self.config.dry_run:
self.tor_delete_recycle(torrent, attr) self.tor_delete_recycle(torrent, attr)
body += logger.print_line( body += logger.print_line(
logger.insert_space("Deleted .torrent AND content files.", 8), loglevel logger.insert_space("Deleted .torrent AND content files.", 8), self.config.loglevel
) )
else: else:
del_tor += 1 del_tor += 1
attr["torrents_deleted_and_contents"] = False attr["torrents_deleted_and_contents"] = False
if not dry_run: if not self.config.dry_run:
self.tor_delete_recycle(torrent, attr) self.tor_delete_recycle(torrent, attr)
body += logger.print_line( body += logger.print_line(
logger.insert_space("Deleted .torrent but NOT content files.", 8), loglevel logger.insert_space("Deleted .torrent but NOT content files.", 8), self.config.loglevel
) )
attr["body"] = "\n".join(body) attr["body"] = "\n".join(body)
self.config.send_notifications(attr) self.config.send_notifications(attr)
self.torrentinfo[t_name]["count"] -= 1 self.torrentinfo[t_name]["count"] -= 1
if num_tags >= 1: if num_tags >= 1:
logger.print_line( logger.print_line(
f"{'Did not Tag/set' if dry_run else 'Tag/set'} share limits for {num_tags} " f"{'Did not Tag/set' if self.config.dry_run else 'Tag/set'} share limits for {num_tags} "
f".torrent{'s.' if num_tags > 1 else '.'}", f".torrent{'s.' if num_tags > 1 else '.'}",
loglevel, self.config.loglevel,
) )
else: else:
logger.print_line("No torrents to tag with no hard links.", loglevel) logger.print_line("No torrents to tag with no hard links.", self.config.loglevel)
if num_untag >= 1: if num_untag >= 1:
logger.print_line( logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} noHL tags / share limits for {num_untag} " f"{'Did not delete' if self.config.dry_run else 'Deleted'} noHL tags / share limits for {num_untag} "
f".torrent{'s.' if num_untag > 1 else '.'}", f".torrent{'s.' if num_untag > 1 else '.'}",
loglevel, self.config.loglevel,
) )
if del_tor >= 1: if del_tor >= 1:
logger.print_line( logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} {del_tor} " f"{'Did not delete' if self.config.dry_run else 'Deleted'} {del_tor} "
f".torrent{'s' if del_tor > 1 else ''} but not content files.", f".torrent{'s' if del_tor > 1 else ''} but not content files.",
loglevel, self.config.loglevel,
) )
if del_tor_cont >= 1: if del_tor_cont >= 1:
logger.print_line( logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} {del_tor_cont} " f"{'Did not delete' if self.config.dry_run else 'Deleted'} {del_tor_cont} "
f".torrent{'s' if del_tor_cont > 1 else ''} AND content files.", f".torrent{'s' if del_tor_cont > 1 else ''} AND content files.",
loglevel, self.config.loglevel,
) )
return num_tags, num_untag, del_tor, del_tor_cont return num_tags, num_untag, del_tor, del_tor_cont
def rem_unregistered(self): def rem_unregistered(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
del_tor = 0 del_tor = 0
del_tor_cont = 0 del_tor_cont = 0
num_tor_error = 0 num_tor_error = 0
@ -588,7 +599,7 @@ class Qbt:
cfg_tag_error = self.config.commands["tag_tracker_error"] cfg_tag_error = self.config.commands["tag_tracker_error"]
def tag_tracker_error(): def tag_tracker_error():
nonlocal dry_run, t_name, msg_up, msg, tracker, t_cat, torrent, tag_error, tor_error_summary, num_tor_error nonlocal t_name, msg_up, msg, tracker, t_cat, torrent, tag_error, tor_error_summary, num_tor_error
tor_error = "" tor_error = ""
tor_error += logger.insert_space(f"Torrent Name: {t_name}", 3) + "\n" tor_error += logger.insert_space(f"Torrent Name: {t_name}", 3) + "\n"
tor_error += logger.insert_space(f"Status: {msg}", 9) + "\n" tor_error += logger.insert_space(f"Status: {msg}", 9) + "\n"
@ -608,15 +619,15 @@ class Qbt:
"notifiarr_indexer": tracker["notifiarr"], "notifiarr_indexer": tracker["notifiarr"],
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
if not dry_run: if not self.config.dry_run:
torrent.add_tags(tags=tag_error) torrent.add_tags(tags=tag_error)
def del_unregistered(): def del_unregistered():
nonlocal dry_run, loglevel, del_tor, del_tor_cont, t_name, msg_up, msg, tracker, t_cat, t_msg, t_status, torrent nonlocal del_tor, del_tor_cont, t_name, msg_up, msg, tracker, t_cat, t_msg, t_status, torrent
body = [] body = []
body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), loglevel) body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), self.config.loglevel)
body += logger.print_line(logger.insert_space(f"Status: {msg}", 9), loglevel) body += logger.print_line(logger.insert_space(f"Status: {msg}", 9), self.config.loglevel)
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel) body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel)
attr = { attr = {
"function": "rem_unregistered", "function": "rem_unregistered",
"title": "Removing Unregistered Torrents", "title": "Removing Unregistered Torrents",
@ -630,21 +641,23 @@ class Qbt:
# Checks if any of the original torrents are working # Checks if any of the original torrents are working
if "" in t_msg or 2 in t_status: if "" in t_msg or 2 in t_status:
attr["torrents_deleted_and_contents"] = False attr["torrents_deleted_and_contents"] = False
if not dry_run: if not self.config.dry_run:
self.tor_delete_recycle(torrent, attr) self.tor_delete_recycle(torrent, attr)
body += logger.print_line(logger.insert_space("Deleted .torrent but NOT content files.", 8), loglevel) body += logger.print_line(
logger.insert_space("Deleted .torrent but NOT content files.", 8), self.config.loglevel
)
del_tor += 1 del_tor += 1
else: else:
attr["torrents_deleted_and_contents"] = True attr["torrents_deleted_and_contents"] = True
if not dry_run: if not self.config.dry_run:
self.tor_delete_recycle(torrent, attr) self.tor_delete_recycle(torrent, attr)
body += logger.print_line(logger.insert_space("Deleted .torrent AND content files.", 8), loglevel) body += logger.print_line(logger.insert_space("Deleted .torrent AND content files.", 8), self.config.loglevel)
del_tor_cont += 1 del_tor_cont += 1
else: else:
attr["torrents_deleted_and_contents"] = True attr["torrents_deleted_and_contents"] = True
if not dry_run: if not self.config.dry_run:
self.tor_delete_recycle(torrent, attr) self.tor_delete_recycle(torrent, attr)
body += logger.print_line(logger.insert_space("Deleted .torrent AND content files.", 8), loglevel) body += logger.print_line(logger.insert_space("Deleted .torrent AND content files.", 8), self.config.loglevel)
del_tor_cont += 1 del_tor_cont += 1
attr["body"] = "\n".join(body) attr["body"] = "\n".join(body)
self.config.send_notifications(attr) self.config.send_notifications(attr)
@ -684,11 +697,13 @@ class Qbt:
tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")]) tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")])
num_untag += 1 num_untag += 1
body = [] body = []
body += logger.print_line(f"Previous Tagged {tag_error} torrent currently has a working tracker.", loglevel) body += logger.print_line(
body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), loglevel) f"Previous Tagged {tag_error} torrent currently has a working tracker.", self.config.loglevel
body += logger.print_line(logger.insert_space(f"Removed Tag: {tag_error}", 4), loglevel) )
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), loglevel) body += logger.print_line(logger.insert_space(f"Torrent Name: {torrent.name}", 3), self.config.loglevel)
if not dry_run: body += logger.print_line(logger.insert_space(f"Removed Tag: {tag_error}", 4), self.config.loglevel)
body += logger.print_line(logger.insert_space(f'Tracker: {tracker["url"]}', 8), self.config.loglevel)
if not self.config.dry_run:
torrent.remove_tags(tags=tag_error) torrent.remove_tags(tags=tag_error)
attr = { attr = {
"function": "untag_tracker_error", "function": "untag_tracker_error",
@ -746,35 +761,36 @@ class Qbt:
if del_tor >= 1 or del_tor_cont >= 1: if del_tor >= 1 or del_tor_cont >= 1:
if del_tor >= 1: if del_tor >= 1:
logger.print_line( logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} {del_tor} " f"{'Did not delete' if self.config.dry_run else 'Deleted'} {del_tor} "
f".torrent{'s' if del_tor > 1 else ''} but not content files.", f".torrent{'s' if del_tor > 1 else ''} but not content files.",
loglevel, self.config.loglevel,
) )
if del_tor_cont >= 1: if del_tor_cont >= 1:
logger.print_line( logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} {del_tor_cont} " f"{'Did not delete' if self.config.dry_run else 'Deleted'} {del_tor_cont} "
f".torrent{'s' if del_tor_cont > 1 else ''} AND content files.", f".torrent{'s' if del_tor_cont > 1 else ''} AND content files.",
loglevel, self.config.loglevel,
) )
else: else:
logger.print_line("No unregistered torrents found.", loglevel) logger.print_line("No unregistered torrents found.", self.config.loglevel)
if num_untag >= 1: if num_untag >= 1:
logger.print_line( logger.print_line(
f"{'Did not delete' if dry_run else 'Deleted'} {tag_error} tags for {num_untag} " f"{'Did not delete' if self.config.dry_run else 'Deleted'} {tag_error} tags for {num_untag} "
f".torrent{'s.' if num_untag > 1 else '.'}", f".torrent{'s.' if num_untag > 1 else '.'}",
loglevel, self.config.loglevel,
) )
if num_tor_error >= 1: if num_tor_error >= 1:
logger.separator( logger.separator(
f"{num_tor_error} Torrents with tracker errors found", space=False, border=False, loglevel=loglevel f"{num_tor_error} Torrents with tracker errors found",
space=False,
border=False,
loglevel=self.config.loglevel,
) )
logger.print_line(tor_error_summary.rstrip(), loglevel) logger.print_line(tor_error_summary.rstrip(), self.config.loglevel)
return del_tor, del_tor_cont, num_tor_error, num_untag return del_tor, del_tor_cont, num_tor_error, num_untag
# Function used to move any torrents from the cross seed directory to the correct save directory # Function used to move any torrents from the cross seed directory to the correct save directory
def cross_seed(self): def cross_seed(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
added = 0 # Keep track of total torrents tagged added = 0 # Keep track of total torrents tagged
tagged = 0 # Track # of torrents tagged that are not cross-seeded tagged = 0 # Track # of torrents tagged that are not cross-seeded
if self.config.commands["cross_seed"]: if self.config.commands["cross_seed"]:
@ -804,11 +820,13 @@ class Qbt:
if self.torrentinfo[t_name]["is_complete"]: if self.torrentinfo[t_name]["is_complete"]:
categories.append(category) categories.append(category)
body = [] body = []
body += logger.print_line(f"{'Not Adding' if dry_run else 'Adding'} to qBittorrent:", loglevel) body += logger.print_line(
body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), loglevel) f"{'Not Adding' if self.config.dry_run else 'Adding'} to qBittorrent:", self.config.loglevel
body += logger.print_line(logger.insert_space(f"Category: {category}", 7), loglevel) )
body += logger.print_line(logger.insert_space(f"Save_Path: {dest}", 6), loglevel) body += logger.print_line(logger.insert_space(f"Torrent Name: {t_name}", 3), self.config.loglevel)
body += logger.print_line(logger.insert_space(f"Tracker: {t_tracker}", 8), loglevel) body += logger.print_line(logger.insert_space(f"Category: {category}", 7), self.config.loglevel)
body += logger.print_line(logger.insert_space(f"Save_Path: {dest}", 6), self.config.loglevel)
body += logger.print_line(logger.insert_space(f"Tracker: {t_tracker}", 8), self.config.loglevel)
attr = { attr = {
"function": "cross_seed", "function": "cross_seed",
"title": "Adding New Cross-Seed Torrent", "title": "Adding New Cross-Seed Torrent",
@ -821,18 +839,20 @@ class Qbt:
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
added += 1 added += 1
if not dry_run: if not self.config.dry_run:
self.client.torrents.add( self.client.torrents.add(
torrent_files=src, save_path=dest, category=category, tags="cross-seed", is_paused=True torrent_files=src, save_path=dest, category=category, tags="cross-seed", is_paused=True
) )
util.move_files(src, dir_cs_out) util.move_files(src, dir_cs_out)
else: else:
logger.print_line(f"Found {t_name} in {dir_cs} but original torrent is not complete.", loglevel) logger.print_line(
logger.print_line("Not adding to qBittorrent", loglevel) f"Found {t_name} in {dir_cs} but original torrent is not complete.", self.config.loglevel
)
logger.print_line("Not adding to qBittorrent", self.config.loglevel)
else: else:
error = f"{t_name} not found in torrents. Cross-seed Torrent not added to qBittorrent." error = f"{t_name} not found in torrents. Cross-seed Torrent not added to qBittorrent."
if dry_run: if self.config.dry_run:
logger.print_line(error, loglevel) logger.print_line(error, self.config.loglevel)
else: else:
logger.print_line(error, "WARNING") logger.print_line(error, "WARNING")
self.config.notify(error, "cross-seed", False) self.config.notify(error, "cross-seed", False)
@ -847,7 +867,9 @@ class Qbt:
): ):
tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")]) tracker = self.config.get_tags([x.url for x in torrent.trackers if x.url.startswith("http")])
tagged += 1 tagged += 1
body = logger.print_line(f"{'Not Adding' if dry_run else 'Adding'} 'cross-seed' tag to {t_name}", loglevel) body = logger.print_line(
f"{'Not Adding' if self.config.dry_run else 'Adding'} 'cross-seed' tag to {t_name}", self.config.loglevel
)
attr = { attr = {
"function": "tag_cross_seed", "function": "tag_cross_seed",
"title": "Tagging Cross-Seed Torrent", "title": "Tagging Cross-Seed Torrent",
@ -858,25 +880,29 @@ class Qbt:
"torrent_tracker": tracker, "torrent_tracker": tracker,
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
if not dry_run: if not self.config.dry_run:
torrent.add_tags(tags="cross-seed") torrent.add_tags(tags="cross-seed")
numcategory = Counter(categories) numcategory = Counter(categories)
for c in numcategory: for c in numcategory:
if numcategory[c] > 0: if numcategory[c] > 0:
logger.print_line( logger.print_line(
f"{numcategory[c]} {c} cross-seed .torrents {'not added' if dry_run else 'added'}.", loglevel f"{numcategory[c]} {c} cross-seed .torrents {'not added' if self.config.dry_run else 'added'}.",
self.config.loglevel,
) )
if added > 0: if added > 0:
logger.print_line(f"Total {added} cross-seed .torrents {'not added' if dry_run else 'added'}.", loglevel) logger.print_line(
f"Total {added} cross-seed .torrents {'not added' if self.config.dry_run else 'added'}.", self.config.loglevel
)
if tagged > 0: if tagged > 0:
logger.print_line(f"Total {tagged} cross-seed .torrents {'not tagged' if dry_run else 'tagged'}.", loglevel) logger.print_line(
f"Total {tagged} cross-seed .torrents {'not tagged' if self.config.dry_run else 'tagged'}.",
self.config.loglevel,
)
return added, tagged return added, tagged
# Function used to recheck paused torrents sorted by size and resume torrents that are completed # Function used to recheck paused torrents sorted by size and resume torrents that are completed
def recheck(self): def recheck(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
resumed = 0 resumed = 0
rechecked = 0 rechecked = 0
if self.config.commands["recheck"]: if self.config.commands["recheck"]:
@ -891,7 +917,8 @@ class Qbt:
if torrent.max_ratio < 0 and torrent.max_seeding_time < 0: if torrent.max_ratio < 0 and torrent.max_seeding_time < 0:
resumed += 1 resumed += 1
body = logger.print_line( body = logger.print_line(
f"{'Not Resuming' if dry_run else 'Resuming'} [{tracker['tag']}] - {torrent.name}", loglevel f"{'Not Resuming' if self.config.dry_run else 'Resuming'} [{tracker['tag']}] - {torrent.name}",
self.config.loglevel,
) )
attr = { attr = {
"function": "recheck", "function": "recheck",
@ -903,7 +930,7 @@ class Qbt:
"notifiarr_indexer": tracker["notifiarr"], "notifiarr_indexer": tracker["notifiarr"],
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
if not dry_run: if not self.config.dry_run:
torrent.resume() torrent.resume()
else: else:
# Check to see if torrent meets AutoTorrentManagement criteria # Check to see if torrent meets AutoTorrentManagement criteria
@ -935,7 +962,9 @@ class Qbt:
): ):
resumed += 1 resumed += 1
body = logger.print_line( body = logger.print_line(
f"{'Not Resuming' if dry_run else 'Resuming'} [{tracker['tag']}] - {torrent.name}", loglevel f"{'Not Resuming' if self.config.dry_run else 'Resuming'} [{tracker['tag']}] - "
f"{torrent.name}",
self.config.loglevel,
) )
attr = { attr = {
"function": "recheck", "function": "recheck",
@ -947,7 +976,7 @@ class Qbt:
"notifiarr_indexer": tracker["notifiarr"], "notifiarr_indexer": tracker["notifiarr"],
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
if not dry_run: if not self.config.dry_run:
torrent.resume() torrent.resume()
# Recheck # Recheck
elif ( elif (
@ -957,7 +986,8 @@ class Qbt:
): ):
rechecked += 1 rechecked += 1
body = logger.print_line( body = logger.print_line(
f"{'Not Rechecking' if dry_run else 'Rechecking'} [{tracker['tag']}] - {torrent.name}", loglevel f"{'Not Rechecking' if self.config.dry_run else 'Rechecking'} [{tracker['tag']}] - {torrent.name}",
self.config.loglevel,
) )
attr = { attr = {
"function": "recheck", "function": "recheck",
@ -969,13 +999,11 @@ class Qbt:
"notifiarr_indexer": tracker["notifiarr"], "notifiarr_indexer": tracker["notifiarr"],
} }
self.config.send_notifications(attr) self.config.send_notifications(attr)
if not dry_run: if not self.config.dry_run:
torrent.recheck() torrent.recheck()
return resumed, rechecked return resumed, rechecked
def rem_orphaned(self): def rem_orphaned(self):
dry_run = self.config.commands["dry_run"]
loglevel = "DRYRUN" if dry_run else "INFO"
orphaned = 0 orphaned = 0
if self.config.commands["rem_orphaned"]: if self.config.commands["rem_orphaned"]:
logger.separator("Checking for Orphaned Files", space=False, border=False) logger.separator("Checking for Orphaned Files", space=False, border=False)
@ -1029,12 +1057,12 @@ class Qbt:
os.makedirs(orphaned_path, exist_ok=True) os.makedirs(orphaned_path, exist_ok=True)
body = [] body = []
num_orphaned = len(orphaned_files) num_orphaned = len(orphaned_files)
logger.print_line(f"{num_orphaned} Orphaned files found", loglevel) logger.print_line(f"{num_orphaned} Orphaned files found", self.config.loglevel)
body += logger.print_line("\n".join(orphaned_files), loglevel) body += logger.print_line("\n".join(orphaned_files), self.config.loglevel)
body += logger.print_line( body += logger.print_line(
f"{'Did not move' if dry_run else 'Moved'} {num_orphaned} Orphaned files " f"{'Did not move' if self.config.dry_run else 'Moved'} {num_orphaned} Orphaned files "
f"to {orphaned_path.replace(remote_path,root_path)}", f"to {orphaned_path.replace(remote_path,root_path)}",
loglevel, self.config.loglevel,
) )
attr = { attr = {
@ -1048,7 +1076,7 @@ class Qbt:
self.config.send_notifications(attr) self.config.send_notifications(attr)
# Delete empty directories after moving orphan files # Delete empty directories after moving orphan files
logger.info("Cleaning up any empty directories...") logger.info("Cleaning up any empty directories...")
if not dry_run: if not self.config.dry_run:
for file in alive_it(orphaned_files): for file in alive_it(orphaned_files):
src = file.replace(root_path, remote_path) src = file.replace(root_path, remote_path)
dest = os.path.join(orphaned_path, file.replace(root_path, "")) dest = os.path.join(orphaned_path, file.replace(root_path, ""))
@ -1057,7 +1085,7 @@ class Qbt:
for parent_path in orphaned_parent_path: for parent_path in orphaned_parent_path:
util.remove_empty_directories(parent_path, "**/*") util.remove_empty_directories(parent_path, "**/*")
else: else:
logger.print_line("No Orphaned Files found.", loglevel) logger.print_line("No Orphaned Files found.", self.config.loglevel)
return orphaned return orphaned
def tor_delete_recycle(self, torrent, info): def tor_delete_recycle(self, torrent, info):