Start to read the config file

This commit is contained in:
Rain Clark 2024-04-05 15:54:40 -04:00
parent 3ed0035dd7
commit 3957eb40ca
2 changed files with 41 additions and 7 deletions

5
config.ini Normal file
View File

@ -0,0 +1,5 @@
[settings]
streamlink_location = ""
download_location = ""
skip_ads =

43
main.py
View File

@ -1,25 +1,47 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import configparser
import os import os
import shutil import shutil
import subprocess import subprocess
import sys import sys
import time import time
from datetime import datetime from datetime import datetime
import configparser
from pathlib import Path from pathlib import Path
import requests import requests
channel_list = [] channel_list = []
downloading = {} downloading = {}
# Config Settings # Default Config Settings
config = configparser.ConfigParser()
streamlink_location = "streamlink" streamlink_location = "streamlink"
download_location = str(f"{Path.home()}/Downloads/Streams/") download_location = str(f"{Path.home()}/Downloads/Streams/")
skip_ads = False skip_ads = False
def load_config(): def load_config():
pass print("Reading config file...")
if os.path.exists("config.ini"):
config.read("config.ini")
if (
config.has_option("settings", "streamlink_location")
and not config["settings"]["streamlink_location"].strip()
):
streamlink_location = config["settings"]["streamlink_location"]
if (
config.has_option("settings", "download_location")
and not config["settings"]["download_location"].strip()
):
download_location = config["settings"]["download_location"]
if (
config.has_option("settings", "skip_ads")
and not config["settings"]["skip_ads"]
):
skip_ads = bool(config["settings"]["skip_ads"])
print("Config file loaded")
else:
print("No config file found using default values!")
def write_log(channel): def write_log(channel):
@ -55,13 +77,20 @@ def check_system():
pass # Creates empty file pass # Creates empty file
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!")
# 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)
def stop_downloads(): def stop_downloads():
"""Goes through every process and stops it if running""" """Goes through every process and stops it if running"""
print("\nCleaning up...") print("\nCleaning up...")
for name, proc in downloading.items(): for name, proc in downloading.items():
proc.terminate() proc.terminate()
print("Stopping download of " + name) print(f"Stopping download of {name}")
def main(): def main():
@ -85,17 +114,17 @@ def main():
"utf-8" "utf-8"
) )
if "isLiveBroadcast" in contents: if "isLiveBroadcast" in contents:
print("\033[1m" + channel + "\033[0m is \033[32mlive\033[0m!") print(f"\033[1m{channel}\033[0m is \033[32mlive\033[0m!")
if channel not in downloading: if channel not in downloading:
download_stream(channel) download_stream(channel)
else: else:
print(channel + " is already downloading") print("f{channel} is already downloading")
write_log(channel) 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:
del downloading[channel] del downloading[channel]
print(channel + " is no longer downloading") print(f"{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( print(
"\n\033[3mLast checked: " + datetime.now().strftime("%H:%M:%S") + "\033[0m" "\n\033[3mLast checked: " + datetime.now().strftime("%H:%M:%S") + "\033[0m"