Begin to fix logging

This commit is contained in:
Rain Clark 2024-04-03 16:55:05 -04:00
parent 6f1f595484
commit 9e7d79c6a5

26
main.py
View File

@ -9,17 +9,28 @@ from datetime import datetime
import requests import requests
channel_list = [] channel_list = []
downloading = [] downloading = {}
def write_log(channel):
with open("log.txt", "a") as log:
line = downloading[channel].stdout.readline()
if line:
log.write(line.decode())
def download_stream(channel): def download_stream(channel):
downloading.append(channel) downloading[channel] = subprocess.Popen(
with open("log.txt", "w") as log: ["dl-stream", "-r", channel],
subprocess.Popen( start_new_session=True,
["dl-stream", "-r", channel], start_new_session=True, stdout=log stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
) )
write_log(channel)
# TODO: Clean things up intp their own method
# Checks if dl-stream is in the systems path # Checks if dl-stream is in the systems path
if not shutil.which("dl-stream"): if not shutil.which("dl-stream"):
sys.exit("ERROR: dl-stream is not found in the systems path!") sys.exit("ERROR: dl-stream is not found in the systems path!")
@ -40,6 +51,8 @@ else:
sys.exit("Please populate the channel_list.txt with one channel per line!") sys.exit("Please populate the channel_list.txt with one channel per line!")
# Run untill progam is killed # Run untill progam is killed
# TODO: Make it so user can kill gracefully
# TODO: This includes all the subprocess too!
while True: while True:
# Exits the program if there is no channels to Grab # Exits the program if there is no channels to Grab
if not channel_list: if not channel_list:
@ -56,10 +69,11 @@ while True:
download_stream(channel) download_stream(channel)
else: else:
print(channel + " is already downloading") print(channel + " is already downloading")
write_log(channel)
else: else:
print("\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.") print("\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.")
if channel in downloading: if channel in downloading:
downloading.remove(channel) del downloading[channel]
print(channel + " is no longer downloading") print(channel + " is no longer downloading")
time.sleep(1) # Wait one second before going to next channel time.sleep(1) # Wait one second before going to next channel
print("\n\033[3mLast checked: " + datetime.now().strftime("%H:%M:%S") + "\033[0m") print("\n\033[3mLast checked: " + datetime.now().strftime("%H:%M:%S") + "\033[0m")