-
Notifications
You must be signed in to change notification settings - Fork 0
/
animetitle.py
executable file
·104 lines (96 loc) · 3.66 KB
/
animetitle.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
#!/usr/bin/python3
# import anime titles from an AniDB title dump
# create filesystem hierarchy from AniDB title dump
# see https://wiki.anidb.net/API#Anime_Titles
# usage: animetitle.py <titledump> <target_folder>
import xml.etree.ElementTree as ET
import sys
import os.path
import os
import re
import html
FN_BLACKLIST = ["'", "\\", ":", '"', "*", "?"]
AID_PAD_WIDTH = 6
AID_CHUNK_SIZE = 2
def parse_file(filename):
anime_list = {}
tree = ET.parse(filename)
root = tree.getroot()
for anime in root:
if anime.attrib["aid"] not in anime_list:
anime_list[anime.attrib["aid"]] = []
for atitle in anime:
anime_list[anime.attrib["aid"]].append(
(
atitle.attrib["type"],
atitle.attrib["{http://www.w3.org/XML/1998/namespace}lang"],
html.unescape(atitle.text),
)
)
return anime_list
if __name__ == "__main__":
import sys
source_file = sys.argv[1]
target_path = sys.argv[2]
data = parse_file(source_file)
for aid in data:
aid_path = aid.zfill(AID_PAD_WIDTH)
aid_path = "/".join(
[
aid_path[i : i + AID_CHUNK_SIZE]
for i in range(0, AID_PAD_WIDTH, AID_CHUNK_SIZE)
]
)
main_title_lang = ""
print(aid_path)
if os.path.exists(os.path.join(target_path, "by-id", aid_path)):
for title in data[aid]:
if title[0] in ("main"):
main_title_lang = title[1]
title_sane = re.sub(
"\.$",
"。",
title[2].translate(
str.maketrans("/", "⁄", "".join(FN_BLACKLIST))
),
)
if not os.path.exists(
os.path.join(target_path, "by-name", title[0], title_sane)
):
os.symlink(
os.path.join("../..", "by-id", aid_path),
os.path.join(target_path, "by-name", title[0], title_sane),
)
for title in data[aid]:
if title[0] == "official" and main_title_lang == "".join(
("x-", title[1], "t")
):
title_sane = re.sub(
"\.$",
"。",
title[2].translate(
str.maketrans("/", "⁄", "".join(FN_BLACKLIST))
),
)
if not os.path.exists(
os.path.join(target_path, "by-name", title[0], title_sane)
):
os.symlink(
os.path.join("../..", "by-id", aid_path),
os.path.join(target_path, "by-name", title[0], title_sane),
)
if title[0] == "official" and title[1] == "en":
title_sane = re.sub(
"\.$",
"。",
title[2].translate(
str.maketrans("/", "⁄", "".join(FN_BLACKLIST))
),
)
if not os.path.exists(
os.path.join(target_path, "by-name", "english", title_sane)
):
os.symlink(
os.path.join("../..", "by-id", aid_path),
os.path.join(target_path, "by-name", "english", title_sane),
)