-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcacher.py
238 lines (183 loc) · 6.62 KB
/
cacher.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
import os
import time
import logging
from threading import Thread
from datetime import datetime
try:
from imp import reload
except Exception:
from importlib import reload
import config as cfg
from utils import sortTorrents
from remote_caller import SCGIRequest
class Cache(SCGIRequest):
def __init__(self):
super().__init__()
self.deletions, self.pending = [], []
self.torrents, self.mountPoints, self.torrentsDownloading, self.pendingDeletions = {}, {}, {}, {}
self.lock = self.refreshing = self.repeat = self.sortOrder = self.groupOrder = False
self.lastModified = 0
self.getMountPoints()
self.refreshTorrents()
Thread(target=self.removeTorrents).start()
Thread(target=self.configMonitor).start()
Thread(target=self.getTorrents).start()
def configMonitor(self):
while True:
self.reloadConfig(True)
time.sleep(1)
def reloadConfig(self, monitor):
lastModified = os.path.getmtime("config.py")
if lastModified != self.lastModified:
self.lastModified = lastModified
try:
reload(cfg)
except Exception as e:
logging.error("cacher.py: Config Error: Couldn't update config settings: " + str(e))
return
if self.sortOrder != cfg.sort_order or self.groupOrder != cfg.group_order:
self.sortOrder = cfg.sort_order
self.groupOrder = cfg.group_order
if monitor:
self.refreshTorrents()
def removeTorrents(self):
self.deletedTorrents = []
while True:
while self.deletedTorrents:
torrentHash, torrentName, torrentPath = self.deletedTorrents.pop(0)[2:]
parentDirectory = torrentPath.rsplit("/", 1)[0] if torrentName in torrentPath else torrentPath
mountPoint = self.mountPoints[parentDirectory] if parentDirectory in self.mountPoints else self.getMountPoint(parentDirectory)
try:
self.torrents[mountPoint].remove(self.torrentHashes[torrentHash])
except Exception:
pass
Thread(target=self.removeTorrent, args=(torrentHash, mountPoint)).start()
time.sleep(0.01)
def removeTorrent(self, torrentHash, mountPoint):
while self.refreshing:
time.sleep(0.01)
try:
self.torrents[mountPoint].remove(self.torrentHashes[torrentHash])
except Exception:
pass
def updatePending(self, torrentData):
while self.refreshing:
time.sleep(0.01)
try:
self.pending.remove(torrentData)
except Exception:
pass
def getTorrents(self):
while True:
while cfg.enable_cache:
while self.lock or self.deletions or self.pending:
time.sleep(60)
self.refreshTorrents()
time.sleep(cfg.cache_interval)
time.sleep(1)
def refreshTorrents(self):
self.reloadConfig(False)
while self.deletions or self.pending:
time.sleep(0.01)
self.refreshing = True
try:
completedTorrents = self.send(
"d.multicall2",
(
"",
"complete",
"d.hash=",
"d.name=",
"d.directory=",
"d.custom1=",
"t.multicall=,t.url=",
"d.timestamp.finished=",
"d.ratio=",
"t.multicall=,t.url=,t.scrape_complete=",
"d.size_bytes=",
),
)
torrentsDownloading = self.send("d.multicall2", ("", "leeching", "d.hash="))
stoppedTorrents = self.send("d.multicall2", ("", "stopped", "d.hash=", "d.complete="))
except Exception:
self.refreshing = False
return True
completedTorrents = [
(
tPath.rsplit("/", 1)[0] if tName in tPath else tPath,
tHash,
tLabel,
tTracker,
(datetime.now() - datetime.utcfromtimestamp(tAge)).days,
tRatio / 1000.0,
max([seeds[1] for seeds in tSeeds]),
tSize,
tSize / 1073741824.0,
)
for tHash, tName, tPath, tLabel, tTracker, tAge, tRatio, tSeeds, tSize in completedTorrents
]
sortedTorrents = sortTorrents(cfg.sort_order, cfg.group_order, completedTorrents)
torrents, torrentHashes = {}, {}
for torrentData in sortedTorrents:
parentDirectory, torrentHash = torrentData[:2]
torrentData = torrentData[1:]
torrentHashes[torrentHash] = torrentData
mountPoint = self.mountPoints[parentDirectory] if parentDirectory in self.mountPoints else self.getMountPoint(parentDirectory)
if mountPoint in torrents:
torrents[mountPoint].append(torrentData)
else:
torrents[mountPoint] = [torrentData]
while (self.lock and cfg.enable_cache and not self.repeat) or self.deletions or self.pending:
while self.deletions:
try:
torrentHash, mountPoint, torrentSize = self.deletions[0]
torrents[mountPoint].remove(torrentHashes[torrentHash])
except Exception:
pass
while self.pending:
try:
torrentHash, mountPoint, torrentSize = self.pending.pop(0)
torrents[mountPoint].remove(torrentHashes[torrentHash])
except Exception:
pass
time.sleep(0.01)
self.torrents = torrents
self.torrentHashes = torrentHashes
self.refreshing = False
torrentsDownloading = [tHash[0] for tHash in torrentsDownloading] + [tHash for tHash, complete in stoppedTorrents if not complete]
[tHashes.remove(tHash) for tHashes in self.torrentsDownloading.values() for tHash in tHashes[:] if tHash not in torrentsDownloading]
def getMountPoint(self, parentDirectory):
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
return mountPoint
def getMountPoints(self):
while True:
try:
completedTorrents = self.send("d.multicall2", ("", "complete", "d.name=", "d.directory="))
torrentsDownloading = self.send("d.multicall2", ("", "leeching", "d.hash=", "d.name=", "d.directory="))
stoppedTorrents = self.send("d.multicall2", ("", "stopped", "d.complete=", "d.hash=", "d.name=", "d.directory="))
break
except Exception:
time.sleep(1)
incompleteTorrents = [
(torrentHash, torrentPath.rsplit("/", 1)[0] if torrentName in torrentPath else torrentPath)
for torrentHash, torrentName, torrentPath in torrentsDownloading
] + [
(torrentHash, torrentPath.rsplit("/", 1)[0] if torrentName in torrentPath else torrentPath)
for complete, torrentHash, torrentName, torrentPath in stoppedTorrents
if not complete
]
for torrentName, torrentPath in completedTorrents:
parentDirectory = torrentPath.rsplit("/", 1)[0] if torrentName in torrentPath else torrentPath
if parentDirectory not in self.mountPoints:
self.getMountPoint(parentDirectory)
for torrentHash, parentDirectory in incompleteTorrents:
if parentDirectory not in self.mountPoints:
mountPoint = self.getMountPoint(parentDirectory)
else:
mountPoint = self.mountPoints[parentDirectory]
try:
self.torrentsDownloading[mountPoint].append(torrentHash)
except Exception:
self.torrentsDownloading[mountPoint] = [torrentHash]