#9 lots of changes, refactored qprocess #11

Merged
jonbranan merged 1 commits from refactor-qprocess into main 2022-07-23 15:00:05 -05:00
4 changed files with 39 additions and 33 deletions
Showing only changes of commit ae4350a547 - Show all commits

View File

@ -7,6 +7,7 @@ from qlogging import *
from qprocess import *
import time
import logging
from collections import Counter
class Qbt:
def __init__(self):
@ -26,6 +27,7 @@ class Qbt:
# Create the logging and pushover objects
self.tl = logging
self.po = pushover
self.ct = Counter
# Variables torlog uses from config.json
self.use_pushover = self.config["use_pushover"]
self.use_log = self.config["use_log"]
@ -42,8 +44,8 @@ class Qbt:
# Pulling domain names to treat carefully
f = open('./tracker-whitelist.json')
self.tracker_whitelist = load(f)
self.tracker_protected_list = []
self.tracker_nonprotected_list = []
self.tracker_list = []
self.up_tor_counter = 0
self.torrent_hash_delete_list = []
if self.use_log:
self.tl.debug(self.tracker_whitelist)
@ -71,6 +73,7 @@ class Qbt:
printprocessor(self)
if self.use_pushover:
tornotifysummary(self)
if self.config["delete_torrents"]:
tordelete(self)
# Run

View File

@ -4,6 +4,7 @@ def buildtorlist(self):
V2 will certainly be more performant. The reason two lists were used was so that torrents
that are in public trackers woudln't be around as long as torrents from a private tracker.
"""
self.total_torrents = len(self.torrentlist)
while self.torrentlist:
torrent = self.torrentlist.pop()
if self.use_log:
@ -18,13 +19,15 @@ def buildtorlist(self):
if torrent['tracker'].split('/')[2] in self.tracker_whitelist.values():
if self.use_log:
self.tl.debug(f'Protected torrent: {torrent["tracker"]}hash: {torrent["hash"]}')
if torrent['tags'] == '':
self.qbt_client.torrents_add_tags(self.tracker_protected_tag,torrent['hash'])
self.tracker_protected_list.append(torrent)
self.tracker_list.append(torrent)
if torrent['tracker'].split('/')[2] not in self.tracker_whitelist.values():
if self.use_log:
self.tl.debug(f'Non-protected torrent: {torrent["tracker"]}hash: {torrent["hash"]}')
if torrent['tags'] == '':
self.qbt_client.torrents_add_tags(self.tracker_non_protected_tag,torrent['hash'])
self.tracker_nonprotected_list.append(torrent)
self.tracker_list.append(torrent)
def writetor(self, filepath='./torrentinfo.txt'):
"""Write all torrent data to a file.
@ -48,11 +51,11 @@ def listqbitapiinfo(self):
def torrentcount(self):
"""write torrent counts to log file"""
self.tl.debug(f'torrents that are protected {self.protected_count}')
self.tl.debug(f"torrents that aren't protected {self.nonprotected_count}")
self.tl.debug(f'torrents that are protected {self.tracker_list.count("ipt")}')
self.tl.debug(f'torrents that aren\'t protected {self.tracker_list.count("public")}')
def tordeletetags(self):
tag_list = ['ipt','public','iptorrents']
tag_list = [self.tracker_protected_tag, self.tracker_non_protected_tag]
self.qbt_client.torrents_delete_tags(tag_list)
def torlisttags(self):

View File

@ -18,9 +18,11 @@ def tornotifytest(self):
def tornotifysummary(self):
"""Main notification method when the app is used in an automated fashion"""
self.poc.send_message(f' Protected torrents: {len(self.tracker_protected_list)}\n\
Non-protected torrents: {len(self.tracker_nonprotected_list)}\n\
Total torrents set for deletion: {len(self.torrent_hash_delete_list)}', title="qbit-maid summary")
self.poc.send_message(f" Total: {self.total_torrents}\n\
Protected: {self.c[self.tracker_protected_tag]}\n\
Non-protected: {self.c[self.tracker_non_protected_tag]}\n\
Marked for deletion: {len(self.torrent_hash_delete_list)}\n\
Orphaned: {self.up_tor_counter}", title="--- qbit-maid summary ---")
def getunixtimestamp(self):
"""Used for debuging and development related to unixtimestamps, not used in main script but useful"""

View File

@ -2,25 +2,12 @@ def torprocessor(self):
"""Main logic to sort through both self.tracker_nonprotected_list and self.tracker_protected_list
If torrent meets criteria for deletion, its infohash_v1 will be appended to self.torrent_hash_delete_list
"""
for canidate in self.tracker_nonprotected_list:
if self.config["protected_tag"] in canidate['tags']:
if self.use_log:
self.tl.warning(f'["{canidate["name"][0:20]}..."] was in non-protected list.')
continue
for canidate in self.tracker_list:
if canidate['state'] == 'downloading':
if self.use_log:
self.tl.info(f'["{canidate["name"][0:20]}..."] is still downloading and will be skipped.')
continue
else:
self.torrent_hash_delete_list.append(canidate['infohash_v1'])
if self.use_log:
self.tl.info(f'Submitted ["{canidate["name"][0:20]}..."] for deletion.')
for canidate in self.tracker_protected_list:
if canidate['state'] == 'downloading':
if self.use_log:
self.tl.warning(f'["{canidate["name"][0:20]}..."] is still downloading and will be skipped.')
continue
if canidate['ratio'] < float(1.05):
elif canidate['ratio'] < float(1.05) and self.tracker_protected_tag in canidate["tags"]:
if self.use_log:
self.tl.debug(f'["{canidate["name"][0:20]}..."] is below a 1.05 ratio({canidate["ratio"]})')
if canidate['added_on'] + self.config["age"] <= self.t.time():
@ -29,20 +16,31 @@ def torprocessor(self):
self.torrent_hash_delete_list.append(canidate['infohash_v1'])
if self.use_log:
self.tl.info(f'Submitted ["{canidate["name"][0:20]}..."] for deletion from the protected list.')
if canidate['ratio'] >= float(1.05):
elif canidate['ratio'] >= float(1.05) and self.tracker_protected_tag in canidate["tags"]:
if self.use_log:
self.tl.debug(f'["{canidate["name"][0:20]}..."] is above a 1.05 ratio({canidate["ratio"]}).')
self.torrent_hash_delete_list.append(canidate['infohash_v1'])
if self.use_log:
self.tl.info(f'Submitted ["{canidate["name"][0:20]}..."] for deletion from the protected list.')
elif self.tracker_non_protected_tag in canidate["tags"]:
self.torrent_hash_delete_list.append(canidate['infohash_v1'])
if self.use_log:
self.tl.info(f'Submitted ["{canidate["name"][0:20]}..."] for deletion.')
else:
pass
self.tl.info(f'["{canidate["name"][0:20]}..."] is orphaned.')
self.up_tor_counter += 1
continue
def printprocessor(self):
"""Print summary of torrents"""
self.tl.info(f'Protected torrents: {len(self.tracker_protected_list)}')
self.tl.info(f'Non-protected torrents: {len(self.tracker_nonprotected_list)}')
self.tl.info(f'Total torrents set for deletion: {len(self.torrent_hash_delete_list)}')
self.c = self.ct()
for item in self.tracker_list:
self.c[item["tags"]] += 1
self.tl.info(f'Total: {self.total_torrents}')
self.tl.info(f'Protected: {self.c[self.tracker_protected_tag]}')
self.tl.info(f'Non-protected: {self.c[self.tracker_non_protected_tag]}')
self.tl.info(f'Orphaned: {self.up_tor_counter}')
self.tl.info(f'Marked for deletion: {len(self.torrent_hash_delete_list)}')
def tordelete(self):
"""Remove torrents, will also delete files, this keeps the filesystem clean.