Commit ef20df4e76

ef20df4e769efb19b25555c107b39849dd2d3268

parent: 13edc23330

Unsigned

Wraient <wraient.911@gmail.com> · 2024-09-21 23:52 UTC

Better errors
comment out verbose output
better error handling

Layout: unified · split

curd.py +41 −41
@@ -1,3 +1,5 @@
1# if you are trying to read this code, god help you.
2# this is how my implementation of the ani-cli code to get the anime works
13
24# search (anime_list.sh) -> tmp/anime_list
35# anime_id -> episode_list.sh
@@ -58,14 +60,14 @@ def read_tmp(tmp_filename:str):
5860
5961def download_anilist_data(access_token, user_id):
6062 ''' dowlnoad anilist user data'''
61 print("downloading user data")
63 # print("downloading user data")
6264 anilist_user_data = get_user_data(access_token, user_id)
6365 with open("response.json", "w") as response:
6466 response.write(str(anilist_user_data))
6567 try:
6668 if anilist_user_data['data'] == None:
6769 print("Cannot process user data.")
68 print(anilist_user_data)
70 # print(anilist_user_data)
6971 exit()
7072 except:
7173 pass
@@ -83,7 +85,7 @@ def load_config():
8385
8486 config_file_path = os.path.join(folder_name, file_name)
8587
86 print(config_file_path)
88 # print(config_file_path)
8789 if not os.path.exists(os.path.join(config_file_path)):
8890 print("Creating config")
8991
@@ -167,8 +169,8 @@ cleaned_text = re.sub(r'\(.*$', '', anime_name).strip()
167169
168170try:
169171 result = search_anime_by_title(anilist_user_data, cleaned_text)[0]
170 print(result)
171 anime_id = result['id']
172 # print(result)
173 media_id = result['id']
172174 progress = int(result['progress'])
173175 title = result['english_title']
174176 total_episodes = result['episodes']
@@ -176,16 +178,15 @@ try:
176178except KeyboardInterrupt:
177179 print("bye")
178180 exit(0)
179except:
180 print("fucked up")
181 anime_id = 98460
182 progress = 0
181except Exception as e:
182 print(f"Searching anime error: {e}")
183 exit(1)
183184
184185try:
185 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(anime_id))
186 print(finding_anime)
186 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(media_id))
187 # print(finding_anime)
187188 if finding_anime:
188 print("AUTOMATICALLY BABY")
189 print("Found anime in history")
189190 write_to_tmp("id", finding_anime['allanime_id'])
190191 write_to_tmp("anime", finding_anime['name'])
191192 else:
@@ -196,7 +197,7 @@ except KeyboardInterrupt:
196197 print("bye")
197198 exit(0)
198199except:
199 print("Cannot automate")
200 print("Please select anime")
200201 select_anime(anime_dict)
201202
202203run_script("episode_list")
@@ -215,26 +216,26 @@ last_episode = int(read_tmp("episode_list").split()[-1])
215216
216217anime_watch_history = get_all_anime(user_config['history_file'])
217218# anime_watch_history[]
218anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id"))
219anime_history = find_anime(anime_watch_history, anilist_id=media_id, allanime_id=get_contents_of("id"))
219220episode_completed = False
220221if anime_history: # if it exists in local history
221 print(f"came in history {str(progress)}")
222 # print(f"came in history {str(progress)}")
222223 if int(anime_history['episode']) != progress+1: # if the upstream progress is ahead
223224 write_to_tmp("ep_no", str(int(progress)+1))
224 print(f"Starting anime from upstream {str(progress + 1)}")
225 # print(f"Starting anime from upstream {str(progress + 1)}")
225226
226227 watching_ep = progress + 1
227228
228229 elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead
229230 mpv_args.append(f"--start={anime_history['time']}")
230231 write_to_tmp("ep_no", anime_history['episode'])
231 print(f"STARTING ANIME FROM EPISODE {anime_history['episode']} {anime_history['time']}")
232 # print(f"STARTING ANIME FROM EPISODE {anime_history['episode']} {anime_history['time']}")
232233
233234 watching_ep = int(anime_history['episode'])
234235else: # if there is no history
235 print("no history found")
236 # print("no history found")
236237
237 print("First time watch")
238 # print("First time watch")
238239
239240 if progress == last_episode:
240241 user_input = input("Do you want to start this anime from beginning? (y/n):")
@@ -251,10 +252,12 @@ else: # if there is no history
251252run_script("episode_url")
252253
253254# Print the result
254if anime_id:
255 print(f"Anime ID: {anime_id}")
255if media_id:
256 # print(f"Anime ID: {media_id}")
257 pass
256258else:
257259 print("Anime not found.")
260 exit(1)
258261
259262links = load_links("scripts/tmp/links")
260263
@@ -262,7 +265,7 @@ while True:
262265
263266 try:
264267 salt = random.randint(0,1500)
265 print("SALT IS:"+str(salt))
268 # print("SALT IS:"+str(salt))
266269 start_video(links[0][1], salt, mpv_args)
267270
268271 mpv_socket_path = "/tmp/mpvsocket"+str(salt)
@@ -294,21 +297,21 @@ while True:
294297 # print("Playback time:", playback_time)
295298
296299 watched_percentage = get_percentage_watched(mpv_socket_path)
297 print("watched percentage", watched_percentage)
300 mpv_playback_speed = get_mpv_playback_speed(mpv_socket_path)
301 # print("mpv speed", mpv_playback_speed)
302 # print("watched percentage", watched_percentage)
298303 if watched_percentage > mark_episode_as_completed_at:
299304
300305 episode_completed = True
301306 binge_watching = True
302 else:
303 print(f"Episode not done: {watched_percentage} {mark_episode_as_completed_at}")
304 # video_duration = subprocess.run(mpv_duration_command, shell=True, capture_output=True, text=True)
305
307 # else:
308 # print(f"Episode not done: {watched_percentage} {mark_episode_as_completed_at}")
309 # video_duration = subprocess.run(mpv_duration_command, shell=True, capture_output=True, text=True):
310 elif data['error'] == "property unavailable": # Stream has not started yet
311 pass
306312 else:
307 if data['error'] == "property unavailable": # Stream has not started yet
308 pass
309 else:
310 print("Error (330):", data["error"])
311 break
313 print("Error (330):", data["error"])
314 break
312315 except json.decoder.JSONDecodeError as e:
313316 print("Error decoding JSON:", e)
314317 break # Exit the loop on unexpected JSON error
@@ -316,11 +319,11 @@ while True:
316319 print("bye")
317320 exit(0)
318321 except Exception as e:
319 print("some error fucked everything up:\n", e)
322 print("Unknown:\n", e)
320323 else:
321324 killing_error = str(result.stderr)
322325 if killing_error == "property unavailable": # mpv is not started yet
323 print("passing")
326 # print("passing")
324327 pass
325328 else: # Stream has ended (maybe)
326329 print("Error (346):", killing_error)
@@ -330,8 +333,10 @@ while True:
330333 # print("fuck")
331334 # print(f"Exception: {e}")
332335 break
333 except ConnectionRefusedError:
336 except ConnectionRefusedError: # doesnt work ig
334337 print("Player Closed")
338 # print("have a great")
339 # exit(0)
335340 except KeyboardInterrupt:
336341 if watched_percentage > mark_episode_as_completed_at:
337342 watching_ep = int(watching_ep)+1
@@ -361,12 +366,7 @@ while True:
361366
362367 watching_ep = int(watching_ep)+1
363368 write_to_tmp("ep_no", str(watching_ep))
364 print("here")
365 update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
366 print("inside")
367 print(f"Episode done: {type(watching_ep)}")
368 print(f"Episode done: {int(watching_ep)}")
369
369 update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
370370 anime_watch_history = get_all_anime(user_config['history_file'])
371371 # anime_watch_history[]
372372 anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id"))
start_video.py +1 −1
@@ -4,7 +4,7 @@ import socket
44import subprocess
55
66def start_video(link, salt:str, args:list=[]):
7 print(f"SALT IS {salt}")
7 # print(f"SALT IS {salt}")
88
99 args_str = ' '.join(args)
1010
track_anime.py +3 −3
@@ -81,7 +81,7 @@ def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:s
8181 for anime in updated_anime_list:
8282 writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']])
8383
84 print("Updated the file")
84 # print("Updated the file")
8585
8686def find_anime(anime_list, anilist_id=-1, allanime_id=-1):
8787 for anime in anime_list:
@@ -91,7 +91,7 @@ def find_anime(anime_list, anilist_id=-1, allanime_id=-1):
9191 return False
9292
9393# Example usage
94database_name = "/home/wraient/.local/share/curd/curd_history.txt"
94# database_name = "/home/wraient/.local/share/curd/curd_history.txt"
9595# add_anime(database_name, '54321', '09876', '3', '100', '24', 'My Hero Academia')
9696
9797# # Print all anime entries
@@ -103,4 +103,4 @@ database_name = "/home/wraient/.local/share/curd/curd_history.txt"
103103# update_anime(database_name, '12345', '67890', '13', '120', 'Attack on Titan')
104104
105105# # Print all anime entries after update
106print(get_all_anime(database_name))
106# print(get_all_anime(database_name))