| @@ -15,16 +15,17 @@ import subprocess |
| 15 | import time |
15 | import time |
| 16 | import argparse |
16 | import argparse |
| 17 | |
17 | |
| 18 | from anilist import search_anime_by_title |
18 | from anilist import get_anime_image, search_anime_by_title |
| 19 | from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list |
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 | from select_link import load_links |
20 | from select_link import load_links |
| 21 | from start_video import start_video, send_command, get_percentage_watched, percentage_watched, get_mpv_playback_speed |
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 |
22 | from select_anime import load_anime_data |
| 23 | from select_anime import extract_anime_info |
23 | from select_anime import extract_anime_info |
| 24 | from select_anime import select_anime |
24 | from select_anime import select_anime |
| 25 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime |
25 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime |
| 26 | |
26 | |
| 27 | mark_episode_as_completed_at = 85 |
27 | mark_episode_as_completed_at = 85 |
| |
28 | discord_client_id = "1287457464148820089" |
| 28 | |
29 | |
| 29 | def get_contents_of(tmp_file_name): |
30 | def get_contents_of(tmp_file_name): |
| 30 | with open(f"{current_dir}/scripts/tmp/{tmp_file_name}", "r") as temp_file: |
31 | with open(f"{current_dir}/scripts/tmp/{tmp_file_name}", "r") as temp_file: |
| @@ -86,6 +87,11 @@ default_config = { |
| 86 | "presence_script_path":"curddiscordpresence.py" |
87 | "presence_script_path":"curddiscordpresence.py" |
| 87 | } |
88 | } |
| 88 | |
89 | |
| |
90 | def convert_seconds_to_minutes(seconds): |
| |
91 | minutes = seconds // 60 |
| |
92 | remaining_seconds = seconds % 60 |
| |
93 | return minutes, remaining_seconds |
| |
94 | |
| 89 | def get_userconfig_value(userconfig:dict, key:str): |
95 | def get_userconfig_value(userconfig:dict, key:str): |
| 90 | return userconfig.get(key, default_config.get(key)) |
96 | return userconfig.get(key, default_config.get(key)) |
| 91 | |
97 | |
| @@ -170,7 +176,7 @@ def load_config() -> dict: |
| 170 | # START OF SCRIPT |
176 | # START OF SCRIPT |
| 171 | current_dir = Path(__file__).parent |
177 | current_dir = Path(__file__).parent |
| 172 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
178 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 173 | print(current_dir) |
179 | # print(current_dir) |
| 174 | if not os.path.exists(f"{current_dir}/scripts/tmp/"): |
180 | if not os.path.exists(f"{current_dir}/scripts/tmp/"): |
| 175 | try: |
181 | try: |
| 176 | os.makedirs(os.path.dirname(f"{current_dir}/scripts/tmp/")) |
182 | os.makedirs(os.path.dirname(f"{current_dir}/scripts/tmp/")) |
| @@ -306,7 +312,7 @@ else: # if history does not exist |
| 306 | progress = last_episode - 1 |
312 | progress = last_episode - 1 |
| 307 | watching_ep = int(progress)+1 |
313 | watching_ep = int(progress)+1 |
| 308 | write_to_tmp("ep_no", str(watching_ep)) |
314 | write_to_tmp("ep_no", str(watching_ep)) |
| 309 | print(watching_ep) |
315 | # print(watching_ep) |
| 310 | # os.system("{current_dir}/scripts/episode_url.sh") |
316 | # os.system("{current_dir}/scripts/episode_url.sh") |
| 311 | run_script("episode_url") |
317 | run_script("episode_url") |
| 312 | |
318 | |
| @@ -320,6 +326,13 @@ else: |
| 320 | |
326 | |
| 321 | links = load_links(f"{current_dir}/scripts/tmp/links") |
327 | links = load_links(f"{current_dir}/scripts/tmp/links") |
| 322 | |
328 | |
| |
329 | if get_userconfig_value(user_config, "discord_presence") == True: |
| |
330 | from pypresence import Presence |
| |
331 | # print("imported pypresence") |
| |
332 | rpc = Presence(discord_client_id) |
| |
333 | rpc.connect() |
| |
334 | anime_image_url = get_anime_image(media_id) |
| |
335 | |
| 323 | while True: |
336 | while True: |
| 324 | |
337 | |
| 325 | try: |
338 | try: |
| @@ -328,28 +341,51 @@ while True: |
| 328 | start_video(links[0][1], salt, mpv_args) |
341 | start_video(links[0][1], salt, mpv_args) |
| 329 | mpv_socket_path = "/tmp/mpvsocket"+str(salt) |
342 | mpv_socket_path = "/tmp/mpvsocket"+str(salt) |
| 330 | connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - """+mpv_socket_path |
343 | connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - """+mpv_socket_path |
| 331 | mpv_pos_command = """echo '{ "command": ["get_property", "time-pos"] }' | socat - """+mpv_socket_path |
344 | is_paused = False |
| 332 | mpv_duration_command = """echo '{ "command": ["get_property", "duration"] }' | socat - """+mpv_socket_path |
| |
| 333 | |
| |
| 334 | while True: |
345 | while True: |
| 335 | time.sleep(2) |
346 | time.sleep(2) |
| 336 | result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True) |
347 | result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True) |
| 337 | # print(result) |
348 | # print(result) |
| 338 | if result.returncode == 0: |
349 | if result.returncode == 0: |
| 339 | output = result.stdout.strip() |
350 | output = result.stdout.strip() |
| 340 | |
| |
| 341 | if not output: # Check if output is empty |
351 | if not output: # Check if output is empty |
| 342 | print("No data received. Retrying...") |
352 | print("No data received. Retrying...") |
| 343 | try: |
353 | try: |
| 344 | data = json.loads(output) |
354 | data = json.loads(output) |
| 345 | if data["error"] == "success": |
355 | if data["error"] == "success": |
| 346 | playback_time = round(int(data["data"]), 2) |
356 | playback_time = round(int(data["data"]), 2) |
| |
357 | if get_userconfig_value(user_config, "discord_presence") == True: |
| |
358 | mpv_status = get_mpv_paused_status(mpv_socket_path) |
| |
359 | if mpv_status == True: |
| |
360 | is_paused = True |
| |
361 | else: |
| |
362 | is_paused = False |
| |
363 | |
| |
364 | # print("mpv_status", mpv_status) |
| |
365 | # print("is_paused", is_paused) |
| |
366 | playback_time_minutes = convert_seconds_to_minutes(playback_time) |
| |
367 | if is_paused: |
| |
368 | # Update presence with paused status |
| |
369 | rpc.update( |
| |
370 | details=f"Watching {title}", |
| |
371 | state=f"Episode {watching_ep} - {"{}:{}".format(playback_time_minutes[0], playback_time_minutes[1])} (Paused)", |
| |
372 | large_image=anime_image_url, |
| |
373 | large_text=title, |
| |
374 | ) |
| |
375 | else: |
| |
376 | # Update presence with playback time |
| |
377 | rpc.update( |
| |
378 | details=f"Watching {title}", |
| |
379 | state=f"Episode {watching_ep} - {playback_time_minutes[0]}:{playback_time_minutes[1]}/{duration}:00", |
| |
380 | large_image=anime_image_url, |
| |
381 | large_text=title, |
| |
382 | ) |
| |
383 | |
| 347 | update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title)) |
384 | update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title)) |
| 348 | # print("Playback time:", playback_time) |
385 | # print("Playback time:", playback_time) |
| 349 | watched_percentage = get_percentage_watched(mpv_socket_path) |
386 | watched_percentage = get_percentage_watched(mpv_socket_path) |
| 350 | mpv_playback_speed = get_mpv_playback_speed(mpv_socket_path) |
387 | mpv_playback_speed = get_mpv_playback_speed(mpv_socket_path) |
| 351 | # print("mpv speed", mpv_playback_speed) |
388 | |
| 352 | # print("watched percentage", watched_percentage) |
| |
| 353 | if watched_percentage > mark_episode_as_completed_at: |
389 | if watched_percentage > mark_episode_as_completed_at: |
| 354 | episode_completed = True |
390 | episode_completed = True |
| 355 | binge_watching = True |
391 | binge_watching = True |