forked from GalaxyXL/qBittorrent-autoremove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoremove.py
146 lines (118 loc) · 4.93 KB
/
autoremove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: UTF-8 -*-
from qbittorrent import Client
import time
import requests
from time import sleep
import ConfigParser
import os
import sys
class my_qBittorrent(object):
def __init__(self,config):
self.config = config
username = self.config.get('global','webui_username')
password = self.config.get('global','webui_password')
webui_url = self.config.get('global','webui_url')
self.torrentHash = []
self.torrentData = []
self.client = Client(webui_url)
self.client.login(username, password)
self.getTorrentInfo()
self.getTorrentSeedTime()
def getTorrentInfo(self):
self.torrents = self.client.torrents(filter = 'completed')
for torrent in self.torrents:
self.torrentHash.append(torrent['hash'])
return
def getTorrentSeedTime(self):
for torrentHash in self.torrentHash:
torrentDict = {'hash':torrentHash,'info':self.client.get_torrent(torrentHash)}
self.torrentData.append(torrentDict)
return
def deleteTorrentPerm(self,torrentHash):
self.client.delete_permanently(torrentHash)
return
def getSingleTorrentInfo(self,torrentHash):
torrentDict = {'hash':torrentHash,'info':self.client.get_torrent(torrentHash)}
return torrentDict
def seedTimeFilter(self,torrentHash,seedTime=1):
seedTimeConv = seedTime*3600
torrentInfo = self.getSingleTorrentInfo(torrentHash)
seedingTime = torrentInfo['info']['seeding_time']
if seedingTime > seedTimeConv:
return True
return False
def trackerFilter(self,torrentHash,tracker = None):
#add the tracker exception
if tracker:
rawInfo = self.client.get_torrent_trackers(torrentHash)
torrentTracker = rawInfo[0]['url']
if tracker in torrentTracker:
return True
return False
def addedTimeFilter(self,torrentHash,addedTime = 1):
#default day
torrentInfo = self.getSingleTorrentInfo(torrentHash)
addedTimeConv = addedTime*24*3600
timeElapsed = torrentInfo['info']['time_elapsed']
if timeElapsed > addedTimeConv:
return True
return False
def integratedFilterAndExecute(self,torrentHash,tacker = None, lowerLimit = None):
seed_time = self.config.getint('filter','seeding_time')
if not lowerLimit:
if not self.trackerFilter(torrentHash, tracker = tacker):
if self.seedTimeFilter(torrentHash, seedTime = seed_time):
self.deleteTorrentPerm(torrentHash)
else:
if not self.trackerFilter(torrentHash, tracker = tacker):
if self.seedTimeFilter(torrentHash, seedTime = seed_time) and self.filterUploadSpeed(torrentHash, lowerLimit = lowerLimit):
self.deleteTorrentPerm(torrentHash)
def Traversal(self):
tracker = self.config.get('filter','exception_tracker')
lowerLimit = self.config.get('filter','upload_speed')
for torrentHash in self.torrentHash:
self.integratedFilterAndExecute(torrentHash,tracker,lowerLimit = lowerLimit)
def getUploadSpeed(self,torrentHash):
torrentInfo = self.getSingleTorrentInfo(torrentHash)
torrentUpSpeed = torrentInfo['up_speed']
return torrentUpSpeed
def filterUploadSpeed(self,torrentHash,lowerLimit = None):
torrentUpSpeed = self.getUploadSpeed(torrentHash)
if lowerLimit:
if torrentUpSpeed < lowerLimit:
return True
else:
return False
return False
def parserConfig(pathToConfig):
config_path = os.path.expanduser(pathToConfig)
config = ConfigParser.SafeConfigParser()
if os.path.isfile(config_path):
config.read(config_path)
else:
configDir = os.path.dirname(config_path)
if not os.path.isdir(configDir):
os.makedirs(configDir)
config.add_section('global')
config.set('global', 'webui_url', '')
config.set('global', 'webui_username', '')
config.set('global','webui_password','')
config.add_section('filter')
config.set('filter', 'seeding_time', '')
config.set('filter','exception_tracker','')
config.set('filter','upload_speed','')
with open(config_path,'w') as f:
config.write(f)
print('Please edit the configuration file: %s' % pathToConfig)
sys.exit(2)
return config
def main():
config = parserConfig('~/.qBautoremove/config')
while 1:
try:
newclient = my_qBittorrent(config)
newclient.Traversal()
except requests.exceptions.ConnectionError:
continue
time.sleep(300)
if __name__ == '__main__': main()