mirror of
https://gitgud.io/Melon__Bread/auto-ttv-grabber.git
synced 2024-11-25 00:38:35 -05:00
Refactored & kills subprocess on exit
This commit is contained in:
parent
4edf3ffdf2
commit
2dc8ac2af7
48
main.py
48
main.py
@ -13,6 +13,7 @@ downloading = {}
|
|||||||
|
|
||||||
|
|
||||||
def write_log(channel):
|
def write_log(channel):
|
||||||
|
"""Writes the latest stdout of a process to log.txt"""
|
||||||
with open("log.txt", "a") as log:
|
with open("log.txt", "a") as log:
|
||||||
line = downloading[channel].stdout.readline()
|
line = downloading[channel].stdout.readline()
|
||||||
if line:
|
if line:
|
||||||
@ -20,6 +21,7 @@ def write_log(channel):
|
|||||||
|
|
||||||
|
|
||||||
def download_stream(channel):
|
def download_stream(channel):
|
||||||
|
"""Downloads a given channel name in its own subprocess"""
|
||||||
downloading[channel] = subprocess.Popen(
|
downloading[channel] = subprocess.Popen(
|
||||||
["dl-stream", "-r", channel],
|
["dl-stream", "-r", channel],
|
||||||
start_new_session=True,
|
start_new_session=True,
|
||||||
@ -29,32 +31,41 @@ def download_stream(channel):
|
|||||||
write_log(channel)
|
write_log(channel)
|
||||||
|
|
||||||
|
|
||||||
# TODO: Clean things up intp their own method
|
def check_system():
|
||||||
|
"""Makes sure everything is place for the script to run"""
|
||||||
|
# TODO: Make it fallback to streamlink if dl-stream not present
|
||||||
# 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!")
|
||||||
|
|
||||||
|
|
||||||
# Checks if the channel_list exists and if not makes one
|
# Checks if the channel_list exists and if not makes one
|
||||||
if os.path.exists("channel_list.txt"):
|
if not os.path.exists("channel_list.txt"):
|
||||||
|
print("ERROR:'channel_list.txt' does not exist, creating now!")
|
||||||
|
with open("channel_list.txt", "w"):
|
||||||
|
pass # Creates empty file
|
||||||
|
sys.exit("Please populate the channel_list.txt with one channel per line!")
|
||||||
|
|
||||||
|
|
||||||
|
def stop_downloads():
|
||||||
|
"""Goes through every process and stops it if running"""
|
||||||
|
print("\nCleaning up...")
|
||||||
|
for name, proc in downloading.items():
|
||||||
|
proc.terminate()
|
||||||
|
print("Stopping download of " + name)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point of the app"""
|
||||||
# Grab all the channels from channel_list.txt and put them in a list
|
# Grab all the channels from channel_list.txt and put them in a list
|
||||||
with open("channel_list.txt", "r") as file:
|
with open("channel_list.txt", "r") as file:
|
||||||
# channel_list = file.readlines()
|
# channel_list = file.readlines()
|
||||||
channel_list = [
|
channel_list = [
|
||||||
line for line in file if line.strip()
|
line for line in file if line.strip()
|
||||||
] # Removes all white spaces per line
|
] # Removes all white spaces per line
|
||||||
else:
|
|
||||||
print("ERROR:'channel_list.txt' does not exist, creating now!")
|
|
||||||
with open("channel_list.txt", "w") as file:
|
|
||||||
pass # Writes Nothing
|
|
||||||
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:
|
||||||
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!")
|
||||||
print("\n------------------------------------")
|
print("\n------------------------------------")
|
||||||
@ -76,6 +87,17 @@ while True:
|
|||||||
del downloading[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"
|
||||||
|
)
|
||||||
print("------------------------------------")
|
print("------------------------------------")
|
||||||
time.sleep(60) # Wait 60 Seconds before trying again
|
time.sleep(60) # Wait 60 Seconds before trying again
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
""" This is executed when run from the command line """
|
||||||
|
try:
|
||||||
|
check_system()
|
||||||
|
main()
|
||||||
|
finally:
|
||||||
|
stop_downloads()
|
||||||
|
Loading…
Reference in New Issue
Block a user