Commit 721b228361

721b228361f53692befe00fc3d4bb4593c1f2076

parent: 3cabba3a70

Unsigned

Wraient <wraient.911@gmail.com> · 2024-09-24 20:10 UTC

Discord Presence add

Layout: unified · split

curd.py +46 −10
@@ -15,16 +15,17 @@ import subprocess
15import time 15import time
16import argparse 16import argparse
17 17
18from anilist import search_anime_by_title 18from anilist import get_anime_image, search_anime_by_title
19from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list 19from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list
20from select_link import load_links 20from select_link import load_links
21from start_video import start_video, send_command, get_percentage_watched, percentage_watched, get_mpv_playback_speed 21from start_video import get_mpv_paused_status, start_video, send_command, get_percentage_watched, percentage_watched, get_mpv_playback_speed
22from select_anime import load_anime_data 22from select_anime import load_anime_data
23from select_anime import extract_anime_info 23from select_anime import extract_anime_info
24from select_anime import select_anime 24from select_anime import select_anime
25from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime 25from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime
26 26
27mark_episode_as_completed_at = 85 27mark_episode_as_completed_at = 85
28discord_client_id = "1287457464148820089"
28 29
29def get_contents_of(tmp_file_name): 30def 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
90def convert_seconds_to_minutes(seconds):
91 minutes = seconds // 60
92 remaining_seconds = seconds % 60
93 return minutes, remaining_seconds
94
89def get_userconfig_value(userconfig:dict, key:str): 95def 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
171current_dir = Path(__file__).parent 177current_dir = Path(__file__).parent
172# current_dir = os.path.dirname(os.path.abspath(__file__)) 178# current_dir = os.path.dirname(os.path.abspath(__file__))
173print(current_dir) 179# print(current_dir)
174if not os.path.exists(f"{current_dir}/scripts/tmp/"): 180if 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))
309print(watching_ep) 315# print(watching_ep)
310# os.system("{current_dir}/scripts/episode_url.sh") 316# os.system("{current_dir}/scripts/episode_url.sh")
311run_script("episode_url") 317run_script("episode_url")
312 318
@@ -320,6 +326,13 @@ else:
320 326
321links = load_links(f"{current_dir}/scripts/tmp/links") 327links = load_links(f"{current_dir}/scripts/tmp/links")
322 328
329if 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
323while True: 336while 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
requirements +2 −1
@@ -1 +1,2 @@
1socat\ No newline at end of file 1socat
2pypresence\ No newline at end of file
start_video.py +7
@@ -37,6 +37,13 @@ def send_command(ipc_socket_path, command):
37 return None 37 return None
38 return None 38 return None
39 39
40def get_mpv_paused_status(ipc_socket_path):
41 status = send_command(ipc_socket_path, ["get_property", "pause"])
42 if status is not None:
43 return status
44 else:
45 return False
46
40def get_mpv_playback_speed(ipc_socket_path): 47def get_mpv_playback_speed(ipc_socket_path):
41 current_speed = send_command(ipc_socket_path, ["get_property", "speed"]) 48 current_speed = send_command(ipc_socket_path, ["get_property", "speed"])
42 if current_speed is not None: 49 if current_speed is not None: