Compare commits

...

9 Commits

Author SHA1 Message Date
1ce273bc61 Disable broken logging 2024-04-08 16:46:18 -04:00
65efcbb171 Removed unused conditonals 2024-04-08 16:44:39 -04:00
e07dd0c5cd Now read the channel list from the config.ini 2024-04-08 16:34:28 -04:00
33d7ae24fb More clean up with type hints 2024-04-08 12:56:56 -04:00
aaf2173894 More clean up with type hints 2024-04-08 12:49:28 -04:00
4218dc827f Clean up 2024-04-07 15:57:03 -04:00
660b7f1e82 Clean up 2024-04-07 15:55:49 -04:00
ed2f65bee5 Should maybe make requests a requirement 2024-04-06 23:47:07 -04:00
18c0f2123d Fixed stream saved location 2024-04-06 22:44:51 -04:00
5 changed files with 106 additions and 60 deletions

4
.gitignore vendored
View File

@ -159,8 +159,8 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Channel List
channel_list.txt
# Configuration
config.ini
# dl-stream log
log.txt

View File

@ -2,12 +2,49 @@
<!--toc:start-->
- [Auto TTV Grabber](#auto-ttv-grabber)
- [Getting started](#getting-started)
- [tl;dr](#tldr)
- [Configuration](#configuration)
- [Settings](#settings)
- [Adding streams](#adding-streams)
<!--toc:end-->
## Getting started
## tl;dr
This script relies on [streamlink](https://streamlink.github.io) being in your systems path.
This script relies on [streamlink](https://streamlink.github.io) being installed.
You can find all of its requirements on the their project page.
This script loops through all of the channels in `channel_list.txt` once per minute checking to see if the channel is live via a HTTP request.
Once a channel is live a [streamlink](https://streamlink.github.io) subprocess spawns in the background downloading the stream to it's default location (`$HOME/Videos/Stream/<channel_name>`)
This script loops through all of the channels in `config.ini`,
once per minute checking to see if the channel is live via a HTTP request.
Once a channel is live a `streamlink` subprocess spawns in the background,
downloading the stream to it's default location (`$HOME/Downloads/Stream/<channel_name>`)
If you wish to change this location, you can find out how to below
## Configuration
You can copy `config.ini.example` from the repo to `config.ini` to begin.
Leaving a setting blank will render it being ignored and using the default value.
### Settings
- `streamlink_location`: If `streamlink` is not in your path you can specify
its absolute location here. Otherwise it looks for it in your path.
- `download_location`: You can specify the absolute path to where you would like
to save the steams. A subdirectory will be created for each channel as it goes live.
If left blank the default location is: `$HOME/Downloads/Streams/<channel_name>`
- `skip_ads` : Takes a `True` or `False` value. If `True` you will need to have
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`
### Adding streams
You will need to add the channels Twitch username to `config.ini`:
```ini
[streams]
1 = coney
2 = dougdoug
3 = parkzer
```
As for as I know there is no limit to how many streams you can add this way,
just make sure when you add a new stream it is incremented by one

7
config.ini.example Normal file
View File

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

103
main.py
View File

@ -7,43 +7,50 @@ import sys
import time
from datetime import datetime
from pathlib import Path
from typing import List
import requests
channel_list = []
channel_list: List[str] = []
downloading = {}
# Default Config Settings
config = configparser.ConfigParser()
streamlink_location = "streamlink"
download_location = f"{Path.home()}/Downloads/Streams"
skip_ads = False
streamlink_location: str = "streamlink"
download_location: str = f"{Path.home()}/Downloads/Streams"
skip_ads: bool = False
def load_config():
def load_config() -> None:
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")
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}")
if len(config["streams"]) < 1:
sys.exit("ERROR: No streams found in config.ini! See README.md for more info.")
else:
print("No config file found using default values!")
for index in range(1, len(config["streams"]) + 1):
channel_list.append(config["streams"][str(index)])
print("Config file loaded")
def write_log(channel):
# 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()
@ -51,14 +58,16 @@ def write_log(channel):
log.write(line.decode())
def download_stream(channel):
def download_stream(channel: str) -> None:
"""Downloads a given channel name in its own subprocess"""
# TODO: Just clean this up at somepoint
addtional_parms = ""
addtional_parms: str = ""
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"
file_name = f"{channel}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.ts"
cmd = f"{streamlink_location} --loglevel none --retry-max 10 {addtional_parms} -o {download_location}/{channel}/{file_name} twitch.tv/{channel} best"
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"
)
downloading[channel] = subprocess.Popen(
[cmd],
shell=True,
@ -66,23 +75,19 @@ def download_stream(channel):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
write_log(channel)
def check_system():
def check_system() -> None:
"""Makes sure everything is place for the script to run"""
# 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.")
# Checks if streamlink is in the systems path
if not shutil.which("streamlink"):
sys.exit("ERROR: streamlink is not found in the systems path!")
# TODO: Combine the channel_list into the config
# Checks if the channel_list exists and if not makes one
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!")
# Make sure the download location exists
if not os.path.exists(download_location):
print(
@ -91,7 +96,7 @@ def check_system():
os.makedirs(download_location)
def stop_downloads():
def stop_downloads() -> None:
"""Goes through every process and stops it if running"""
print("\nCleaning up...")
for name, proc in downloading.items():
@ -99,20 +104,12 @@ def stop_downloads():
print(f"Stopping download of {name}")
def main():
def main() -> None:
"""Main entry point of the app"""
# 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
# Run untill progam is killed
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()
@ -126,7 +123,7 @@ def main():
download_stream(channel)
else:
print(f"{channel} is already downloading")
write_log(channel)
# write_log(channel)
else:
print("\033[1m" + channel + "\033[0m is \033[31mnot live\033[0m.")
if channel in downloading:
@ -141,10 +138,10 @@ def main():
if __name__ == "__main__":
""" This is executed when run from the command line """
"""This is executed when run from the command line"""
try:
load_config()
check_system()
load_config()
main()
finally:
stop_downloads()

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.2.1