From 3aa5931095c342a55fad23e583572241f13d3056 Mon Sep 17 00:00:00 2001 From: jblu Date: Sat, 23 Jul 2022 15:46:48 -0500 Subject: [PATCH] added min tor time, large performance improvement --- README.md | 4 +++- config.json.example | 4 +++- qbit-maid.py | 3 +++ qlist.py | 37 ++++--------------------------------- qlogging.py | 43 ++++++++++++++++++++++++++++++++++++++++++- qprocess.py | 17 +++++------------ 6 files changed, 60 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index d866bdd..4afe113 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,9 @@ You will need a category-whitelist.json in the root directory. This will ignore | non_protected_tag | we don't care about these torrents | | logpath | will write a log in root directory if left as is other wise specify other path using forward slashes | | age | number, seconds for how long we keep torrents from IPTORRENTS | +| minimum_age | age in seconds torrents should reached before they are removed | | use_pushover | true or false to enable or disable pushover notification summary | | use_log | true or false to enable or disable writing to alog file | | po_key | pushover key | -| po_token | pushover api token | \ No newline at end of file +| po_token | pushover api token | +| delete_torrents | true or false to enable or disable deletion. Useful for dry-runs | \ No newline at end of file diff --git a/config.json.example b/config.json.example index 5f5689e..6a9fb9b 100644 --- a/config.json.example +++ b/config.json.example @@ -8,8 +8,10 @@ "protected_tag": "ipt", "non_protected_tag": "public", "age": 2419200, + "minimum_age": 432000, "use_pushover": true, "use_log": true, "po_key": "", - "po_token": "" + "po_token": "", + "delete_torrents": false } \ No newline at end of file diff --git a/qbit-maid.py b/qbit-maid.py index 4902257..08bcdc2 100644 --- a/qbit-maid.py +++ b/qbit-maid.py @@ -37,6 +37,8 @@ class Qbt: self.loglevel = self.config["loglevel"] self.tracker_protected_tag = self.config["protected_tag"] self.tracker_non_protected_tag = self.config["non_protected_tag"] + self.minimum_age = self.config["minimum_age"] + self.age = self.config["age"] # Calling log and notify functions torlog(self) tornotify(self) @@ -46,6 +48,7 @@ class Qbt: self.tracker_whitelist = load(f) self.tracker_list = [] self.up_tor_counter = 0 + self.preme_tor_counter = 0 self.torrent_hash_delete_list = [] if self.use_log: self.tl.debug(self.tracker_whitelist) diff --git a/qlist.py b/qlist.py index 58074c2..ba4c7c9 100644 --- a/qlist.py +++ b/qlist.py @@ -9,6 +9,9 @@ def buildtorlist(self): torrent = self.torrentlist.pop() if self.use_log: self.tl.debug(f'["{torrent["name"][0:20]}..."] {torrent["infohash_v1"]}') + if torrent['added_on'] + self.minimum_age <= self.t.time(): + self.preme_tor_counter += 1 + continue if torrent['category'] in self.cat_whitelist.values(): self.tl.info(f'Ignored torrent:["{torrent["name"][0:20]}..."]') continue @@ -27,36 +30,4 @@ def buildtorlist(self): 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_list.append(torrent) - -def writetor(self, filepath='./torrentinfo.txt'): - """Write all torrent data to a file. - Useful for development of new features. - """ - with open(filepath, 'w') as fp: - fp.write(str(self.torrentlist)) - -def listfirsttor(self, index=0): - """Only lists the first torrent""" - self.tl.debug('First torrent in the list:') - torrent = self.torrentlist[index] - for k,v in torrent.items(): - self.tl.debug(f'{k}: {v}') - self.tl.debug('\n') - -def listqbitapiinfo(self): - """Writes torrent info to log file""" - self.tl.debug(f'qBittorrent: {self.qbt_client.app.version}') - self.tl.debug(f'qBittorrent Web API: {self.qbt_client.app.web_api_version}') - -def torrentcount(self): - """write torrent counts to log file""" - 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 = [self.tracker_protected_tag, self.tracker_non_protected_tag] - self.qbt_client.torrents_delete_tags(tag_list) - -def torlisttags(self): - pass \ No newline at end of file + self.tracker_list.append(torrent) \ No newline at end of file diff --git a/qlogging.py b/qlogging.py index 8c4b3ea..f2a8e1b 100644 --- a/qlogging.py +++ b/qlogging.py @@ -19,12 +19,53 @@ def tornotifytest(self): def tornotifysummary(self): """Main notification method when the app is used in an automated fashion""" self.poc.send_message(f" Total: {self.total_torrents}\n\ + Premature: {self.preme_tor_counter}\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 printprocessor(self): + """Print summary of torrents""" + 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'Premature: {self.preme_tor_counter}') + 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 getunixtimestamp(self): """Used for debuging and development related to unixtimestamps, not used in main script but useful""" self.uts = self.t.time() - self.tl.info(self.uts) \ No newline at end of file + self.tl.info(self.uts) + +def writetor(self, filepath='./torrentinfo.txt'): + """Write all torrent data to a file. + Useful for development of new features. + """ + with open(filepath, 'w') as fp: + fp.write(str(self.torrentlist)) + +def listfirsttor(self, index=0): + """Only lists the first torrent""" + self.tl.debug('First torrent in the list:') + torrent = self.torrentlist[index] + for k,v in torrent.items(): + self.tl.debug(f'{k}: {v}') + self.tl.debug('\n') + +def listqbitapiinfo(self): + """Writes torrent info to log file""" + self.tl.debug(f'qBittorrent: {self.qbt_client.app.version}') + self.tl.debug(f'qBittorrent Web API: {self.qbt_client.app.web_api_version}') + +def torrentcount(self): + """write torrent counts to log file""" + 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 torlisttags(self): + pass \ No newline at end of file diff --git a/qprocess.py b/qprocess.py index d5c346d..b58b5d9 100644 --- a/qprocess.py +++ b/qprocess.py @@ -10,9 +10,9 @@ def torprocessor(self): 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(): + if canidate['added_on'] + self.age <= self.t.time(): if self.use_log: - self.tl.debug(f'["{canidate["name"][0:20]}..."] Seconds old: {self.t.time() - self.config["age"] - canidate["added_on"]}') + self.tl.debug(f'["{canidate["name"][0:20]}..."] Seconds old: {self.t.time() - self.age - canidate["added_on"]}') 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.') @@ -31,16 +31,9 @@ def torprocessor(self): self.up_tor_counter += 1 continue -def printprocessor(self): - """Print summary of torrents""" - 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 tordeletetags(self): + tag_list = [self.tracker_protected_tag, self.tracker_non_protected_tag] + self.qbt_client.torrents_delete_tags(tag_list) def tordelete(self): """Remove torrents, will also delete files, this keeps the filesystem clean.