-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanager.py
executable file
·232 lines (194 loc) · 7.15 KB
/
manager.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
import json
import os
cwd = os.path.dirname(os.path.abspath(__file__))
if not os.path.exists("json_data"):
os.mkdir(os.path.join(cwd, "json_data"))
DATABASE_FILE = os.path.join(cwd, "json_data/animerecord.json")
def ensure_file_exists():
"""
Ensure the database file exists. If not, create an empty JSON file.
"""
if not os.path.isfile(DATABASE_FILE):
with open(DATABASE_FILE, 'w') as f:
json.dump({}, f)
def load_database():
"""
Load the database from the JSON file.
"""
ensure_file_exists() # Ensure the file exists before loading
with open(DATABASE_FILE, 'r') as f:
return json.load(f)
def save_database(data):
"""
Save the provided data to the JSON database file.
"""
with open(DATABASE_FILE, 'w') as f:
json.dump(data, f, indent=4) # Pretty-print the JSON with an indent of 4 spaces
def get_next_index(database):
"""
Get the next available index for a new record.
"""
# Find the maximum index from existing keys or default to 0 if the database is empty
return max([int(key) for key in database.keys()] or [0]) + 1
def update_entry(record, database=None):
"""
Update an existing record in the database.
"""
if database is None:
database = load_database() # Load the current database only if not passed as an argument
title = record[1].get('title')
# Find the index of the existing record by matching the title
existing_index = next((index for index, data in database.items() if data["title"] == title), None)
if existing_index is None:
print(f"No existing record found for title '{title}'. Adding as new record.")
add_new_record(record, database) # If no existing record, add it as a new record
return
# Extract record details
keyword = record[0]
anime_type = record[1].get('type')
max_episode = record[1].get('episodes')
year = record[1].get('year')
cover = record[1].get('poster')
changes = []
for k,v in database.items():
if database[k]['title'] == title :
changes.append(database[k]['about'])
changes.append(database[k]['current_episode'])
about = changes[0]
current_episode = changes[1]
if isinstance(current_episode, str):
current_episode = 0
if len(record) == 3:
if type(record[2]) == int:
current_episode = record[2] if current_episode < record[2] else current_episode
else:
if ',' in record[2] and len(record[2]) < 30:
latest_episode = max(list(int(s) for s in record[2].split(',')))
current_episode = latest_episode if current_episode < latest_episode else current_episode
else:
about = record[2]
elif len(record) == 4:
about = record[2]
current_episode_info = record[3]
latest_episode = max(list(int(s) for s in current_episode_info.split(',')))
current_episode = latest_episode if current_episode < latest_episode else current_episode
# Determine the status based on the current episode
status = "Not Started Watching"
try:
if current_episode > 0:
if current_episode < max_episode:
status = f"Watching Episode {current_episode}"
else:
status = "Completed"
except TypeError:
print(f"\n{current_episode} is currently str,'>' not supported between instances of 'str' and 'int'")
pass
# Update the existing record with new details
database[existing_index] = {
"title": title,
"keyword": keyword,
"type": anime_type,
"cover_photo": cover,
"rating": 0,
"status": status,
"current_episode": current_episode,
"max_episode": max_episode,
"year_aired": year,
"about": about
}
save_database(database) # Save the updated database
def add_new_record(record, database):
"""
Add a new record to the database.
"""
next_index = get_next_index(database)
# Extract record details
keyword = record[0]
title = record[1].get('title')
anime_type = record[1].get('type')
max_episode = record[1].get('episodes')
year = record[1].get('year')
cover = record[1].get('poster')
about = ""
current_episode = 0
if len(record) == 3:
if ',' in record[2] and all(part.isdigit() for part in record[2].split(',')):
current_episode = int(record[2].split(',')[-1])
else:
about = record[2]
elif len(record) == 4:
about = record[2]
current_episode_info = record[3]
current_episode = int(max(list(record[3])))
status = "Not Started Watching"
if current_episode > 0:
if current_episode < max_episode:
status = f"Watching Episode {current_episode}"
else:
status = "Completed"
database[next_index] = {
"title": title,
"keyword": keyword,
"type": anime_type,
"cover_photo": cover,
"rating": 0,
"status": status,
"current_episode": current_episode,
"max_episode": max_episode,
"year_aired": year,
"about": about
}
save_database(database)
def process_record(record, update=False):
"""
Process and add a new record to the database. If the record exists, update it if `update` is True.
"""
database = load_database()
title = record[1].get('title')
# Check if the record already exists in the database by title
existing_index = next((index for index, data in database.items() if data["title"] == title), None)
if existing_index is not None:
if update:
print(f"Record with title '{title}' already exists. Updating it.")
update_entry(record, database)
else:
print(f"Record with title '{title}' already exists. No action taken.")
else:
print(f"Adding new record with title '{title}'.")
add_new_record(record, database)
def search_record(query):
"""
Search for records in the database that match the query.
"""
database = load_database()
results = {}
for key, value in database.items():
lower_query = query.lower()
if lower_query in value["title"].lower() or lower_query in value["keyword"].lower():
results[key] = value
return results
def print_all_records():
"""
Print all records from the database in a formatted JSON.
"""
database = load_database()
print(json.dumps(database, indent=4))
# Example usage
sample = [
'kabaneri',
{
'id': 875,
'title': 'Koutetsujou no Kabaneri',
'type': 'TV',
'episodes': 12,
'status': 'Finished Airing',
'season': 'Spring',
'year': 2016,
'score': 7.27,
'poster': 'https://i.animepahe.ru/posters/3c01c83a35626201293b677d166226fcef7e13b00b875991907f1a54aebad626.jpg',
'session': 'fccced41-eb03-ea7c-ceaf-13b40bad9cd3'
},
'The world is in the midst of the industrial revolution when horrific creatures emerge from a mysterious virus...'
]
if __name__ == '__main__':
process_record(sample, update=True)