From 3ff32c60eba416e35cfb60825809bf8b301a5850 Mon Sep 17 00:00:00 2001 From: zeroquinc Date: Fri, 26 Jan 2024 00:59:57 +0100 Subject: [PATCH] Refactor code for updating RP and calculating end time --- main.py | 13 +++++++------ src/rpc.py | 12 ++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 1ceaa5e..04d581d 100644 --- a/main.py +++ b/main.py @@ -16,12 +16,13 @@ def main(): while True: info = fetch_info(session) length = fetch_length(session) - if info is not None and length is not None: # Check if info and length are not None because if they are, it means Kodi is not playing anything - if info != last_info or length != last_length: - update_rp(info, length) # Update the RP if there's new information - last_info, last_length = info, length # Update the last info and length - else: - time.sleep(3) # Pause for 3 seconds if there's no new information + if info is None or length is None: # If either info or length is None, continue to the next iteration + time.sleep(3) + continue + if info != last_info or length != last_length: + update_rp(info, length) # Update the RP if there's new information + last_info, last_length = info, length # Update the last info and length + time.sleep(3) # Always pause for 3 seconds between iterations except KeyboardInterrupt: logger.info("Program interrupted by user. Exiting...") diff --git a/src/rpc.py b/src/rpc.py index ed38cf7..93a7c2d 100644 --- a/src/rpc.py +++ b/src/rpc.py @@ -113,9 +113,6 @@ def set_rp(info, length): return if previous_info == info and previous_speed == length['speed']: # Check if both info and speed are the same as before to prevent unnecessary updates return - if info['type'] == 'unknown': # Check if the media type is unknown to prevent unnecessary updates - clear_rpc_if_unknown(info, length, start_time, end_time, None, None, None, None, None) - return logger.debug(f"Retrieved info: {info}") @@ -241,7 +238,14 @@ def calculate_start_time(length): # Function to calculate the end time of a media def calculate_end_time(start_time, length): start_time = datetime.fromtimestamp(start_time) - return (start_time + timedelta(hours=length['totaltime']['hours'], minutes=length['totaltime']['minutes'], seconds=length['totaltime']['seconds'])).timestamp() + end_time = (start_time + timedelta(hours=length['totaltime']['hours'], minutes=length['totaltime']['minutes'], seconds=length['totaltime']['seconds'])).timestamp() + + # If start_time and end_time are the same, fetch length again + while start_time == end_time: + length = fetch_length() # Assuming fetch_length() is a function that fetches the length + end_time = (start_time + timedelta(hours=length['totaltime']['hours'], minutes=length['totaltime']['minutes'], seconds=length['totaltime']['seconds'])).timestamp() + + return end_time # Function to create the buttons for a media def create_buttons(imdb_url, letterboxd_url, tmdb_url, trakt_url):