Compare commits

...

6 Commits

Author SHA1 Message Date
6030f2d0dd Split up add params 2024-04-12 12:53:39 -04:00
c7782b46b8 Delete config.ini 2024-04-10 20:53:28 +00:00
5772f1be4b Now actually read the config file. 😅 2024-04-10 16:49:00 -04:00
d3a222e27b Better way to handle logging 2024-04-10 16:44:57 -04:00
5dd3e92a5b Formatting & Refactoring 2024-04-10 13:35:42 -04:00
0d53c8c292 Added notes on what to work on 2024-04-10 12:29:57 -04:00
4 changed files with 49 additions and 37 deletions

View File

@ -34,6 +34,9 @@ If left blank the default location is: `$HOME/Downloads/Streams/<channel_name>`
installed the [ttvlol](https://github.com/2bc4/streamlink-ttvlol) `streamlink`
plugin. Setting this to `True` without doing so will cause the
`streamlink` subprocess to fail. Default value is `False`
- `log` : Takes a `True` or `False` value. If `True` there will be a `log.txt` file
in the channel's download directory with all of the output that came from its
corresponding subprocess. Default value is `False`
### Adding streams

View File

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

View File

@ -2,6 +2,7 @@
streamlink_location = ""
download_location = ""
skip_ads =
log =
[streams]

77
main.py
View File

@ -18,27 +18,25 @@ downloading = {}
streamlink_location: str = "streamlink"
download_location: str = f"{Path.home()}/Downloads/Streams"
skip_ads: bool = False
log: bool = False
def load_config() -> None:
print("Reading config file...")
config = configparser.ConfigParser()
config.read("config.ini")
if (
config.has_option("settings", "streamlink_location")
and not config["settings"]["streamlink_location"].strip()
):
if config.has_option("settings", "streamlink_location"):
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()
):
if config.has_option("settings", "download_location"):
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"]:
if config.has_option("settings", "skip_ads"):
skip_ads = bool(config["settings"]["skip_ads"])
print(f"Skip ads: {skip_ads}")
if config.has_option("settings", "log"):
log = bool(config["settings"]["log"])
print(f"Logs: {log}")
if len(config["streams"]) < 1:
sys.exit("ERROR: No streams found in config.ini! See README.md for more info.")
@ -49,25 +47,38 @@ def load_config() -> None:
print("Config file loaded")
# TODO: Log better and properly
def write_log(channel: str) -> None:
"""Writes the latest stdout of a process to log.txt"""
with open("log.txt", "a") as log:
line = downloading[channel].stdout.readline()
if line:
log.write(line.decode())
# TODO: Have steamlink itself check if the channel is live
def is_live(channel: str) -> bool:
"""Checks if a channel is live on Twitch"""
try:
contents = requests.get("https://www.twitch.tv/" + channel).content.decode(
"utf-8"
)
if "isLiveBroadcast" in contents:
return True
else:
return False
except Exception:
print(f"There was an issue checking if {channel} was life. Will try next time!")
if channel in downloading:
return True
else:
return False
def download_stream(channel: str) -> None:
"""Downloads a given channel name in its own subprocess"""
# TODO: Just clean this up at somepoint
addtional_parms: str = ""
loging: str = ""
ad_skipping: str = ""
print(log)
if log:
loging = f"--loglevel info --logfile {download_location}/{channel}/log.txt"
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"
ad_skipping = "--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"
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"
)
cmd: str = f"{streamlink_location} --retry-max 10 {loging} {ad_skipping} -o {download_location}/{channel}/{file_name} twitch.tv/{channel} best"
# TODO: Check if the process failed for some reason
downloading[channel] = subprocess.Popen(
[cmd],
shell=True,
@ -108,30 +119,32 @@ def main() -> None:
"""Main entry point of the app"""
# Run untill progam is killed
# TODO: Check on the process if it is still alive and restart as needed
while True:
# Exits the program if there is no channels to grab
print("\n------------------------------------")
for channel in channel_list:
channel = channel.strip()
# TODO: Have steamlink itself check if the channel is live
contents = requests.get("https://www.twitch.tv/" + channel).content.decode(
"utf-8"
)
if "isLiveBroadcast" in contents:
print(f"\033[1m{channel}\033[0m is \033[32mlive\033[0m!")
if is_live(channel):
print(f"\n\033[1m{channel}\033[0m is \033[32mlive\033[0m!", end=" ")
if channel not in downloading:
download_stream(channel)
else:
print(f"{channel} is already downloading")
# write_log(channel)
print("\033[33m(Already Downloading)\033[0m")
else:
print("\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.")
print(
"\n\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.",
end=" ",
)
if channel in downloading:
del downloading[channel]
print(f"{channel} is no longer downloading")
print("\033[35m(Stopping)\033[0m")
time.sleep(1) # Wait one second before going to next channel
print(
"\n\033[3mLast checked: " + datetime.now().strftime("%H:%M:%S") + "\033[0m"
"\n\n\033[3mLast checked: "
+ datetime.now().strftime("%H:%M:%S")
+ "\033[0m"
)
print("------------------------------------")
time.sleep(60) # Wait 60 Seconds before trying again