-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimeRepository.py
147 lines (125 loc) · 5.51 KB
/
AnimeRepository.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
import os
import json
from Anime import Anime
class AnimeRepository:
def __init__(self):
self.animes = {}
self.user_anime_list = None
def add(self, anime):
self.animes[anime.id] = anime
def get_all_animes(self):
return list(self.animes.values())
def get_anime_by_id(self, id):
try:
return self.animes[id]
except:
return None
def create_anime(self, info):
if self.get_anime_by_id(info['id']) is None:
new_anime = Anime(
id=info['id'],
title=info['title'],
main_picture=info.get('main_picture', None),
alternative_titles=info.get('alternative_titles', None),
start_date=info.get('start_date', None),
end_date=info.get('end_date', None),
synopsis=info.get('synopsis', None),
mean=info.get('mean', None),
rank=info.get('rank', None),
popularity=info.get('popularity', None),
num_list_users=info.get('num_list_users', None),
num_scoring_users=info.get('num_scoring_users', None),
nsfw=info.get('nsfw', None),
created_at=info.get('created_at', None),
updated_at=info.get('updated_at', None),
media_type=info.get('media_type', None),
status=info.get('status', None),
genres=info.get('genres', None),
my_list_status=info.get('my_list_status', None),
num_episodes=info.get('num_episodes', None),
start_season=info.get('start_season', None),
broadcast=info.get('broadcast', None),
source=info.get('source', None),
average_episode_duration=info.get('average_episode_duration', None),
rating=info.get('rating', None),
pictures=info.get('picture', None),
background=info.get('background', None),
related_anime=info.get('related_anime', []),
related_manga=info.get('related_manga', []),
recommendations=info.get('recommendations', []),
studios=info.get('studios', None),
statistics=info.get('statistics', None),
prequel=info.get('prequel', None),
sequel=info.get('sequel', None)
)
self.add(new_anime)
self.save_anime(new_anime)
def update_anime_list_status(self, all_anime):
new_animes = []
for anime in all_anime:
id = anime.get('node', {}).get('id', None)
list_status = anime.get('list_status', {})
if id is not None:
if self.get_anime_by_id(id) is not None:
self.animes[id].my_list_status = list_status # Update user list status for anime
else:
new_animes.append(id) # Append to list, to create entirely new anime object later
return new_animes
def save_anime(self, anime):
if not os.path.exists('animes'):
os.makedirs('animes')
anime_info = anime.to_dict()
file_path = os.path.join('animes', f'{anime.id}.json')
with open(file_path, 'w') as file:
json.dump(anime_info, file, indent=4)
def load_anime(self, anime_id):
file_path = os.path.join('animes', f'{anime_id}.json')
if os.path.exists(file_path):
with open(file_path, 'r') as file:
return json.load(file)
return None
def save_user_anime_list(self, all_anime): # actually: refresh user anime list
ids = []
for info in all_anime:
ids.append(info['node']['id'])
self.user_anime_list = ids
def get_prequel_sequel(self):
for anime in self.get_all_animes():
for related in anime.related_anime:
if related['relation_type'] == 'prequel' and anime.prequel is None:
anime.prequel = related['node']['id']
elif related['relation_type'] == 'sequel' and anime.sequel is None:
anime.sequel = related['node']['id']
if anime.prequel is None:
anime.prequel = False
if anime.sequel is None:
anime.sequel = False
def generate_anime_seasons_liniage(self):
def get_complete_lineage(anime_id):
if anime_id not in self.animes:
return []
anime = self.animes[anime_id]
lineage = []
# Traverse to the earliest prequel
while anime.prequel:
anime = self.animes.get(anime.prequel, None)
if not anime:
break
lineage.append(anime.id)
lineage.reverse() # Reverse to get chronological order
# Reset to original anime and traverse through sequels
anime = self.animes[anime_id]
lineage.append(anime.id)
while anime.sequel:
anime = self.animes.get(anime.sequel, None)
if not anime:
break
lineage.append(anime.id)
return lineage
anime_lineages = {}
for anime in self.get_all_animes():
if anime.prequel is False: # Starting points
lineage = get_complete_lineage(anime.id)
if len(lineage) > 0:
anime_lineages[anime.id] = lineage
return anime_lineages