Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: split main.py into multiple functions #14

Merged
merged 1 commit into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading