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
1 3
2# search (anime_list.sh) -> tmp/anime_list 4# search (anime_list.sh) -> tmp/anime_list
3# anime_id -> episode_list.sh 5# anime_id -> episode_list.sh
@@ -58,14 +60,14 @@ def read_tmp(tmp_filename:str):
58 60
59def download_anilist_data(access_token, user_id): 61def download_anilist_data(access_token, user_id):
60 ''' dowlnoad anilist user data''' 62 ''' dowlnoad anilist user data'''
61 print("downloading user data") 63 # print("downloading user data")
62 anilist_user_data = get_user_data(access_token, user_id) 64 anilist_user_data = get_user_data(access_token, user_id)
63 with open("response.json", "w") as response: 65 with open("response.json", "w") as response:
64 response.write(str(anilist_user_data)) 66 response.write(str(anilist_user_data))
65 try: 67 try:
66 if anilist_user_data['data'] == None: 68 if anilist_user_data['data'] == None:
67 print("Cannot process user data.") 69 print("Cannot process user data.")
68 print(anilist_user_data) 70 # print(anilist_user_data)
69 exit() 71 exit()
70 except: 72 except:
71 pass 73 pass
@@ -83,7 +85,7 @@ def load_config():
83 85
84 config_file_path = os.path.join(folder_name, file_name) 86 config_file_path = os.path.join(folder_name, file_name)
85 87
86 print(config_file_path) 88 # print(config_file_path)
87 if not os.path.exists(os.path.join(config_file_path)): 89 if not os.path.exists(os.path.join(config_file_path)):
88 print("Creating config") 90 print("Creating config")
89 91
@@ -167,8 +169,8 @@ cleaned_text = re.sub(r'\(.*$', '', anime_name).strip()
167 169
168try: 170try:
169 result = search_anime_by_title(anilist_user_data, cleaned_text)[0] 171 result = search_anime_by_title(anilist_user_data, cleaned_text)[0]
170 print(result) 172 # print(result)
171 anime_id = result['id'] 173 media_id = result['id']
172 progress = int(result['progress']) 174 progress = int(result['progress'])
173 title = result['english_title'] 175 title = result['english_title']
174 total_episodes = result['episodes'] 176 total_episodes = result['episodes']
@@ -176,16 +178,15 @@ try:
176except KeyboardInterrupt: 178except KeyboardInterrupt:
177 print("bye") 179 print("bye")
178 exit(0) 180 exit(0)
179except: 181except Exception as e:
180 print("fucked up") 182 print(f"Searching anime error: {e}")
181 anime_id = 98460 183 exit(1)
182 progress = 0
183 184
184try: 185try:
185 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(anime_id)) 186 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(media_id))
186 print(finding_anime) 187 # print(finding_anime)
187 if finding_anime: 188 if finding_anime:
188 print("AUTOMATICALLY BABY") 189 print("Found anime in history")
189 write_to_tmp("id", finding_anime['allanime_id']) 190 write_to_tmp("id", finding_anime['allanime_id'])
190 write_to_tmp("anime", finding_anime['name']) 191 write_to_tmp("anime", finding_anime['name'])
191 else: 192 else:
@@ -196,7 +197,7 @@ except KeyboardInterrupt:
196 print("bye") 197 print("bye")
197 exit(0) 198 exit(0)
198except: 199except:
199 print("Cannot automate") 200 print("Please select anime")
200 select_anime(anime_dict) 201 select_anime(anime_dict)
201 202
202run_script("episode_list") 203run_script("episode_list")
@@ -215,26 +216,26 @@ last_episode = int(read_tmp("episode_list").split()[-1])
215 216
216anime_watch_history = get_all_anime(user_config['history_file']) 217anime_watch_history = get_all_anime(user_config['history_file'])
217# anime_watch_history[] 218# 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"))
219episode_completed = False 220episode_completed = False
220if anime_history: # if it exists in local history 221if anime_history: # if it exists in local history
221 print(f"came in history {str(progress)}") 222 # print(f"came in history {str(progress)}")
222 if int(anime_history['episode']) != progress+1: # if the upstream progress is ahead 223 if int(anime_history['episode']) != progress+1: # if the upstream progress is ahead
223 write_to_tmp("ep_no", str(int(progress)+1)) 224 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)}")
225 226
226 watching_ep = progress + 1 227 watching_ep = progress + 1
227 228
228 elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead 229 elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead
229 mpv_args.append(f"--start={anime_history['time']}") 230 mpv_args.append(f"--start={anime_history['time']}")
230 write_to_tmp("ep_no", anime_history['episode']) 231 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']}")
232 233
233 watching_ep = int(anime_history['episode']) 234 watching_ep = int(anime_history['episode'])
234else: # if there is no history 235else: # if there is no history
235 print("no history found") 236 # print("no history found")
236 237
237 print("First time watch") 238 # print("First time watch")
238 239
239 if progress == last_episode: 240 if progress == last_episode:
240 user_input = input("Do you want to start this anime from beginning? (y/n):") 241 user_input = input("Do you want to start this anime from beginning? (y/n):")
@@ -251,10 +252,12 @@ else: # if there is no history
251run_script("episode_url") 252run_script("episode_url")
252 253
253# Print the result 254# Print the result
254if anime_id: 255if media_id:
255 print(f"Anime ID: {anime_id}") 256 # print(f"Anime ID: {media_id}")
257 pass
256else: 258else:
257 print("Anime not found.") 259 print("Anime not found.")
260 exit(1)
258 261
259links = load_links("scripts/tmp/links") 262links = load_links("scripts/tmp/links")
260 263
@@ -262,7 +265,7 @@ while True:
262 265
263 try: 266 try:
264 salt = random.randint(0,1500) 267 salt = random.randint(0,1500)
265 print("SALT IS:"+str(salt)) 268 # print("SALT IS:"+str(salt))
266 start_video(links[0][1], salt, mpv_args) 269 start_video(links[0][1], salt, mpv_args)
267 270
268 mpv_socket_path = "/tmp/mpvsocket"+str(salt) 271 mpv_socket_path = "/tmp/mpvsocket"+str(salt)
@@ -294,21 +297,21 @@ while True:
294 # print("Playback time:", playback_time) 297 # print("Playback time:", playback_time)
295 298
296 watched_percentage = get_percentage_watched(mpv_socket_path) 299 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)
298 if watched_percentage > mark_episode_as_completed_at: 303 if watched_percentage > mark_episode_as_completed_at:
299 304
300 episode_completed = True 305 episode_completed = True
301 binge_watching = True 306 binge_watching = True
302 else: 307 # else:
303 print(f"Episode not done: {watched_percentage} {mark_episode_as_completed_at}") 308 # 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) 309 # video_duration = subprocess.run(mpv_duration_command, shell=True, capture_output=True, text=True):
305 310 elif data['error'] == "property unavailable": # Stream has not started yet
311 pass
306 else: 312 else:
307 if data['error'] == "property unavailable": # Stream has not started yet 313 print("Error (330):", data["error"])
308 pass 314 break
309 else:
310 print("Error (330):", data["error"])
311 break
312 except json.decoder.JSONDecodeError as e: 315 except json.decoder.JSONDecodeError as e:
313 print("Error decoding JSON:", e) 316 print("Error decoding JSON:", e)
314 break # Exit the loop on unexpected JSON error 317 break # Exit the loop on unexpected JSON error
@@ -316,11 +319,11 @@ while True:
316 print("bye") 319 print("bye")
317 exit(0) 320 exit(0)
318 except Exception as e: 321 except Exception as e:
319 print("some error fucked everything up:\n", e) 322 print("Unknown:\n", e)
320 else: 323 else:
321 killing_error = str(result.stderr) 324 killing_error = str(result.stderr)
322 if killing_error == "property unavailable": # mpv is not started yet 325 if killing_error == "property unavailable": # mpv is not started yet
323 print("passing") 326 # print("passing")
324 pass 327 pass
325 else: # Stream has ended (maybe) 328 else: # Stream has ended (maybe)
326 print("Error (346):", killing_error) 329 print("Error (346):", killing_error)
@@ -330,8 +333,10 @@ while True:
330 # print("fuck") 333 # print("fuck")
331 # print(f"Exception: {e}") 334 # print(f"Exception: {e}")
332 break 335 break
333 except ConnectionRefusedError: 336 except ConnectionRefusedError: # doesnt work ig
334 print("Player Closed") 337 print("Player Closed")
338 # print("have a great")
339 # exit(0)
335 except KeyboardInterrupt: 340 except KeyboardInterrupt:
336 if watched_percentage > mark_episode_as_completed_at: 341 if watched_percentage > mark_episode_as_completed_at:
337 watching_ep = int(watching_ep)+1 342 watching_ep = int(watching_ep)+1
@@ -361,12 +366,7 @@ while True:
361 366
362 watching_ep = int(watching_ep)+1 367 watching_ep = int(watching_ep)+1
363 write_to_tmp("ep_no", str(watching_ep)) 368 write_to_tmp("ep_no", str(watching_ep))
364 print("here") 369 update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
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
370 anime_watch_history = get_all_anime(user_config['history_file']) 370 anime_watch_history = get_all_anime(user_config['history_file'])
371 # anime_watch_history[] 371 # anime_watch_history[]
372 anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id")) 372 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
4import subprocess 4import subprocess
5 5
6def start_video(link, salt:str, args:list=[]): 6def start_video(link, salt:str, args:list=[]):
7 print(f"SALT IS {salt}") 7 # print(f"SALT IS {salt}")
8 8
9 args_str = ' '.join(args) 9 args_str = ' '.join(args)
10 10
track_anime.py +3 −3
@@ -81,7 +81,7 @@ def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:s
81 for anime in updated_anime_list: 81 for anime in updated_anime_list:
82 writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']]) 82 writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']])
83 83
84 print("Updated the file") 84 # print("Updated the file")
85 85
86def find_anime(anime_list, anilist_id=-1, allanime_id=-1): 86def find_anime(anime_list, anilist_id=-1, allanime_id=-1):
87 for anime in anime_list: 87 for anime in anime_list:
@@ -91,7 +91,7 @@ def find_anime(anime_list, anilist_id=-1, allanime_id=-1):
91 return False 91 return False
92 92
93# Example usage 93# Example usage
94database_name = "/home/wraient/.local/share/curd/curd_history.txt" 94# database_name = "/home/wraient/.local/share/curd/curd_history.txt"
95# add_anime(database_name, '54321', '09876', '3', '100', '24', 'My Hero Academia') 95# add_anime(database_name, '54321', '09876', '3', '100', '24', 'My Hero Academia')
96 96
97# # Print all anime entries 97# # Print all anime entries
@@ -103,4 +103,4 @@ database_name = "/home/wraient/.local/share/curd/curd_history.txt"
103# update_anime(database_name, '12345', '67890', '13', '120', 'Attack on Titan') 103# update_anime(database_name, '12345', '67890', '13', '120', 'Attack on Titan')
104 104
105# # Print all anime entries after update 105# # Print all anime entries after update
106print(get_all_anime(database_name)) 106# print(get_all_anime(database_name))