Skip to content

Commit

Permalink
Refactor code for updating RP and calculating end time
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroquinc committed Jan 25, 2024
1 parent 1ff3b9b commit 3ff32c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
13 changes: 7 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")

Expand Down
12 changes: 8 additions & 4 deletions src/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3ff32c6

Please sign in to comment.