2021-09-29 23:55:23 +02:00
|
|
|
from urllib.parse import urlparse, quote
|
|
|
|
from pathlib import Path
|
2021-09-30 15:19:14 +02:00
|
|
|
import os.path
|
|
|
|
import os
|
2021-09-29 23:55:23 +02:00
|
|
|
import configparser
|
|
|
|
import subprocess
|
2021-09-30 15:19:14 +02:00
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
import sys
|
2021-09-29 23:55:23 +02:00
|
|
|
|
|
|
|
def read_config():
|
|
|
|
config = configparser.RawConfigParser()
|
|
|
|
config.read("config.ini")
|
|
|
|
return config
|
|
|
|
|
|
|
|
def get_mixes(mixes_dir):
|
|
|
|
mixes = []
|
|
|
|
for path in Path(mixes_dir).rglob('url.txt'):
|
|
|
|
with open(path, "r") as fd:
|
|
|
|
first_line = fd.readline()
|
|
|
|
first_line = first_line.strip()
|
|
|
|
url = quote(urlparse(first_line).geturl(), safe=":/") # do a sanity url parse and quote
|
|
|
|
mixes.append((path.parent, url))
|
|
|
|
return mixes
|
|
|
|
|
|
|
|
def download_channel(target_directory, channel_url, options="", update_mpd=False, mpd_root=""):
|
|
|
|
mpd_update_options = []
|
|
|
|
if update_mpd:
|
|
|
|
mpd_update_options = ["--exec", "mpc update \"{}\"".format(str(target_directory).replace(mpd_root, "", 1).lstrip("/"))]
|
|
|
|
p = subprocess.Popen(["youtube-dl", *options.split(" "), *mpd_update_options, channel_url], cwd=target_directory)
|
|
|
|
p.wait()
|
|
|
|
|
2021-09-30 15:19:14 +02:00
|
|
|
def lock_downloader():
|
|
|
|
if os.path.isfile("lockfile"):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
with open("lockfile", "w") as fd:
|
|
|
|
print("Creating lockfile")
|
|
|
|
timestamp = int(datetime.datetime.now().timestamp())
|
|
|
|
fd.write(str(timestamp) + "\n")
|
|
|
|
return True
|
|
|
|
|
|
|
|
def unlock_downloader():
|
|
|
|
os.remove("lockfile")
|
|
|
|
|
|
|
|
|
|
|
|
lock_success = lock_downloader()
|
|
|
|
if not lock_success:
|
|
|
|
with open("lockfile", "r") as fd:
|
|
|
|
timestamp = fd.readline().strip()
|
|
|
|
time = datetime.datetime.fromtimestamp(int(timestamp))
|
|
|
|
print("Downloader already running. It was started at {}".format(time))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-09-29 23:55:23 +02:00
|
|
|
config = read_config()
|
|
|
|
mixes = get_mixes(config["general"]["mixes_dir"])
|
|
|
|
for folder,url in mixes:
|
|
|
|
download_channel(folder, url, config["youtube-dl"]["options"], update_mpd=config["mpd"]["update"], mpd_root=config["mpd"]["mpd_root"])
|
2021-09-30 15:19:14 +02:00
|
|
|
print("Download operation finished. Releasing lockfile now.")
|
|
|
|
unlock_downloader()
|