-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_mylist_export.py
executable file
·245 lines (222 loc) · 8.85 KB
/
parse_mylist_export.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
#!/usr/bin/python3
import xmltodict
import sys
import anime
import configparser
import argparse
import queue
import threading
import datetime
THREADS = 2
def get_config(config_file: str = "weltschmerz.cfg"):
config = configparser.ConfigParser()
config.read_file(open(config_file))
parser = argparse.ArgumentParser(
description="Remove files that changed in size from database."
)
parser.add_argument(
"--database", help="database to use", default=config.get("client", "database")
)
parser.add_argument(
"--log-file",
dest="log_file",
help="logfile to use",
default=config.get("client", "log"),
)
parser.add_argument(
"--mylist-xml-files",
nargs="+",
help="mylist export: xml-plain-full anime files to process",
)
args = parser.parse_args()
return args
def xml_parser():
while True:
with open(q_in.get(), "r") as f:
anime_xml = f.read()
anime_data = xmltodict.parse(
anime_xml,
force_list=("episode", "file", "eprelation", "filerelation"),
)
q_out.put(anime_data)
q_in.task_done()
def try_parse_date(date_str: str, format_str: str):
try:
return datetime.datetime.strptime(date_str, format_str)
except ValueError:
return None
def not_empty(var):
if var != "-" or "":
return var
else:
return None
def dict_to_mylist_anime_worker():
while True:
anime_data = q_out.get()
aired = try_parse_date(anime_data["anime"]["startdate"]["long"], "%d.%m.%Y")
ended = try_parse_date(anime_data["anime"]["enddate"]["long"], "%d.%m.%Y")
anime_anime = anime.Anime(
aid=anime_data["anime"]["id"],
year=anime_data["anime"]["year"],
type=anime_data["anime"]["type"]["@id"],
eps=anime_data["anime"]["count"]["@eps"],
seps=anime_data["anime"]["count"]["@specials"],
airdate=aired,
enddate=ended,
rating=not_empty(anime_data["anime"]["rating"]),
votecount=not_empty(anime_data["anime"]["votes"]),
tempvote=not_empty(anime_data["anime"]["tmprating"]),
tempvcount=not_empty(anime_data["anime"]["tmpvotes"]),
avgreview=not_empty(anime_data["anime"]["reviewrating"]),
reviewcount=not_empty(anime_data["anime"]["reviews"]),
)
dbs.session.merge(anime_anime)
# load anime from db to get related episodes etc.
anime_anime = (
dbs.session.query(anime.Anime)
.filter(anime.Anime.aid == anime_data["anime"]["id"])
.one()
)
anime_mylist_anime = anime.MylistAnime(
aid=anime_data["anime"]["id"],
ml_count_episodes=anime_data["anime"]["mylist_entry"]["count"]["@eps"],
ml_count_specials=anime_data["anime"]["mylist_entry"]["count"]["@specials"],
ml_count_total=anime_data["anime"]["mylist_entry"]["count"]["@total"],
ml_watched_episodes=anime_data["anime"]["mylist_entry"]["watched"]["@eps"],
ml_watched_specials=anime_data["anime"]["mylist_entry"]["watched"][
"@specials"
],
ml_watched_total=anime_data["anime"]["mylist_entry"]["watched"]["@total"],
)
print(
f"anime: {anime_mylist_anime.aid} - eps: {anime_mylist_anime.ml_count_episodes} - watched: {anime_mylist_anime.ml_watched_episodes}"
)
dbs.session.merge(anime_mylist_anime)
# mylist export contains wishlisted anime,
# so anime may have no episodes/files in mylist
# resulting xml contains empty episodes element
if not anime_data["anime"]["episodes"]:
q_out.task_done()
continue
for episode in anime_data["anime"]["episodes"]["episode"]:
try:
aired = datetime.datetime.strptime(
episode["aired"]["#text"], "%d.%m.%Y %H:%M"
)
except ValueError:
aired = None
try:
episode_update = datetime.datetime.strptime(
episode["update"]["#text"], "%d.%m.%Y %H:%M"
)
except ValueError:
episode_update = None
anime_episode = anime.Episode(
eid=episode["@id"],
aid=anime_data["anime"]["id"],
ep=episode["@number"],
airdate=aired,
length=episode["length"],
title_en=episode["name"],
last_update=episode_update,
)
if "name_kanji" in episode.keys():
anime_episode.title_jp = episode["name_kanji"]
if "name_romaji" in episode.keys():
anime_episode.title_jp_t = episode["name_romaji"]
dbs.session.merge(anime_episode)
if dbs.session.dirty:
dbs.session.commit()
for file in episode["files"]["file"]:
# generic files are 0 bytes
# skip them for now
if int(file["size_plain"]) == 0:
continue
# files containing multiple episodes have episode relations, can't track those atm
# skip them for now, when the current episode is from an ep relation
if (
file["eprelations"]
and "eprelation" in file["eprelations"].keys()
and len(file["eprelations"]["eprelation"]) >= 1
):
if episode["@id"] in [
eprel["@eid"] for eprel in file["eprelations"]["eprelation"]
]:
continue
try:
file_update = datetime.datetime.strptime(
episode["update"]["#text"], "%d.%m.%Y %H:%M"
)
except ValueError:
file_update = None
anime_file = anime.File(
fid=file["@id"],
filesize=file["size_plain"],
aid=anime_data["anime"]["id"],
eid=episode["@id"],
gid=file["group"]["@id"],
source=file["source"],
extension=file["filetype"],
hash_crc=file["hash_crc"] or None,
hash_md5=file["hash_md5"] or None,
hash_sha1=file["hash_sha1"] or None,
hash_tth=file["hash_tth"] or None,
hash_ed2k=file["hash_ed2k"]["key"],
last_update=file_update,
)
dbs.session.merge(anime_file)
if dbs.session.dirty:
dbs.session.commit()
try:
view_date = datetime.datetime.strptime(
file["viewdate"]["long"], "%d.%m.%Y %H:%M"
)
except ValueError:
view_date = None
anime_mylist_file = anime.MylistFile(
fid=file["@id"],
ml_state=file["mystate"]["@id"],
ml_viewed=file["state"]["iswatched"],
ml_viewdate=view_date,
ml_storage=file["storage"],
ml_source=file["source"],
)
dbs.session.merge(anime_mylist_file)
if dbs.session.dirty:
dbs.session.commit()
file_screenshots = (
dbs.session.query(anime.TitleScreenShot)
.filter(anime.TitleScreenShot.fid == anime_file.fid)
.all()
)
for file_screenshot in file_screenshots:
if (
file_screenshot.aid != anime_file.aid
or file_screenshot.eid != anime_file.eid
):
file_screenshot.aid = anime_file.aid
file_screenshot.eid = anime_file.eid
dbs.session.merge(file_screenshot)
if dbs.session.dirty:
dbs.session.commit()
if dbs.session.dirty:
dbs.session.commit()
q_out.task_done()
if __name__ == "__main__":
config = get_config()
dbs = anime.DatabaseSession(config.database, False)
mylist_anime = []
workers = {}
q_in = queue.Queue()
q_out = queue.Queue()
for mylist_xml_file in config.mylist_xml_files:
q_in.put(mylist_xml_file)
for i in range(1, THREADS):
workers[i] = threading.Thread(target=xml_parser)
workers[i].daemon = True
workers[i].start()
db_worker = threading.Thread(target=dict_to_mylist_anime_worker)
db_worker.daemon = True
db_worker.start()
q_in.join()
q_out.join()