qbit-maid/qprocess.py

83 lines
4.2 KiB
Python
Raw Normal View History

2022-09-02 18:28:03 -05:00
from cgitb import enable
2022-09-03 15:02:40 -05:00
def tor_processor(self):
2022-07-29 16:02:19 -05:00
"""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_list:
2022-08-26 17:28:47 -05:00
# if canidate['state'] == 'downloading':
2022-09-03 15:02:40 -05:00
if is_downloading(canidate['state']):
2022-07-29 16:02:19 -05:00
if self.use_log:
self.tl.info(f'["{canidate["name"][0:20]}..."] is still downloading and will be skipped.')
continue
2022-08-26 17:28:47 -05:00
# elif canidate['ratio'] < float(1.05) and self.tracker_protected_tag in canidate["tags"]:
2022-09-03 15:02:40 -05:00
elif is_protected_under_ratio(canidate['ratio'], 1.05, self.tracker_protected_tag, canidate["tags"]):
2022-07-29 16:02:19 -05:00
if self.use_log:
self.tl.debug(f'["{canidate["name"][0:20]}..."] is below a 1.05 ratio({canidate["ratio"]})')
2022-08-26 17:28:47 -05:00
# if canidate['added_on'] + self.age <= self.t.time():
2022-09-03 15:02:40 -05:00
if is_old_tor(canidate['added_on'], self.age, self.t.time()):
2022-07-29 16:02:19 -05:00
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.')
2022-08-26 17:28:47 -05:00
# elif canidate['ratio'] >= float(1.05) and self.tracker_protected_tag in canidate["tags"]:
2022-09-03 15:02:40 -05:00
elif is_protected_over_ratio(canidate['ratio'], 1.05, self.tracker_protected_tag, canidate["tags"]):
2022-07-29 16:02:19 -05:00
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.')
2022-08-26 17:28:47 -05:00
# elif self.tracker_non_protected_tag in canidate["tags"]:
2022-09-03 15:02:40 -05:00
elif is_not_protected_tor(self.tracker_non_protected_tag, canidate["tags"]):
2022-07-29 16:02:19 -05:00
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:
2022-09-02 18:28:03 -05:00
if self.enable_dragnet:
dragnet(self,canidate['state'],canidate['ratio'],canidate["tags"],canidate['added_on'],self.age,self.t.time(),canidate['infohash_v1'],canidate["name"][0:20])
2022-07-29 16:02:19 -05:00
self.tl.info(f'["{canidate["name"][0:20]}..."] is orphaned.')
self.up_tor_counter += 1
continue
2022-09-03 15:02:40 -05:00
def tor_delete_tags(self):
2022-07-29 16:02:19 -05:00
tag_list = [self.tracker_protected_tag, self.tracker_non_protected_tag]
self.qbt_client.torrents_delete_tags(tag_list)
2022-09-03 15:02:40 -05:00
def tor_delete(self):
2022-07-29 16:02:19 -05:00
"""Remove torrents, will also delete files, this keeps the filesystem clean.
Only pass self.torrent_hash_delete_list if you would like to keep the files."""
if self.use_log:
self.tl.debug('Hash list submitted for deletion:')
self.tl.debug(self.torrent_hash_delete_list)
2022-08-26 17:28:47 -05:00
self.qbt_client.torrents_delete(True, self.torrent_hash_delete_list)
2022-09-03 15:02:40 -05:00
def is_downloading(state):
2022-08-26 17:28:47 -05:00
if state == 'downloading':
return True
2022-09-03 15:02:40 -05:00
def is_protected_under_ratio(torratio, setratio, settag, tortag):
2022-08-26 17:28:47 -05:00
if torratio < float(setratio) and settag in tortag:
return True
2022-09-03 15:02:40 -05:00
def is_old_tor(toradd, setage, currenttime):
2022-08-26 17:28:47 -05:00
if toradd + setage <= currenttime:
return True
2022-09-03 15:02:40 -05:00
def is_protected_over_ratio(torratio, setratio, settag, tortag):
2022-08-26 17:28:47 -05:00
if torratio >= float(setratio) and settag in tortag:
return True
2022-09-03 15:02:40 -05:00
def is_not_protected_tor(setnonprotectedtag, tortags):
2022-08-26 17:28:47 -05:00
if setnonprotectedtag in tortags:
return True
2022-08-31 10:56:37 -05:00
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]
2022-09-02 18:28:03 -05:00
with open(self.dragnet_outfile, 'w', encoding='UTF8', newline='') as f:
2022-08-31 10:56:37 -05:00
writer = self.cv.writer(f)
if f.tell() == 0:
writer.writerow(header)
writer.writerow(row)