-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
anime_offline_database.py
86 lines (64 loc) · 2.27 KB
/
anime_offline_database.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
# Download the anime title list
import requests
import json
import os
import os.path
import time
import args
URL = "https://raw.githubusercontent.com/manami-project/anime-offline-database/master/anime-offline-database.json"
PATH = "./anime-offline-database-processed.json"
def update_id_database():
if can_download_database():
if args.parsed_args.verbose:
print("[anime_offline_database.py] [INFO] Updating cached anime-offline-database-processed.json")
response = requests.get(URL)
data = response.json()["data"]
id_database = []
if args.parsed_args.verbose:
print("[anime_offline_database.py] [INFO] Creating anime ID relations")
for anime in data:
relation = {
"anilist": None,
"anidb": None,
"myanimelist": None,
"kitsu": None
}
for source in anime["sources"]:
anime_id = source.split("/")[-1]
if "anilist.co" in source:
relation["anilist"] = int(anime_id)
if "anidb.net" in source:
relation["anidb"] = int(anime_id)
if "myanimelist.net" in source:
relation["myanimelist"] = int(anime_id)
if "kitsu.io" in source:
relation["kitsu"] = int(anime_id)
if all(value == None for value in relation.values()):
if args.parsed_args.verbose:
print(f"[anime_offline_database.py] [WARNING] No relations found for {anime['title']}")
continue
id_database.append(relation)
if args.parsed_args.verbose:
print("[anime_offline_database.py] [INFO] Saving processed relations")
local_database_file = open(PATH, "w")
local_database_file.seek(0)
json.dump(id_database, local_database_file, indent=4)
local_database_file.close()
else:
if args.parsed_args.verbose:
print("[anime_offline_database.py] [INFO] Using cached anime-offline-database-processed.json")
def can_download_database():
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
# AniDB only allows this file to be downloaded once per day
update_time = os.path.getmtime(PATH)
return ((time.time() - update_time) / 3600 > 24)
else:
return True
def convert_anime_id(anime_id, id_from, id_to):
local_database_file = open(PATH, "r")
local_database = json.load(local_database_file)
for entry in local_database:
if entry[id_from] == int(anime_id):
local_database_file.close()
return entry[id_to]
local_database_file.close()