-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_utils.py
262 lines (217 loc) · 7.32 KB
/
update_utils.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
from datetime import date, datetime, timedelta
import os
import time
from . import utils
import json
import re
import threading
from sqlite3 import OperationalError
from .classes import Anime
class UpdateUtils:
def updateAll(self, schedule=True):
update_iter = self.updateAllProgression(schedule)
next(update_iter)
for t, txt in update_iter:
t.join()
def updateAllProgression(self, schedule=False):
def wrapper(f):
try:
f()
except OperationalError as e:
if e.args == ('database is locked',):
self.log("MAIN_STATE", "[ERROR] - On update function: Database is locked!")
else:
self.log("MAIN_STATE", "[ERROR] - On update function:", str(e))
raise
except Exception as e:
self.log("MAIN_STATE", "[ERROR] - On update function:", str(e))
raise
reloadFunc = {
self.updateCache: "Updating cache",
self.updateDirs: "Updating directories",
# self.updateTag: "Updating tags",
self.updateStatus: "Updating status",
# self.regroupFiles: "Regrouping files",
}
# It's better to get schedule after startup
# if schedule:
# reloadFunc = utils.dict_merge(reloadFunc, {
# self.getSchedule: "Updating schedule"
# })
yield len(reloadFunc)
for f, text in reloadFunc.items():
thread = threading.Thread(target=wrapper, args=(f,), daemon=True)
thread.start()
yield thread, text
thread.join()
def regroupFiles(self, silent=False):
# Not really needed anymore, also it's fucking slow
if not silent:
self.log("DB_UPDATE", "Regrouping files")
database = self.getDatabase()
files = []
for file in self.fm.list(self.animePath):
if self.fm.isfile(self.animePath + '/' + file):
files.append(file)
torrentDb = database.sql(
'SELECT id,title FROM anime WHERE id IN (SELECT id FROM torrents)',
to_dict=True)
for data in torrentDb:
anime = Anime(data)
path = self.getFolder(anime=anime)
if self.fm.isdir(path):
torrents = self.getTorrents(anime.id)
hashes = list(map(lambda t: t.hash, torrents))
self.tm.move(path=path, hashes=hashes)
if not silent:
self.log("DB_UPDATE", "Files regrouped!")
def updateCache(self):
c = 0
maxDate = timedelta(days=7)
for f in os.listdir(self.cache):
path = os.path.join(self.cache, f)
t = os.path.getmtime(path)
date = datetime.fromtimestamp(t)
delta = datetime.today() - date
if delta > maxDate:
c += 1
os.remove(path)
self.log("DB_UPDATE", "Updated cache, {} image{} deleted.".format(
c if c > 0 else "no", "s" if c >= 2 else ""))
def updateDirs(self):
def check_dir_empty(path):
if self.fm.isfile(path):
return False
files = self.fm.list(path)
if len(files) == 0:
return True
else:
return all(check_dir_empty(path + '/' + f) for f in files)
def remove_dir(path):
if self.fm.isfile(path):
return
files = self.fm.list(path)
if len(files) > 0:
for f in files:
remove_dir(path + '/' + f)
# os.rmdir(path)
self.fm.delete(path)
modified = False
pattern = re.compile(r"^.*? - (\d+)$")
for f in self.fm.list(self.animePath):
path = self.animePath + '/' + f
if self.fm.exists(path):
if check_dir_empty(path):
self.log("DB_UPDATE", os.path.normpath(path), 'is empty!')
remove_dir(path)
modified = True
match = re.findall(pattern, f)
if not match or not match[0]:
# TODO - Find corresponding torrent
pass
elif self.fm.exists(path):
pass
# TODO - Find corresponding anime and put in a directory
if not modified:
self.log("DB_UPDATE", "No empty directory to remove.")
def updateStatus(self):
self.log("DB_UPDATE", "Updating status") # TODO - Use procedures
return
statusUpdate = []
database = self.getDatabase()
with database.get_lock():
keys = database.keys(table="anime")
c = 0
anime_db = database.sql('SELECT * FROM anime WHERE status="UPCOMING" AND date_from is not null ORDER BY date_from ASC;') # , iterate=True)
for data in anime_db:
anime = Anime(keys=keys, values=data)
delta = datetime.now() - datetime.utcfromtimestamp(anime.date_from)
if delta >= timedelta(): # timedelta() == 0
statusUpdate.append(anime)
else:
# Animes are ordered by date_from ASC
break
status_dict = {}
for anime in statusUpdate:
old_status = anime.status
anime.status = None
status = self.getStatus(anime)
if status not in status_dict:
status_dict[status] = []
status_dict[status].append(anime.id)
c += 1
for status, ids in status_dict.items():
database.sql("UPDATE anime SET status=? WHERE id IN({});".format(",".join(map(str, ids))), [status], get_output=False)
database.save()
if c >= 1:
self.log('DB_UPDATE', "Status updated for {} animes".format(c))
else:
self.log('DB_UPDATE', "No status to update.")
def updateTag(self):
return # Useless with a server
self.log("DB_UPDATE", "Updating tags")
pattern = re.compile(r"^.*? - (\d+)$")
with self.database.get_lock():
toWatch = set()
toSeen = {data[0] for data in self.database.sql('SELECT anime_id FROM user_tags WHERE tag="WATCHING" AND anime_id IN (SELECT id FROM torrents)')}
for f in self.fm.list(self.animePath):
path = self.animePath + '/' + f
if self.fm.isdir(path):
match = re.findall(pattern, f)
if match and match[0]:
anime_id = int(match[0])
if anime_id in toSeen:
toSeen.remove(anime_id)
else:
toWatch.add(anime_id)
try:
if len(toWatch) >= 1:
self.log('DB_UPDATE', f'Updating {len(toSeen)} anime tags to Seen')
self.database.sql("UPDATE anime SET tag = 'WATCHING' WHERE id IN(" + ",".join("?" * len(toWatch)) + ");", toWatch)
if len(toSeen) >= 1:
self.log('DB_UPDATE', f'Updating {len(toWatch)} anime tags to Watching')
self.database.sql("UPDATE anime SET tag = 'SEEN' WHERE id IN(" + ",".join("?" * len(toSeen)) + ");", toSeen)
except OperationalError:
self.log('DB_UPDATE', 'Error while updating tags')
c = len(toSeen) + len(toWatch)
if c >= 1:
self.database.save()
self.log('DB_UPDATE', "{} tags updated!".format(c))
else:
self.log('DB_UPDATE', "No tags to update.")
def getSchedule(self, thread=False, force=False):
if thread is True:
threading.Thread(target=self.getSchedule, daemon=True).start()
return
# timer = utils.Timer("schedule")
now = time.time()
if force is False and now - self.lastSchedule < 3600: # 1 hours / 60*60
# Too fast, don't spam it
return
self.lastSchedule = now
self.setSettings({'lastSchedule': int(now)})
self.log('SCHEDULE', "Starting")
start = time.time()
data = self.api.schedule(limit=self.maxTrendingAnime)
queue = []
timeout = time.time() + self.scheduleTimeout
while not data.empty():
time_left = timeout - time.time()
if time_left < 0:
self.log('SCHEDULE', "Schedule took too long, interrupted")
break
anime = data.get(timeout=time_left)
if anime is None or len(anime) == 0:
continue
queue.append(anime)
database = self.getDatabase()
with database.get_lock():
ids = {a.id: a for a in queue}
found = database.sql(f"SELECT id FROM anime WHERE id IN ({','.join('?'*len(ids))})", list(ids.keys()))
for id, in found:
database.update(id, ids[id], table="anime")
del ids[id]
for id, a in ids.items():
database.insert(a, table="anime")
database.save()
self.log('SCHEDULE', f"Done: {round(time.time()-start, 3)} sec")