| @@ -13,19 +13,627 @@ import json |
| 13 | import random |
13 | import random |
| 14 | import subprocess |
14 | import subprocess |
| 15 | import time |
15 | import time |
| |
16 | import curses |
| 16 | import argparse |
17 | import argparse |
| 17 | |
18 | import requests |
| 18 | from anilist import get_anime_image, search_anime_by_title |
19 | import csv |
| 19 | from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list |
20 | import socket |
| 20 | from select_link import load_links |
| |
| 21 | from start_video import get_mpv_paused_status, start_video, send_command, get_percentage_watched, percentage_watched, get_mpv_playback_speed |
| |
| 22 | from select_anime import load_anime_data |
| |
| 23 | from select_anime import extract_anime_info |
| |
| 24 | from select_anime import select_anime |
| |
| 25 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime |
| |
| 26 | |
21 | |
| 27 | discord_client_id = "1287457464148820089" |
22 | discord_client_id = "1287457464148820089" |
| 28 | |
23 | |
| |
24 | # ----------------------------------------------- AniList Functions ---------------------------------------- |
| |
25 | |
| |
26 | def search_anime_anilist(query, token): |
| |
27 | url = "https://graphql.anilist.co" |
| |
28 | |
| |
29 | query_string = ''' |
| |
30 | query ($search: String) { |
| |
31 | Page(page: 1, perPage: 10) { |
| |
32 | media(search: $search, type: ANIME) { |
| |
33 | id |
| |
34 | title { |
| |
35 | romaji |
| |
36 | english |
| |
37 | native |
| |
38 | } |
| |
39 | } |
| |
40 | } |
| |
41 | } |
| |
42 | ''' |
| |
43 | |
| |
44 | variables = { |
| |
45 | 'search': query |
| |
46 | } |
| |
47 | |
| |
48 | headers = { |
| |
49 | 'Authorization': f'Bearer {token}', |
| |
50 | 'Content-Type': 'application/json' |
| |
51 | } |
| |
52 | |
| |
53 | response = requests.post(url, json={'query': query_string, 'variables': variables}, headers=headers) |
| |
54 | |
| |
55 | if response.status_code == 200: |
| |
56 | anime_list = response.json()['data']['Page']['media'] |
| |
57 | anime_dict = {} |
| |
58 | for anime in anime_list: |
| |
59 | if anime['title']['english']==None: |
| |
60 | anime_dict[anime['title']['romaji']] = anime['id'] |
| |
61 | else: |
| |
62 | anime_dict[anime['title']['english']] = anime['id'] |
| |
63 | |
| |
64 | return anime_dict |
| |
65 | else: |
| |
66 | print(f"Failed to search for anime. Status Code: {response.status_code}, Response: {response.text}") |
| |
67 | return {} |
| |
68 | |
| |
69 | def get_anilist_user_id(token): |
| |
70 | url = "https://graphql.anilist.co" |
| |
71 | query = ''' |
| |
72 | query { |
| |
73 | Viewer { |
| |
74 | id |
| |
75 | name |
| |
76 | } |
| |
77 | } |
| |
78 | ''' |
| |
79 | |
| |
80 | headers = { |
| |
81 | "Authorization": f"Bearer {token}", |
| |
82 | "Content-Type": "application/json", |
| |
83 | "Accept": "application/json" |
| |
84 | } |
| |
85 | |
| |
86 | response = requests.post(url, json={"query": query}, headers=headers) |
| |
87 | |
| |
88 | if response.status_code == 200: |
| |
89 | data = response.json() |
| |
90 | user_id = data['data']['Viewer']['id'] |
| |
91 | user_name = data['data']['Viewer']['name'] |
| |
92 | return user_id, user_name |
| |
93 | else: |
| |
94 | raise Exception(f"Error: {response.status_code}, {response.text}") |
| |
95 | |
| |
96 | def add_anime_to_watching_list(anime_id: int, token: str): |
| |
97 | url = "https://graphql.anilist.co" |
| |
98 | |
| |
99 | mutation = ''' |
| |
100 | mutation ($mediaId: Int) { |
| |
101 | SaveMediaListEntry (mediaId: $mediaId, status: CURRENT) { |
| |
102 | id |
| |
103 | status |
| |
104 | } |
| |
105 | } |
| |
106 | ''' |
| |
107 | |
| |
108 | variables = { |
| |
109 | 'mediaId': anime_id |
| |
110 | } |
| |
111 | |
| |
112 | headers = { |
| |
113 | 'Authorization': f'Bearer {token}', |
| |
114 | 'Content-Type': 'application/json' |
| |
115 | } |
| |
116 | |
| |
117 | response = requests.post(url, json={'query': mutation, 'variables': variables}, headers=headers) |
| |
118 | |
| |
119 | if response.status_code == 200: |
| |
120 | print(f"Anime with ID {anime_id} has been added to your watching list.") |
| |
121 | else: |
| |
122 | print(f"Failed to add anime. Status Code: {response.status_code}, Response: {response.text}") |
| |
123 | |
| |
124 | def get_anime_image(anilist_media_id): |
| |
125 | url = "https://graphql.anilist.co" |
| |
126 | query = ''' |
| |
127 | query ($id: Int) { |
| |
128 | Media(id: $id) { |
| |
129 | coverImage { |
| |
130 | large |
| |
131 | } |
| |
132 | } |
| |
133 | } |
| |
134 | ''' |
| |
135 | variables = { |
| |
136 | 'id': anilist_media_id |
| |
137 | } |
| |
138 | |
| |
139 | # Make the API request |
| |
140 | response = requests.post(url, json={'query': query, 'variables': variables}) |
| |
141 | |
| |
142 | if response.status_code == 200: |
| |
143 | data = response.json() |
| |
144 | image_url = data['data']['Media']['coverImage']['large'] |
| |
145 | return image_url |
| |
146 | else: |
| |
147 | raise Exception(f"Failed to retrieve data from AniList API. Status Code: {response.status_code}") |
| |
148 | |
| |
149 | def get_user_data(access_token, user_id): |
| |
150 | query = """ |
| |
151 | { |
| |
152 | MediaListCollection(userId: %s, type: ANIME) { |
| |
153 | lists { |
| |
154 | entries { |
| |
155 | media { |
| |
156 | id |
| |
157 | episodes |
| |
158 | duration |
| |
159 | title { |
| |
160 | romaji |
| |
161 | english |
| |
162 | } |
| |
163 | } |
| |
164 | status |
| |
165 | score |
| |
166 | progress |
| |
167 | } |
| |
168 | } |
| |
169 | } |
| |
170 | } |
| |
171 | """ % user_id |
| |
172 | |
| |
173 | # Send the request |
| |
174 | response = requests.post( |
| |
175 | 'https://graphql.anilist.co', |
| |
176 | json={'query': query}, |
| |
177 | headers={'Authorization': f'Bearer {access_token}'} |
| |
178 | ) |
| |
179 | |
| |
180 | |
| |
181 | # Print the response |
| |
182 | return response.json() |
| |
183 | |
| |
184 | def load_json_file(file_path): |
| |
185 | with open(file_path, 'r', encoding='utf-8') as file: |
| |
186 | return json.load(file) |
| |
187 | |
| |
188 | def search_anime_by_title(json_data, search_title): |
| |
189 | results = [] |
| |
190 | if search_title=="1P": |
| |
191 | search_title = "ONE PIECE" |
| |
192 | for list_item in json_data['data']['MediaListCollection']['lists']: |
| |
193 | for entry in list_item['entries']: |
| |
194 | media = entry['media'] |
| |
195 | romaji_title = media['title']['romaji'] |
| |
196 | english_title = media['title']['english'] |
| |
197 | episodes = media['episodes'] |
| |
198 | duration = media['duration'] |
| |
199 | try: |
| |
200 | if search_title.lower() in romaji_title.lower() or search_title.lower() in english_title.lower(): |
| |
201 | results.append({ |
| |
202 | 'id': media['id'], |
| |
203 | 'progress': entry['progress'], |
| |
204 | 'romaji_title': romaji_title, |
| |
205 | 'english_title': english_title, |
| |
206 | 'episodes': episodes, |
| |
207 | 'duration': duration, |
| |
208 | }) |
| |
209 | except: |
| |
210 | pass |
| |
211 | |
| |
212 | return results |
| |
213 | |
| |
214 | def update_anime_progress(token: str, media_id: int, progress: int): |
| |
215 | url = "https://graphql.anilist.co" |
| |
216 | |
| |
217 | query = ''' |
| |
218 | mutation($mediaId: Int, $progress: Int) { |
| |
219 | SaveMediaListEntry(mediaId: $mediaId, progress: $progress) { |
| |
220 | id |
| |
221 | progress |
| |
222 | } |
| |
223 | } |
| |
224 | ''' |
| |
225 | |
| |
226 | variables = { |
| |
227 | "mediaId": media_id, # The AniList ID of the anime |
| |
228 | "progress": progress # The number of the latest episode you watched |
| |
229 | } |
| |
230 | |
| |
231 | headers = { |
| |
232 | "Authorization": f"Bearer {token}", |
| |
233 | "Content-Type": "application/json", |
| |
234 | "Accept": "application/json" |
| |
235 | } |
| |
236 | |
| |
237 | response = requests.post(url, json={"query": query, "variables": variables}, headers=headers) |
| |
238 | |
| |
239 | if response.status_code == 200: |
| |
240 | data = response.json() |
| |
241 | updated_progress = data['data']['SaveMediaListEntry']['progress'] |
| |
242 | print("updating progress..") |
| |
243 | print(f"Anime progress updated! Latest watched episode: {updated_progress}") |
| |
244 | else: |
| |
245 | print(f"Error {response.status_code}: {response.text}") |
| |
246 | |
| |
247 | def rate_anime(anilist_token, media_id, score): |
| |
248 | url = 'https://graphql.anilist.co' |
| |
249 | |
| |
250 | # GraphQL mutation to rate anime |
| |
251 | query = ''' |
| |
252 | mutation($mediaId: Int, $score: Float) { |
| |
253 | SaveMediaListEntry(mediaId: $mediaId, score: $score) { |
| |
254 | id |
| |
255 | mediaId |
| |
256 | score |
| |
257 | } |
| |
258 | } |
| |
259 | ''' |
| |
260 | |
| |
261 | # Variables for the mutation |
| |
262 | variables = { |
| |
263 | "mediaId": media_id, |
| |
264 | "score": float(score) |
| |
265 | } |
| |
266 | |
| |
267 | headers = { |
| |
268 | "Authorization": f"Bearer {anilist_token}", |
| |
269 | "Content-Type": "application/json", |
| |
270 | } |
| |
271 | |
| |
272 | response = requests.post(url, json={'query': query, 'variables': variables}, headers=headers) |
| |
273 | |
| |
274 | if response.status_code == 200: |
| |
275 | data = response.json() |
| |
276 | print(f"Successfully rated anime (mediaId: {media_id}) with score: {score}") |
| |
277 | return data |
| |
278 | else: |
| |
279 | print(f"Failed to rate anime. Status Code: {response.status_code}, Response: {response.text}") |
| |
280 | return None |
| |
281 | |
| |
282 | # ----------------------------------------------- Select Anime Functions ---------------------------------------- |
| |
283 | |
| |
284 | def load_anime_data(filename): |
| |
285 | anime_list = {} |
| |
286 | with open(filename, "r") as file: |
| |
287 | data = file.read().strip() |
| |
288 | entries = data.split("NEWLINEFROMHERE") |
| |
289 | for entry in entries: |
| |
290 | if entry.strip(): |
| |
291 | parts = entry.split(maxsplit=1) |
| |
292 | if len(parts) == 2: |
| |
293 | anime_id, anime_name = parts |
| |
294 | anime_list[anime_name] = anime_id |
| |
295 | return anime_list |
| |
296 | |
| |
297 | def extract_anime_info(json_data): |
| |
298 | anime_info_list = [] |
| |
299 | anime_info_dict = {} |
| |
300 | |
| |
301 | # Traverse the JSON structure to get to the list of entries |
| |
302 | for list_entry in json_data['data']['MediaListCollection']['lists']: |
| |
303 | for entry in list_entry['entries']: |
| |
304 | anime = entry['media'] |
| |
305 | anime_info = { |
| |
306 | 'id': anime['id'], |
| |
307 | 'romaji_name': anime['title']['romaji'], |
| |
308 | 'english_name': anime['title']['english'], |
| |
309 | 'status': entry['status'], |
| |
310 | 'episodes': anime['episodes'], |
| |
311 | } |
| |
312 | |
| |
313 | if anime['title']['english'] == None: |
| |
314 | anime['title']['english'] = anime['title']['romaji'] |
| |
315 | # if anime['title'][''] == None: |
| |
316 | # anime['title']['english'] = anime['title']['romanji'] |
| |
317 | |
| |
318 | anime_info_dict[anime['title']['english']] = str(anime['id']) # {anime_name: anime_id, ...} |
| |
319 | anime_info_list.append(anime_info) # [{"id": anime_id, "romaji_name": anime_name_romnaji, "english_name": anime_name_english, status: anime_status}, ...] |
| |
320 | |
| |
321 | return [anime_info_dict, anime_info_list] |
| |
322 | |
| |
323 | def select_anime(anime_list): |
| |
324 | def main(stdscr): |
| |
325 | # Clear screen |
| |
326 | curses.curs_set(1) |
| |
327 | stdscr.clear() |
| |
328 | stdscr.refresh() |
| |
329 | |
| |
330 | # Initial values |
| |
331 | current_input = "" |
| |
332 | filtered_list = list(anime_list.keys()) |
| |
333 | selected_index = 0 |
| |
334 | scroll_position = 0 # Position to start displaying the list from |
| |
335 | |
| |
336 | try: |
| |
337 | while True: |
| |
338 | stdscr.clear() |
| |
339 | |
| |
340 | # Get terminal dimensions |
| |
341 | height, width = stdscr.getmaxyx() |
| |
342 | list_height = height - 2 # Leave space for input and status lines |
| |
343 | |
| |
344 | # Handle edge case where terminal window is too small |
| |
345 | if height < 5 or width < len("Search Anime: ") + len(current_input): |
| |
346 | stdscr.addstr(0, 0, "Terminal window too small!") |
| |
347 | stdscr.refresh() |
| |
348 | continue |
| |
349 | |
| |
350 | # Display search prompt |
| |
351 | stdscr.addstr(0, 0, "Search Anime: " + current_input) |
| |
352 | |
| |
353 | # Calculate scrolling parameters |
| |
354 | if selected_index < scroll_position: |
| |
355 | scroll_position = selected_index |
| |
356 | elif selected_index >= scroll_position + list_height: |
| |
357 | scroll_position = selected_index - list_height + 1 |
| |
358 | |
| |
359 | # Display filtered results with highlighting and scrolling |
| |
360 | for idx in range(scroll_position, min(len(filtered_list), scroll_position + list_height)): |
| |
361 | anime = filtered_list[idx] |
| |
362 | display_idx = idx - scroll_position + 1 |
| |
363 | if idx == selected_index: |
| |
364 | stdscr.addstr(display_idx, 0, anime, curses.A_REVERSE) |
| |
365 | else: |
| |
366 | stdscr.addstr(display_idx, 0, anime) |
| |
367 | |
| |
368 | # Get user input |
| |
369 | key = stdscr.getch() |
| |
370 | |
| |
371 | # Handle key inputs |
| |
372 | if key in [curses.KEY_BACKSPACE, 127, 8]: |
| |
373 | current_input = current_input[:-1] |
| |
374 | selected_index = 0 |
| |
375 | elif key == curses.KEY_DOWN: |
| |
376 | if selected_index < len(filtered_list) - 1: |
| |
377 | selected_index += 1 |
| |
378 | elif key == curses.KEY_UP: |
| |
379 | if selected_index > 0: |
| |
380 | selected_index -= 1 |
| |
381 | elif key == curses.KEY_ENTER or key in [10, 13]: # Enter key |
| |
382 | if filtered_list: |
| |
383 | selected_anime = filtered_list[selected_index] |
| |
384 | selected_id = anime_list[selected_anime] |
| |
385 | break |
| |
386 | elif key == ord('q'): # Exit if 'q' is pressed |
| |
387 | return |
| |
388 | else: |
| |
389 | try: |
| |
390 | current_input += chr(key) |
| |
391 | selected_index = 0 |
| |
392 | except: |
| |
393 | pass |
| |
394 | |
| |
395 | # Filter the anime list based on current input |
| |
396 | filtered_list = [anime for anime in anime_list.keys() if current_input.lower() in anime.lower()] |
| |
397 | |
| |
398 | # Ensure selected index is within bounds after filtering |
| |
399 | if selected_index >= len(filtered_list): |
| |
400 | selected_index = len(filtered_list) - 1 |
| |
401 | |
| |
402 | # Refresh screen |
| |
403 | stdscr.refresh() |
| |
404 | |
| |
405 | except Exception as e: |
| |
406 | # If an exception occurs, print it after exiting curses |
| |
407 | curses.endwin() |
| |
408 | print(f"An error occurred: {e}") |
| |
409 | finally: |
| |
410 | # Ensure curses always exits cleanly |
| |
411 | curses.endwin() |
| |
412 | |
| |
413 | # Print selected anime and its ID after exiting curses mode |
| |
414 | # print(f"Selected Anime: {selected_anime}, ID: {selected_id}") |
| |
415 | with open(f"/tmp/curd/curd_anime", "w") as anime_name: |
| |
416 | anime_name.write(selected_anime) |
| |
417 | |
| |
418 | with open(f"/tmp/curd/curd_id", "w") as id_file: |
| |
419 | id_file.write(str(selected_id)) |
| |
420 | |
| |
421 | return True |
| |
422 | |
| |
423 | # Initialize curses |
| |
424 | try: |
| |
425 | curses.wrapper(main) |
| |
426 | except: |
| |
427 | pass |
| |
428 | |
| |
429 | # ----------------------------------------------- Select Link Functions ---------------------------------------- |
| |
430 | |
| |
431 | |
| |
432 | def load_links(filename): |
| |
433 | links = [] |
| |
434 | with open(filename, 'r') as file: |
| |
435 | content = file.read() |
| |
436 | # Regex to match the name and URL pairs |
| |
437 | matches = re.findall(r'(\S+)\s*>\s*(https?://\S+)', content) |
| |
438 | links = [(name, url) for name, url in matches] |
| |
439 | return links |
| |
440 | |
| |
441 | def display_links(stdscr, links): |
| |
442 | curses.curs_set(0) # Hide the cursor |
| |
443 | stdscr.clear() |
| |
444 | h, w = stdscr.getmaxyx() |
| |
445 | max_height = h - 1 |
| |
446 | start = 0 |
| |
447 | selected_index = 0 |
| |
448 | |
| |
449 | while True: |
| |
450 | stdscr.clear() |
| |
451 | for i in range(start, min(start + max_height, len(links))): |
| |
452 | if i == selected_index: |
| |
453 | stdscr.attron(curses.A_REVERSE) # Highlight the selected item |
| |
454 | stdscr.addstr(i - start, 0, links[i][0]) |
| |
455 | if i == selected_index: |
| |
456 | stdscr.attroff(curses.A_REVERSE) # Remove highlight after the line |
| |
457 | stdscr.addstr(max_height, 0, "Use arrow keys to scroll, Enter to select, q to quit") |
| |
458 | stdscr.refresh() |
| |
459 | |
| |
460 | key = stdscr.getch() |
| |
461 | |
| |
462 | if key == curses.KEY_DOWN: |
| |
463 | if selected_index < len(links) - 1: |
| |
464 | selected_index += 1 |
| |
465 | if selected_index >= start + max_height: |
| |
466 | start += 1 |
| |
467 | elif key == curses.KEY_UP: |
| |
468 | if selected_index > 0: |
| |
469 | selected_index -= 1 |
| |
470 | if selected_index < start: |
| |
471 | start -= 1 |
| |
472 | elif key == curses.KEY_ENTER or key == 10: # Enter key |
| |
473 | stdscr.clear() |
| |
474 | stdscr.addstr(0, 0, f"Selected URL: {links[selected_index][1]}") |
| |
475 | # print("done") |
| |
476 | with open(f"/tmp/curd/curd_link", "w") as temp: |
| |
477 | temp.write(links[selected_index][1]) |
| |
478 | stdscr.refresh() |
| |
479 | # stdscr.getch() # Wait for any key press to exit |
| |
480 | return links[selected_index][1] |
| |
481 | break |
| |
482 | elif key == ord('q'): |
| |
483 | break |
| |
484 | |
| |
485 | # ----------------------------------------------- Start Video Functions ---------------------------------------- |
| |
486 | |
| |
487 | def start_video(link, salt:str, args:list=[]): |
| |
488 | # print(f"SALT IS {salt}") |
| |
489 | |
| |
490 | args_str = ' '.join(args) |
| |
491 | |
| |
492 | # Build the complete command string |
| |
493 | command = f"mpv {args_str} --no-terminal --really-quiet --input-ipc-server=/tmp/curd/curd_mpvsocket{salt} {link}" |
| |
494 | |
| |
495 | subprocess.Popen(command, shell=True) |
| |
496 | |
| |
497 | def send_command(ipc_socket_path, command): |
| |
498 | """ |
| |
499 | Sends a command to the MPV IPC socket and returns the response. |
| |
500 | """ |
| |
501 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| |
502 | s.connect(ipc_socket_path) |
| |
503 | command_str = json.dumps({"command": command}) + "\n" |
| |
504 | s.sendall(command_str.encode()) |
| |
505 | response = s.recv(4096).decode() |
| |
506 | |
| |
507 | try: |
| |
508 | response_data = json.loads(response) |
| |
509 | if 'data' in response_data: |
| |
510 | return response_data['data'] |
| |
511 | except json.JSONDecodeError: |
| |
512 | return None |
| |
513 | return None |
| |
514 | |
| |
515 | def get_mpv_paused_status(ipc_socket_path): |
| |
516 | status = send_command(ipc_socket_path, ["get_property", "pause"]) |
| |
517 | if status is not None: |
| |
518 | return status |
| |
519 | else: |
| |
520 | return False |
| |
521 | |
| |
522 | def get_mpv_playback_speed(ipc_socket_path): |
| |
523 | current_speed = send_command(ipc_socket_path, ["get_property", "speed"]) |
| |
524 | if current_speed is not None: |
| |
525 | return current_speed |
| |
526 | else: |
| |
527 | print("Failed to get playback speed.") |
| |
528 | |
| |
529 | def get_percentage_watched(ipc_socket_path): |
| |
530 | """ |
| |
531 | Calculates the percentage watched of the currently playing video. |
| |
532 | """ |
| |
533 | # Get current playback time and total duration |
| |
534 | current_time = send_command(ipc_socket_path, ["get_property", "time-pos"]) |
| |
535 | duration = send_command(ipc_socket_path, ["get_property", "duration"]) |
| |
536 | |
| |
537 | if current_time is not None and duration is not None and duration > 0: |
| |
538 | percentage_watched = (current_time / duration) * 100 |
| |
539 | return percentage_watched |
| |
540 | return None |
| |
541 | |
| |
542 | def percentage_watched(playback_time:int, duration:int): |
| |
543 | if playback_time is not None and duration is not None and duration > 0: |
| |
544 | video_percentage_watched = (playback_time/duration) * 100 |
| |
545 | return video_percentage_watched |
| |
546 | return None |
| |
547 | |
| |
548 | # ----------------------------------------------- Track Anime Functions ---------------------------------------- |
| |
549 | def add_anime(database_file, anilist_id, allanime_id, episode, time, duration, name): |
| |
550 | with open(database_file, mode='a', newline='') as file: |
| |
551 | writer = csv.writer(file) |
| |
552 | writer.writerow([anilist_id, allanime_id, episode, time, duration, name]) |
| |
553 | print("Written to file") |
| |
554 | |
| |
555 | # Function to delete an anime entry by Anilist ID and Allanime ID |
| |
556 | def delete_anime(database_file, anilist_id, allanime_id): |
| |
557 | anime_list = [] |
| |
558 | with open(database_file, mode='r', newline='') as file: |
| |
559 | reader = csv.reader(file) |
| |
560 | for row in reader: |
| |
561 | # Keep only entries that don't match the Anilist ID and Allanime ID |
| |
562 | if row[0] != anilist_id or row[1] != allanime_id: |
| |
563 | anime_list.append(row) |
| |
564 | |
| |
565 | # Rewrite the database without the deleted entry |
| |
566 | with open(database_file, mode='w', newline='') as file: |
| |
567 | writer = csv.writer(file) |
| |
568 | writer.writerows(anime_list) |
| |
569 | |
| |
570 | # Function to retrieve all anime entries |
| |
571 | def get_all_anime(database_file): |
| |
572 | anime_list = [] |
| |
573 | with open(database_file, mode='r', newline='') as file: |
| |
574 | reader = csv.reader(file) |
| |
575 | for row in reader: |
| |
576 | anime_list.append({ |
| |
577 | 'anilist_id': row[0], |
| |
578 | 'allanime_id': row[1], |
| |
579 | 'episode': row[2], |
| |
580 | 'time': row[3], |
| |
581 | 'duration': row[4], |
| |
582 | 'name': row[5] |
| |
583 | }) |
| |
584 | return anime_list |
| |
585 | |
| |
586 | # Function to update or add a new anime entry |
| |
587 | def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:str, time:str, duration:str, name:str): |
| |
588 | anime_list = get_all_anime(database_file) |
| |
589 | updated = False |
| |
590 | |
| |
591 | # Create a new list to store updated entries |
| |
592 | updated_anime_list = [] |
| |
593 | |
| |
594 | # Check if the anime entry exists and update it |
| |
595 | for anime in anime_list: |
| |
596 | if anime['anilist_id'] == anilist_id and anime['allanime_id'] == allanime_id: |
| |
597 | updated_anime_list.append({ |
| |
598 | 'anilist_id': anilist_id, |
| |
599 | 'allanime_id': allanime_id, |
| |
600 | 'episode': episode, |
| |
601 | 'time': time, |
| |
602 | 'duration': duration, |
| |
603 | 'name': name |
| |
604 | }) |
| |
605 | updated = True |
| |
606 | else: |
| |
607 | updated_anime_list.append(anime) |
| |
608 | |
| |
609 | # If no entry was updated, add new entry |
| |
610 | if not updated: |
| |
611 | updated_anime_list.append({ |
| |
612 | 'anilist_id': anilist_id, |
| |
613 | 'allanime_id': allanime_id, |
| |
614 | 'episode': episode, |
| |
615 | 'time': time, |
| |
616 | 'duration': duration, |
| |
617 | 'name': name |
| |
618 | }) |
| |
619 | |
| |
620 | # Write all entries back to the file |
| |
621 | with open(database_file, mode='w', newline='') as file: |
| |
622 | writer = csv.writer(file) |
| |
623 | for anime in updated_anime_list: |
| |
624 | writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']]) |
| |
625 | |
| |
626 | # print("Updated the file") |
| |
627 | |
| |
628 | def find_anime(anime_list, anilist_id=-1, allanime_id=-1): |
| |
629 | for anime in anime_list: |
| |
630 | if anime['anilist_id'] == anilist_id or anime['allanime_id'] == allanime_id: |
| |
631 | return anime |
| |
632 | |
| |
633 | return False |
| |
634 | |
| |
635 | # ----------------------------------------------- Curd Functions ---------------------------------------- |
| |
636 | |
| 29 | def get_contents_of(tmp_file_name): |
637 | def get_contents_of(tmp_file_name): |
| 30 | with open(f"/tmp/curd/curd_{tmp_file_name}", "r") as temp_file: |
638 | with open(f"/tmp/curd/curd_{tmp_file_name}", "r") as temp_file: |
| 31 | return temp_file.read() |
639 | return temp_file.read() |
| @@ -59,16 +667,10 @@ def read_tmp(tmp_filename:str): |
| 59 | print("fuck") |
667 | print("fuck") |
| 60 | return False |
668 | return False |
| 61 | |
669 | |
| 62 | # write_to_tmp("id", "this") |
| |
| 63 | # print(read_tmp("id")) |
| |
| 64 | # exit(0) |
| |
| 65 | |
| |
| 66 | def download_anilist_data(access_token, user_id): |
670 | def download_anilist_data(access_token, user_id): |
| 67 | ''' dowlnoad anilist user data''' |
671 | ''' dowlnoad anilist user data''' |
| 68 | # print("downloading user data") |
672 | # print("downloading user data") |
| 69 | anilist_user_data = get_user_data(access_token, user_id) |
673 | anilist_user_data = get_user_data(access_token, user_id) |
| 70 | with open("response.json", "w") as response: |
| |
| 71 | response.write(str(anilist_user_data)) |
| |
| 72 | try: |
674 | try: |
| 73 | if anilist_user_data['data'] == None: |
675 | if anilist_user_data['data'] == None: |
| 74 | print("Cannot process user data.") |
676 | print("Cannot process user data.") |
| @@ -182,11 +784,8 @@ def load_config() -> dict: |
| 182 | |
784 | |
| 183 | return config_dict |
785 | return config_dict |
| 184 | |
786 | |
| 185 | # else: |
787 | # ----------------------------------------------- Start of script ---------------------------------------- |
| 186 | # with open(config_file_path, "r") as config_file: |
| |
| 187 | # return config_file.read() |
| |
| 188 | |
788 | |
| 189 | # START OF SCRIPT |
| |
| 190 | current_dir = Path(__file__).parent |
789 | current_dir = Path(__file__).parent |
| 191 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
790 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 192 | # print(current_dir) |
791 | # print(current_dir) |
| @@ -238,7 +837,6 @@ if args.new: |
| 238 | |
837 | |
| 239 | user_id, user_name = get_anilist_user_id(access_token) |
838 | user_id, user_name = get_anilist_user_id(access_token) |
| 240 | mark_episode_as_completed_at = get_userconfig_value(user_config, "percentage_to_mark_complete") |
839 | mark_episode_as_completed_at = get_userconfig_value(user_config, "percentage_to_mark_complete") |
| 241 | print(mark_episode_as_completed_at) |
| |
| 242 | |
840 | |
| 243 | anilist_user_data = download_anilist_data(access_token, user_id) |
841 | anilist_user_data = download_anilist_data(access_token, user_id) |
| 244 | anime_dict = extract_anime_info(anilist_user_data)[0] |
842 | anime_dict = extract_anime_info(anilist_user_data)[0] |