-
Notifications
You must be signed in to change notification settings - Fork 3
/
checker.py
274 lines (205 loc) · 8.35 KB
/
checker.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
import logging
from collections import deque
try:
from imp import reload
except Exception:
from importlib import reload
import config as cfg
from deleter import Deleter
from messenger import message
from utils import convertRules
from remote_caller import SCGIRequest
include = "include"
exclude = "exclude"
whitelist = "whitelist"
blacklist = "blacklist"
class Checker(SCGIRequest):
def __init__(self, cache):
super().__init__()
self.cache = cache
deleter = Deleter(self.cache)
self.delete = deleter.deletions
self.mountPoints = self.cache.mountPoints
self.pendingDeletions = self.cache.pendingDeletions
self.torrentsDownloading = self.cache.torrentsDownloading
self.cfgGeneralRules = self.cfgLabelRules = self.cfgTrackerRules = self.lastHash = None
self.lastModified = 0
def check(self, torrentData):
torrentName, torrentHash, torrentPath, torrentSize = torrentData[1:]
parentDirectory = torrentPath.rsplit("/", 1)[0] if torrentName in torrentPath else torrentPath
torrentSize = int(torrentSize) / 1073741824.0
lastModified = os.path.getmtime("config.py")
try:
mountPoint = self.mountPoints[parentDirectory]
except Exception:
mountPoint = [path for path in [parentDirectory.rsplit("/", n)[0] for n in range(parentDirectory.count("/"))] if os.path.ismount(path)]
mountPoint = mountPoint[0] if mountPoint else "/"
self.mountPoints[parentDirectory] = mountPoint
if lastModified != self.lastModified:
try:
reload(cfg)
except Exception as e:
self.cache.lock = False
logging.critical("checker.py: {}: Config Error: Couldn't update config settings: {}".format(torrentName, e))
return
if self.cfgLabelRules != cfg.label_rules:
self.cfgLabelRules = cfg.label_rules
self.labelRules, self.trackers = {}, {}
convertRules(cfg.label_rules, self.labelRules)
if self.cfgTrackerRules != cfg.tracker_rules:
self.cfgTrackerRules = cfg.tracker_rules
self.trackerRules, self.trackers = {}, {}
convertRules(cfg.tracker_rules, self.trackerRules)
if self.cfgGeneralRules != cfg.general_rules:
self.cfgGeneralRules = cfg.general_rules
self.requirements = (
cfg.general_rules["age"] if "age" in cfg.general_rules else False,
cfg.general_rules["ratio"] if "ratio" in cfg.general_rules else False,
cfg.general_rules["seeds"] if "seeds" in cfg.general_rules else False,
cfg.general_rules["size"] if "size" in cfg.general_rules else False,
cfg.general_rules["fb_mode"] if "fb_mode" in cfg.general_rules else False,
cfg.general_rules["fb_age"] if "fb_age" in cfg.general_rules else False,
cfg.general_rules["fb_ratio"] if "fb_ratio" in cfg.general_rules else False,
cfg.general_rules["fb_seeds"] if "fb_seeds" in cfg.general_rules else False,
cfg.general_rules["fb_size"] if "fb_size" in cfg.general_rules else False,
)
self.lastModified = lastModified
if not cfg.enable_cache and self.cache.refreshTorrents():
self.cache.lock = False
logging.critical("checker.py: {}: XMLRPC Error: Couldn't retrieve torrents".format(torrentName))
return
try:
completedTorrents = self.cache.torrents[mountPoint]
completedTorrentsCopy = deque(completedTorrents)
except Exception:
completedTorrentsCopy = None
try:
downloading = self.torrentsDownloading[mountPoint]
if downloading:
try:
torrentsDownloading = self.send("d.multicall2", ("", "leeching", "d.left_bytes=", "d.hash=", "d.state="))
torrentsDownloading = sum([tBytes for tBytes, tHash, tState in torrentsDownloading if tState and tHash in downloading])
except Exception as e:
self.cache.lock = False
logging.critical("checker.py: {}: XMLRPC Error: Couldn't retrieve torrents: {}".format(torrentName, e))
return
else:
torrentsDownloading = 0
downloading.append(torrentHash)
except Exception:
self.torrentsDownloading[mountPoint] = [torrentHash]
torrentsDownloading = 0
try:
pendingDeletions = self.pendingDeletions[mountPoint]
except Exception:
pendingDeletions = self.pendingDeletions[mountPoint] = 0
disk = os.statvfs(mountPoint)
availableSpace = (disk.f_bsize * disk.f_bavail + pendingDeletions - torrentsDownloading) / 1073741824.0
minimumSpace = cfg.minimum_space_mp[mountPoint] if mountPoint in cfg.minimum_space_mp else cfg.minimum_space
requiredSpace = torrentSize - (availableSpace - minimumSpace)
freedSpace = 0
override = True
labelMatch = False
fallbackTorrents = deque()
while freedSpace < requiredSpace and (completedTorrentsCopy or fallbackTorrents):
if completedTorrentsCopy:
torrent = completedTorrentsCopy.popleft()
tHash, tLabel, tTracker, tAge, tRatio, tSeeds, tSizeBytes, tSizeGigabytes = torrent
if cfg.exclude_unlabelled and not tLabel:
continue
if override:
override = False
minAge, minRatio, minSeeds, minSize, fbMode, fbAge, fbRatio, fbSeeds, fbSize = self.requirements
if self.labelRules:
if tLabel in self.labelRules:
labelRule = self.labelRules[tLabel]
if labelRule is exclude:
continue
if labelRule is not include:
if blacklist in labelRule:
try:
tracker = self.trackers[tLabel + tTracker[0][0]]
except Exception:
tracker = [tracker for tracker in labelRule[-1] for url in tTracker if tracker in url[0]]
for url in tTracker:
self.trackers[tLabel + url[0]] = tracker
if tracker:
continue
elif whitelist in labelRule:
try:
tracker = self.trackers[tLabel + tTracker[0][0]]
except Exception:
tracker = [tracker for tracker in labelRule[-1] for url in tTracker if tracker in url[0]]
for url in tTracker:
self.trackers[tLabel + url[0]] = tracker
if not tracker:
continue
if include not in labelRule:
override = True
minAge, minRatio, minSeeds, minSize, fbMode, fbAge, fbRatio, fbSeeds, fbSize = labelRule[:9]
labelMatch = True
elif cfg.labels_only:
continue
else:
labelMatch = False
if self.trackerRules and not override:
try:
tracker = self.trackers[tTracker[0][0]]
except Exception:
tracker = [tracker for tracker in self.trackerRules for url in tTracker if tracker in url[0]]
for url in tTracker:
self.trackers[url[0]] = tracker
if tracker:
trackerRule = self.trackerRules[tracker[0]]
if trackerRule is exclude:
continue
if trackerRule is not include:
override = True
minAge, minRatio, minSeeds, minSize, fbMode, fbAge, fbRatio, fbSeeds, fbSize = trackerRule[:9]
elif cfg.trackers_only or (cfg.labels_and_trackers_only and not labelMatch):
continue
if tAge < minAge or tRatio < minRatio or tSeeds < minSeeds or tSizeGigabytes < minSize:
if fbMode == 1:
if tAge >= fbAge and tRatio >= fbRatio and tSeeds >= fbSeeds and tSizeGigabytes >= fbSize:
fallbackTorrents.append(torrent)
elif fbMode == 2:
if (
(fbAge is not False and tAge >= fbAge)
or (fbRatio is not False and tRatio >= fbRatio)
or (fbSeeds is not False and tSeeds >= fbSeeds)
or (fbSize is not False and tSizeGigabytes >= fbSize)
):
fallbackTorrents.append(torrent)
continue
else:
torrent = fallbackTorrents.popleft()
tHash, tLabel, tTracker, tAge, tRatio, tSeeds, tSizeBytes, tSizeGigabytes = torrent
try:
completedTorrents.remove(torrent)
except Exception:
continue
self.delete.append((tHash, mountPoint, tSizeBytes))
self.pendingDeletions[mountPoint] += tSizeBytes
freedSpace += tSizeGigabytes
if freedSpace >= requiredSpace:
self.cache.lock = False
try:
self.send("d.start", (torrentHash,))
except Exception as e:
logging.error("checker.py: {}: XMLRPC Error: Couldn't start torrent: {}".format(torrentName, e))
else:
if cfg.repeat_check and self.lastHash != torrentHash:
self.lastHash = torrentHash
self.torrentsDownloading[mountPoint].remove(torrentHash)
self.cache.repeat = True
self.cache.refreshTorrents()
self.cache.repeat = False
self.check(torrentData)
return
self.cache.lock = False
if cfg.enable_email or cfg.enable_pushbullet or cfg.enable_pushover or cfg.enable_telegram or cfg.enable_discord or cfg.enable_slack:
try:
message()
except Exception as e:
logging.error("checker.py: Message Error: Couldn't send message: " + str(e))