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
1515import time
1616import argparse
1717
18from anilist import search_anime_by_title
18from anilist import get_anime_image, search_anime_by_title
1919from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list
2020from 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
2222from select_anime import load_anime_data
2323from select_anime import extract_anime_info
2424from select_anime import select_anime
2525from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime
2626
2727mark_episode_as_completed_at = 85
28discord_client_id = "1287457464148820089"
2829
2930def get_contents_of(tmp_file_name):
3031 with open(f"{current_dir}/scripts/tmp/{tmp_file_name}", "r") as temp_file:
@@ -86,6 +87,11 @@ default_config = {
8687 "presence_script_path":"curddiscordpresence.py"
8788}
8889
90def convert_seconds_to_minutes(seconds):
91 minutes = seconds // 60
92 remaining_seconds = seconds % 60
93 return minutes, remaining_seconds
94
8995def get_userconfig_value(userconfig:dict, key:str):
9096 return userconfig.get(key, default_config.get(key))
9197
@@ -170,7 +176,7 @@ def load_config() -> dict:
170176# START OF SCRIPT
171177current_dir = Path(__file__).parent
172178# current_dir = os.path.dirname(os.path.abspath(__file__))
173print(current_dir)
179# print(current_dir)
174180if not os.path.exists(f"{current_dir}/scripts/tmp/"):
175181 try:
176182 os.makedirs(os.path.dirname(f"{current_dir}/scripts/tmp/"))
@@ -306,7 +312,7 @@ else: # if history does not exist
306312 progress = last_episode - 1
307313 watching_ep = int(progress)+1
308314 write_to_tmp("ep_no", str(watching_ep))
309print(watching_ep)
315# print(watching_ep)
310316# os.system("{current_dir}/scripts/episode_url.sh")
311317run_script("episode_url")
312318
@@ -320,6 +326,13 @@ else:
320326
321327links = load_links(f"{current_dir}/scripts/tmp/links")
322328
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
323336while True:
324337
325338 try:
@@ -328,28 +341,51 @@ while True:
328341 start_video(links[0][1], salt, mpv_args)
329342 mpv_socket_path = "/tmp/mpvsocket"+str(salt)
330343 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
332 mpv_duration_command = """echo '{ "command": ["get_property", "duration"] }' | socat - """+mpv_socket_path
333
344 is_paused = False
334345 while True:
335346 time.sleep(2)
336347 result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True)
337348 # print(result)
338349 if result.returncode == 0:
339350 output = result.stdout.strip()
340
341351 if not output: # Check if output is empty
342352 print("No data received. Retrying...")
343353 try:
344354 data = json.loads(output)
345355 if data["error"] == "success":
346356 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
347384 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))
348385 # print("Playback time:", playback_time)
349386 watched_percentage = get_percentage_watched(mpv_socket_path)
350387 mpv_playback_speed = get_mpv_playback_speed(mpv_socket_path)
351 # print("mpv speed", mpv_playback_speed)
352 # print("watched percentage", watched_percentage)
388
353389 if watched_percentage > mark_episode_as_completed_at:
354390 episode_completed = True
355391 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):
3737 return None
3838 return None
3939
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
4047def get_mpv_playback_speed(ipc_socket_path):
4148 current_speed = send_command(ipc_socket_path, ["get_property", "speed"])
4249 if current_speed is not None: