From 9fc77d3ea067613500f8740878aaa23c5c9c0be1 Mon Sep 17 00:00:00 2001 From: jblu Date: Fri, 26 Aug 2022 17:28:47 -0500 Subject: [PATCH 1/3] refactored the main processes --- qlist.py | 47 ++++++++++++++++++++++++++++------ qprocess.py | 41 +++++++++++++++++++++++++----- test_qbitmaid.py | 66 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 126 insertions(+), 28 deletions(-) diff --git a/qlist.py b/qlist.py index 6d6cfc7..93cf50b 100644 --- a/qlist.py +++ b/qlist.py @@ -9,27 +9,58 @@ 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(): + # if torrent['added_on'] + self.minimum_age >= self.t.time(): + if ispreme(torrent['added_on'], self.minimum_age, self.t.time()): self.preme_tor_counter += 1 continue - if torrent['category'] in self.cat_whitelist.values(): + # if torrent['category'] in self.cat_whitelist.values(): + if iscatignored(torrent['category'], self.cat_whitelist.values()): self.tl.info(f'Ignored torrent:["{torrent["name"][0:20]}..."]') self.ignored_counter += 1 continue - if torrent['tracker'] == '': + # if torrent['tracker'] == '': + if istrackerblank(torrent['tracker']): if self.use_log: self.tl.warning(f'Torrent doesn\'t have a tracker ["{torrent["name"][0:20]}..."] [{torrent["tracker"]}]hash: {torrent["hash"]}') self.ignored_counter += 1 continue - if torrent['tracker'].split('/')[2] in self.tracker_whitelist.values(): + # if torrent['tracker'].split('/')[2] in self.tracker_whitelist.values(): + if isprotectedtracker(torrent['tracker'], self.tracker_whitelist.values()): if self.use_log: self.tl.debug(f'Protected torrent: {torrent["tracker"]}hash: {torrent["hash"]}') - if torrent['tags'] == '': + # if torrent['tags'] == '': + if istagblank(torrent['tags']): self.qbt_client.torrents_add_tags(self.tracker_protected_tag,torrent['hash']) self.tracker_list.append(torrent) - if torrent['tracker'].split('/')[2] not in self.tracker_whitelist.values(): + if isnotprotectedtracker(torrent['tracker'], self.tracker_whitelist.values()): if self.use_log: self.tl.debug(f'Non-protected torrent: {torrent["tracker"]}hash: {torrent["hash"]}') - if torrent['tags'] == '': + # if torrent['tags'] == '': + if istagblank(torrent['tags']): self.qbt_client.torrents_add_tags(self.tracker_non_protected_tag,torrent['hash']) - self.tracker_list.append(torrent) \ No newline at end of file + self.tracker_list.append(torrent) + +def ispreme(added, minage, time): + if added + minage >= time: + return True + +def iscatignored(cat, catlist): + if cat in catlist: + return True + +def istrackerblank(tracker): + if tracker == '': + return True + +def isprotectedtracker(tracker, trackerlist): + if tracker.split('/')[2] in trackerlist: + return True + +def isnotprotectedtracker(tracker, trackerlist): + if tracker.split('/')[2] not in trackerlist: + return True + +def istagblank(tag): + if tag == '': + return True + \ No newline at end of file diff --git a/qprocess.py b/qprocess.py index bf6c2f4..f24bef7 100644 --- a/qprocess.py +++ b/qprocess.py @@ -3,30 +3,36 @@ def torprocessor(self): If torrent meets criteria for deletion, its infohash_v1 will be appended to self.torrent_hash_delete_list """ for canidate in self.tracker_list: - if canidate['state'] == 'downloading': + # if canidate['state'] == 'downloading': + if isdownloading(canidate['state']): if self.use_log: self.tl.info(f'["{canidate["name"][0:20]}..."] is still downloading and will be skipped.') continue - elif canidate['ratio'] < float(1.05) and self.tracker_protected_tag in canidate["tags"]: + # elif canidate['ratio'] < float(1.05) and self.tracker_protected_tag in canidate["tags"]: + elif isprotectedunderratio(canidate['ratio'], 1.05, self.tracker_protected_tag, 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.age <= self.t.time(): + # if canidate['added_on'] + self.age <= self.t.time(): + if isoldtor(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.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.') - elif canidate['ratio'] >= float(1.05) and self.tracker_protected_tag in canidate["tags"]: + # elif canidate['ratio'] >= float(1.05) and self.tracker_protected_tag in canidate["tags"]: + elif isprotectedoverratio(canidate['ratio'], 1.05, self.tracker_protected_tag, 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"]: + # elif self.tracker_non_protected_tag in canidate["tags"]: + elif isnonprotectedtor(self.tracker_non_protected_tag, 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: + dragnet(self, canidate['state'],canidate['ratio'],canidate["tags"],canidate['added_on'],self.age,self.t.time(),canidate['infohash_v1'],canidate["name"][0:20]) self.tl.info(f'["{canidate["name"][0:20]}..."] is orphaned.') self.up_tor_counter += 1 continue @@ -41,4 +47,27 @@ def tordelete(self): if self.use_log: self.tl.debug('Hash list submitted for deletion:') self.tl.debug(self.torrent_hash_delete_list) - self.qbt_client.torrents_delete(True, self.torrent_hash_delete_list) \ No newline at end of file + self.qbt_client.torrents_delete(True, self.torrent_hash_delete_list) + +def isdownloading(state): + if state == 'downloading': + return True + +def isprotectedunderratio(torratio, setratio, settag, tortag): + if torratio < float(setratio) and settag in tortag: + return True + +def isoldtor(toradd, setage, currenttime): + if toradd + setage <= currenttime: + return True + +def isprotectedoverratio(torratio, setratio, settag, tortag): + if torratio >= float(setratio) and settag in tortag: + return True + +def isnonprotectedtor(setnonprotectedtag, tortags): + if setnonprotectedtag in tortags: + return True + +def dragnet(self,state,ratio,tags,added,age,time,thash,tname): + pass \ No newline at end of file diff --git a/test_qbitmaid.py b/test_qbitmaid.py index 3d2ce5e..d47437c 100644 --- a/test_qbitmaid.py +++ b/test_qbitmaid.py @@ -1,18 +1,56 @@ import unittest -from qprocess import torprocessor -import json -import logging -import sys -class TestQprocess(unittest.TestCase): - def test_log_and_test_data(self): - self.log= logging.getLogger( "SomeTest.testSomething" ) - self.tracker_list = open("./test/torrentinfo.txt", "r") - self.log.debug(self.tracker_list) - assert self.tracker_list - - # torprocessor(self) +from qlist import ispreme,iscatignored,istrackerblank,isprotectedtracker +class TestQbitmaid(unittest.TestCase): + def test_ispreme_sanity(self): + self.assertTrue(ispreme(1,1,1)) + self.assertFalse(ispreme(1,1,3)) + + def test_ispreme(self): + pass + + def test_iscatignored_sanity(self): + self.assertTrue(iscatignored('a', ['a','b','c'])) + self.assertTrue(iscatignored('b', ['a','b','c'])) + self.assertTrue(iscatignored('c', ['a','b','c'])) + self.assertFalse(iscatignored('d', ['a','b','c'])) + self.assertFalse(iscatignored(1, ['a','b','c'])) + self.assertFalse(iscatignored(1.0000000, ['a','b','c'])) + + def test_iscatignored(self): + pass + + def test_istrackerblank_sanity(self): + self.assertTrue(istrackerblank('')) + self.assertFalse(istrackerblank('a')) + self.assertFalse(istrackerblank(1)) + self.assertFalse(istrackerblank(1.00000000)) + + def test_istrackerblank(self): + pass + + def test_isprotectedtracker_sanity(self): + self.assertTrue(isprotectedtracker('https://a.com/',['a.com','b.me','c.io'])) + self.assertFalse(isprotectedtracker('https://google.com/',['a.com','b.me','c.io'])) + self.assertFalse(isprotectedtracker('https://d.com',['a.com','b.me','c.io'])) + + def test_isprotectedtracker(self): + pass + + def test_isnotprotectedtracker_sanity(self): + pass + + def test_isnotprotectedtracker(self): + pass + + def test_istagblank(self): + pass + + def test_(self): + pass + + def test_(self): + pass + if __name__ == '__main__': - logging.basicConfig( stream=sys.stderr ) - logging.getLogger( "SomeTest.testSomething" ).setLevel( logging.DEBUG ) unittest.main() \ No newline at end of file -- 2.45.2 From 4cd1c519e218a229883a5fcd377ee822480dfc03 Mon Sep 17 00:00:00 2001 From: jblu Date: Tue, 30 Aug 2022 23:15:28 -0500 Subject: [PATCH 2/3] finished minimal unit tests --- qprocess.py | 4 ++-- test_qbitmaid.py | 51 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/qprocess.py b/qprocess.py index f24bef7..c148f36 100644 --- a/qprocess.py +++ b/qprocess.py @@ -32,7 +32,7 @@ def torprocessor(self): if self.use_log: self.tl.info(f'Submitted ["{canidate["name"][0:20]}..."] for deletion.') else: - dragnet(self, canidate['state'],canidate['ratio'],canidate["tags"],canidate['added_on'],self.age,self.t.time(),canidate['infohash_v1'],canidate["name"][0:20]) + dragnet(canidate['state'],canidate['ratio'],canidate["tags"],canidate['added_on'],self.age,self.t.time(),canidate['infohash_v1'],canidate["name"][0:20]) self.tl.info(f'["{canidate["name"][0:20]}..."] is orphaned.') self.up_tor_counter += 1 continue @@ -69,5 +69,5 @@ def isnonprotectedtor(setnonprotectedtag, tortags): if setnonprotectedtag in tortags: return True -def dragnet(self,state,ratio,tags,added,age,time,thash,tname): +def dragnet(state,ratio,tags,added,age,time,thash,tname): pass \ No newline at end of file diff --git a/test_qbitmaid.py b/test_qbitmaid.py index d47437c..1f993f2 100644 --- a/test_qbitmaid.py +++ b/test_qbitmaid.py @@ -1,5 +1,6 @@ import unittest -from qlist import ispreme,iscatignored,istrackerblank,isprotectedtracker +from qlist import ispreme,iscatignored,istrackerblank,isprotectedtracker,isnotprotectedtracker,istagblank +from qprocess import isdownloading,isprotectedunderratio,isoldtor,isprotectedoverratio,isnonprotectedtor class TestQbitmaid(unittest.TestCase): def test_ispreme_sanity(self): @@ -38,19 +39,59 @@ class TestQbitmaid(unittest.TestCase): pass def test_isnotprotectedtracker_sanity(self): - pass + self.assertFalse(isnotprotectedtracker('https://a.com/',['a.com','b.me','c.io'])) + self.assertTrue(isnotprotectedtracker('https://google.com/',['a.com','b.me','c.io'])) + self.assertTrue(isnotprotectedtracker('https://d.com',['a.com','b.me','c.io'])) def test_isnotprotectedtracker(self): pass def test_istagblank(self): + self.assertTrue(istagblank('')) + self.assertFalse(istagblank('a')) + self.assertFalse(istagblank(1)) + self.assertFalse(istagblank(1.0001)) + self.assertFalse(istagblank(False)) + self.assertFalse(istagblank(True)) + + def test_isdownloading_sanity(self): + self.assertTrue(isdownloading('downloading')) + + def test_isdownloading(self): + self.assertFalse(isdownloading('DOWNLOADING')) + self.assertFalse(isdownloading('dOwNlOaDiNg')) + + def test_isprotectedunderratio_sanity(self): + self.assertTrue(isprotectedunderratio(0.5,1,'a','a,b,c')) + + def test_isprotectedunderratio(self): pass - def test_(self): + def test_isoldtor_sanity(self): + self.assertTrue(isoldtor(1,2,4)) + + def test_isoldtor(self): pass - - def test_(self): + + def test_isprotectedoverratio_sanity(self): + self.assertTrue(isprotectedoverratio(2,1,'a','a,b,c')) + + def test_isprotectedoverratio(self): pass + + def test_isnonprotectedtor_sanity(self): + self.assertTrue(isnonprotectedtor('a','a,b,c')) + + def test_isnonprotectedtor(self): + pass + + pass + + # def test__sanity(self): + # pass + + # def test_(self): + # pass if __name__ == '__main__': unittest.main() \ No newline at end of file -- 2.45.2 From 81ad9e3d134fb34c6a9f23da02e53abba3e7cd56 Mon Sep 17 00:00:00 2001 From: jblu Date: Wed, 31 Aug 2022 10:56:37 -0500 Subject: [PATCH 3/3] added dragnet function --- .gitignore | 1 + qbit-maid.py | 3 ++- qprocess.py | 12 +++++++++--- test_qbitmaid.py | 2 -- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index af0d4f7..da47f49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.log *.json +*.csv # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/qbit-maid.py b/qbit-maid.py index 85ad5b5..f6c90c9 100644 --- a/qbit-maid.py +++ b/qbit-maid.py @@ -9,6 +9,7 @@ import time import datetime import logging from collections import Counter +import csv class Qbt: def __init__(self): @@ -30,6 +31,7 @@ class Qbt: self.tl = logging self.po = pushover self.ct = Counter + self.cv = csv # Variables torlog uses from config.json self.use_pushover = self.config["use_pushover"] self.use_log = self.config["use_log"] @@ -63,7 +65,6 @@ class Qbt: except qbittorrentapi.APIError as e: self.tl.exception(e) self.po.send_message(e, title="qbit-maid API ERROR") - # self.torrentlist = {} # Pulling all torrent data self.torrentlist = self.qbt_client.torrents_info() #Main process block diff --git a/qprocess.py b/qprocess.py index c148f36..95b08bc 100644 --- a/qprocess.py +++ b/qprocess.py @@ -32,7 +32,7 @@ def torprocessor(self): if self.use_log: self.tl.info(f'Submitted ["{canidate["name"][0:20]}..."] for deletion.') else: - dragnet(canidate['state'],canidate['ratio'],canidate["tags"],canidate['added_on'],self.age,self.t.time(),canidate['infohash_v1'],canidate["name"][0:20]) + dragnet(self,canidate['state'],canidate['ratio'],canidate["tags"],canidate['added_on'],self.age,self.t.time(),canidate['infohash_v1'],canidate["name"][0:20]) self.tl.info(f'["{canidate["name"][0:20]}..."] is orphaned.') self.up_tor_counter += 1 continue @@ -69,5 +69,11 @@ def isnonprotectedtor(setnonprotectedtag, tortags): if setnonprotectedtag in tortags: return True -def dragnet(state,ratio,tags,added,age,time,thash,tname): - pass \ No newline at end of file +def dragnet(self,state,ratio,tags,added,age,time,thash,tname): + header = ['state','ratio','tags','added','age','time','thash','tname'] + row = [state,ratio,tags,added,age,time,thash,tname] + with open('./orphanedtorrents.csv', 'w', encoding='UTF8', newline='') as f: + writer = self.cv.writer(f) + if f.tell() == 0: + writer.writerow(header) + writer.writerow(row) \ No newline at end of file diff --git a/test_qbitmaid.py b/test_qbitmaid.py index 1f993f2..230f397 100644 --- a/test_qbitmaid.py +++ b/test_qbitmaid.py @@ -85,8 +85,6 @@ class TestQbitmaid(unittest.TestCase): def test_isnonprotectedtor(self): pass - pass - # def test__sanity(self): # pass -- 2.45.2