#!/usr/bin/env python3 import os import shutil import subprocess import sys import time from datetime import datetime import requests channel_list = [] 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): downloading[channel] = subprocess.Popen( ["dl-stream", "-r", channel], start_new_session=True, 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 if not shutil.which("dl-stream"): sys.exit("ERROR: dl-stream is not found in the systems path!") # Checks if the channel_list exists and if not makes one if os.path.exists("channel_list.txt"): # Grab all the channels from channel_list.txt and put them in a list with open("channel_list.txt", "r") as file: # channel_list = file.readlines() channel_list = [ line for line in file if line.strip() ] # 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 # TODO: Make it so user can kill gracefully # TODO: This includes all the subprocess too! while True: # Exits the program if there is no channels to Grab if not channel_list: sys.exit("Please populate the channel_list.txt with one channel per line!") print("\n------------------------------------") for channel in channel_list: channel = channel.strip() contents = requests.get("https://www.twitch.tv/" + channel).content.decode( "utf-8" ) if "isLiveBroadcast" in contents: print("\033[1m" + channel + "\033[0m is \033[32mlive\033[0m!") if channel not in downloading: download_stream(channel) else: print(channel + " is already downloading") write_log(channel) else: print("\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.") if channel in downloading: del downloading[channel] print(channel + " is no longer downloading") 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("------------------------------------") time.sleep(60) # Wait 60 Seconds before trying again