Skip to content

Commit

Permalink
Merge pull request #14 from zeroquinc:main-optimizations
Browse files Browse the repository at this point in the history
chore: split main.py into multiple functions
  • Loading branch information
zeroquinc authored May 4, 2024
2 parents fef7049 + 8d5fe95 commit 8c25e1b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
from src.custom_logger import logger
from src.rpc import fetch_info, fetch_length, update_rp

"""
This file contains the main function of the program. Run this to start the program.
"""
def get_session_info(session):
return fetch_info(session), fetch_length(session)

def should_update_rp(info, length, last_info, last_length, last_time):
return info != last_info or length != last_length or (length['result']['speed'] == 1 and length['result']['time'] != last_time)

def update_and_sleep(info, length, last_info, last_length, last_time):
if should_update_rp(info, length, last_info, last_length, last_time):
update_rp(info, length)
last_info, last_length, last_time = info, length, length['result']['time']
time.sleep(3)
return last_info, last_length, last_time

# Main function
def main():
try:
with requests.Session() as session:
last_info, last_length, last_time = None, None, None
while True:
info = fetch_info(session)
length = fetch_length(session)
if info is None or length is None: # If either info or length is None, continue to the next iteration
info, length = get_session_info(session)
if None in (info, length):
time.sleep(3)
continue
if info != last_info or length != last_length or (length['result']['speed'] == 1 and length['result']['time'] == last_time):
update_rp(info, length) # Update the RP if there's new information
last_info, last_length, last_time = info, length, last_time # Update the last info and length
time.sleep(3) # Always pause for 3 seconds between iterations
last_info, last_length, last_time = update_and_sleep(info, length, last_info, last_length, last_time)
except KeyboardInterrupt:
logger.info("Program interrupted by user. Exiting...")

Expand Down

0 comments on commit 8c25e1b

Please sign in to comment.