2024-04-02 20:51:44 -04:00
|
|
|
#!/usr/bin/env python3
|
2024-04-05 15:54:40 -04:00
|
|
|
import configparser
|
2024-04-02 21:17:14 -04:00
|
|
|
import os
|
2024-04-03 13:27:00 -04:00
|
|
|
import shutil
|
2024-04-03 15:13:14 -04:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2024-04-03 14:17:51 -04:00
|
|
|
from datetime import datetime
|
2024-04-05 15:11:59 -04:00
|
|
|
from pathlib import Path
|
2024-04-08 12:49:28 -04:00
|
|
|
from typing import List
|
2024-04-08 12:56:56 -04:00
|
|
|
|
2024-04-07 15:57:03 -04:00
|
|
|
import requests
|
2024-04-03 15:13:14 -04:00
|
|
|
|
2024-04-08 12:49:28 -04:00
|
|
|
channel_list: List[str] = []
|
2024-04-03 16:55:05 -04:00
|
|
|
downloading = {}
|
|
|
|
|
2024-04-05 15:54:40 -04:00
|
|
|
# Default Config Settings
|
2024-04-07 15:47:37 -04:00
|
|
|
streamlink_location: str = "streamlink"
|
|
|
|
download_location: str = f"{Path.home()}/Downloads/Streams"
|
|
|
|
skip_ads: bool = False
|
2024-04-05 15:11:59 -04:00
|
|
|
|
|
|
|
|
2024-04-08 12:49:28 -04:00
|
|
|
def load_config() -> None:
|
2024-04-05 15:54:40 -04:00
|
|
|
print("Reading config file...")
|
2024-04-07 15:47:37 -04:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read("config.ini")
|
|
|
|
if (
|
|
|
|
config.has_option("settings", "streamlink_location")
|
|
|
|
and not config["settings"]["streamlink_location"].strip()
|
|
|
|
):
|
|
|
|
streamlink_location = config["settings"]["streamlink_location"]
|
|
|
|
print(f"Streamlink location: {streamlink_location}")
|
|
|
|
if (
|
|
|
|
config.has_option("settings", "download_location")
|
|
|
|
and not config["settings"]["download_location"].strip()
|
|
|
|
):
|
|
|
|
download_location = config["settings"]["download_location"]
|
|
|
|
print(f"Download location: {download_location}")
|
|
|
|
if config.has_option("settings", "skip_ads") and not config["settings"]["skip_ads"]:
|
|
|
|
skip_ads = bool(config["settings"]["skip_ads"])
|
|
|
|
print(f"Skip ads: {skip_ads}")
|
|
|
|
|
2024-04-08 16:34:28 -04:00
|
|
|
if len(config["streams"]) < 1:
|
|
|
|
sys.exit("ERROR: No streams found in config.ini! See README.md for more info.")
|
|
|
|
else:
|
|
|
|
for index in range(1, len(config["streams"]) + 1):
|
|
|
|
channel_list.append(config["streams"][str(index)])
|
|
|
|
|
2024-04-07 15:47:37 -04:00
|
|
|
print("Config file loaded")
|
2024-04-05 15:11:59 -04:00
|
|
|
|
2024-04-03 16:55:05 -04:00
|
|
|
|
2024-04-08 16:46:18 -04:00
|
|
|
# TODO: Log better and properly
|
2024-04-08 12:49:28 -04:00
|
|
|
def write_log(channel: str) -> None:
|
2024-04-03 18:00:04 -04:00
|
|
|
"""Writes the latest stdout of a process to log.txt"""
|
2024-04-03 16:55:05 -04:00
|
|
|
with open("log.txt", "a") as log:
|
|
|
|
line = downloading[channel].stdout.readline()
|
|
|
|
if line:
|
|
|
|
log.write(line.decode())
|
2024-04-02 20:51:44 -04:00
|
|
|
|
|
|
|
|
2024-04-08 12:49:28 -04:00
|
|
|
def download_stream(channel: str) -> None:
|
2024-04-03 18:00:04 -04:00
|
|
|
"""Downloads a given channel name in its own subprocess"""
|
2024-04-06 22:30:51 -04:00
|
|
|
# TODO: Just clean this up at somepoint
|
2024-04-08 12:56:56 -04:00
|
|
|
addtional_parms: str = ""
|
2024-04-05 16:55:42 -04:00
|
|
|
if skip_ads:
|
|
|
|
addtional_parms = "--twitch-proxy-playlist=https://lb-eu.cdn-perfprod.com,https://lb-eu2.cdn-perfprod.com,https://lb-na.cdn-perfprod.com,https://lb-as.cdn-perfprod.com,https://as.luminous.dev --twitch-disable-ads"
|
2024-04-08 12:56:56 -04:00
|
|
|
file_name: str = f"{channel}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.ts"
|
|
|
|
cmd: str = (
|
|
|
|
f"{streamlink_location} --loglevel none --retry-max 10 {addtional_parms} -o {download_location}/{channel}/{file_name} twitch.tv/{channel} best"
|
|
|
|
)
|
2024-04-06 22:30:51 -04:00
|
|
|
downloading[channel] = subprocess.Popen(
|
|
|
|
[cmd],
|
|
|
|
shell=True,
|
|
|
|
start_new_session=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
)
|
2024-04-03 16:55:05 -04:00
|
|
|
|
2024-04-02 20:51:44 -04:00
|
|
|
|
2024-04-08 12:49:28 -04:00
|
|
|
def check_system() -> None:
|
2024-04-03 18:00:04 -04:00
|
|
|
"""Makes sure everything is place for the script to run"""
|
2024-04-07 15:47:37 -04:00
|
|
|
|
|
|
|
# Checks for config file
|
|
|
|
if not os.path.exists("config.ini"):
|
|
|
|
sys.exit("ERROR: config.ini is not found! See README.md for more info.")
|
|
|
|
|
2024-04-05 16:55:42 -04:00
|
|
|
# Checks if streamlink is in the systems path
|
|
|
|
if not shutil.which("streamlink"):
|
|
|
|
sys.exit("ERROR: streamlink is not found in the systems path!")
|
2024-04-02 20:51:44 -04:00
|
|
|
|
2024-04-05 15:54:40 -04:00
|
|
|
# Make sure the download location exists
|
|
|
|
if not os.path.exists(download_location):
|
|
|
|
print(
|
|
|
|
f"Download destination does not exist.\n Creating now at {download_location}"
|
|
|
|
)
|
|
|
|
os.makedirs(download_location)
|
|
|
|
|
2024-04-03 18:00:04 -04:00
|
|
|
|
2024-04-08 12:49:28 -04:00
|
|
|
def stop_downloads() -> None:
|
2024-04-03 18:00:04 -04:00
|
|
|
"""Goes through every process and stops it if running"""
|
|
|
|
print("\nCleaning up...")
|
|
|
|
for name, proc in downloading.items():
|
|
|
|
proc.terminate()
|
2024-04-05 15:54:40 -04:00
|
|
|
print(f"Stopping download of {name}")
|
2024-04-03 13:27:00 -04:00
|
|
|
|
|
|
|
|
2024-04-08 12:49:28 -04:00
|
|
|
def main() -> None:
|
2024-04-03 18:00:04 -04:00
|
|
|
"""Main entry point of the app"""
|
|
|
|
|
|
|
|
# Run untill progam is killed
|
|
|
|
while True:
|
|
|
|
# Exits the program if there is no channels to grab
|
|
|
|
print("\n------------------------------------")
|
|
|
|
for channel in channel_list:
|
|
|
|
channel = channel.strip()
|
2024-04-05 16:55:42 -04:00
|
|
|
# TODO: Have steamlink itself check if the channel is live
|
2024-04-10 12:29:57 -04:00
|
|
|
# TODO: Have checking if channel is live in its own function
|
|
|
|
# TODO: If the request erros out just move on and not break the program
|
2024-04-03 18:00:04 -04:00
|
|
|
contents = requests.get("https://www.twitch.tv/" + channel).content.decode(
|
|
|
|
"utf-8"
|
|
|
|
)
|
|
|
|
if "isLiveBroadcast" in contents:
|
2024-04-05 15:54:40 -04:00
|
|
|
print(f"\033[1m{channel}\033[0m is \033[32mlive\033[0m!")
|
2024-04-03 18:00:04 -04:00
|
|
|
if channel not in downloading:
|
|
|
|
download_stream(channel)
|
|
|
|
else:
|
2024-04-10 12:29:57 -04:00
|
|
|
# TODO: Format this and dont make its own line
|
2024-04-05 17:02:58 -04:00
|
|
|
print(f"{channel} is already downloading")
|
2024-04-08 16:46:18 -04:00
|
|
|
# write_log(channel)
|
2024-04-02 20:51:44 -04:00
|
|
|
else:
|
2024-04-03 18:00:04 -04:00
|
|
|
print("\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.")
|
|
|
|
if channel in downloading:
|
|
|
|
del downloading[channel]
|
2024-04-10 12:29:57 -04:00
|
|
|
# TODO: Format this and dont make its own line
|
2024-04-05 15:54:40 -04:00
|
|
|
print(f"{channel} is no longer downloading")
|
2024-04-03 18:00:04 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-04-07 15:47:37 -04:00
|
|
|
"""This is executed when run from the command line"""
|
2024-04-03 18:00:04 -04:00
|
|
|
try:
|
|
|
|
check_system()
|
2024-04-07 15:47:37 -04:00
|
|
|
load_config()
|
2024-04-03 18:00:04 -04:00
|
|
|
main()
|
|
|
|
finally:
|
|
|
|
stop_downloads()
|