From 42b617e5dbec1bb688c1044fb95fc1233ef2779d Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 22 Apr 2021 16:19:21 +0200 Subject: [PATCH] Add 'fetch.py' --- fetch.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 fetch.py diff --git a/fetch.py b/fetch.py new file mode 100644 index 0000000..2f352f9 --- /dev/null +++ b/fetch.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +from concurrent.futures import ThreadPoolExecutor, as_completed +import re + +import youtube_dl + + +def main(): + reg = re.compile(r"^\S+)>?\s(?P.*)$") + with open("00_todownload.md", "r") as f: + lines = filter(lambda x: bool(x), map(lambda x: x.strip(), f.readlines())) + + with ThreadPoolExecutor() as executor: + promises = {executor.submit(download, line, reg): line for line in lines} + for future in as_completed(promises): + line = promises[future] + print(line, future.result()) + + +def download(line, reg): + options = { + "format": "bestaudio/best", + 'postprocessors': [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }], + } + groups = reg.match(line) + print(f"start download of {groups['name']}") + options["outtmpl"] = groups["name"] + ".%(ext)s" + with youtube_dl.YoutubeDL(options) as ydl: + ydl.download([groups["url"]]) + print(f"finished download of {groups['name']}") + + +if __name__ == "__main__": + main() +