diff options
| author | Brendan F <EpicWolverine@users.noreply.github.com> | 2023-05-14 23:12:27 -0700 |
|---|---|---|
| committer | Brendan F <EpicWolverine@users.noreply.github.com> | 2023-05-14 23:12:27 -0700 |
| commit | 737344a72d23dc97b0d0e73cc4ab7fdffd0fbf49 (patch) | |
| tree | 2a915b59ab29ac79012ca3345999d9e23562d1f9 /playlist_generator | |
| parent | b19a001171bd8197a30f397091d67eba5e4c1111 (diff) | |
Merge in react app code
From sluchajfun and youtube-heardle-template
Diffstat (limited to 'playlist_generator')
| -rw-r--r-- | playlist_generator/playlist_generator.py | 80 | ||||
| -rw-r--r-- | playlist_generator/requirements.txt | 1 |
2 files changed, 81 insertions, 0 deletions
diff --git a/playlist_generator/playlist_generator.py b/playlist_generator/playlist_generator.py new file mode 100644 index 0000000..19e1e5b --- /dev/null +++ b/playlist_generator/playlist_generator.py @@ -0,0 +1,80 @@ +#!/usr/bin/python3 +# Glitch has Python 3.7.10 installed +import argparse +from random import shuffle +import yt_dlp + + +class PlaylistGenerator: + def get_urls_from_file(self, path: str) -> list[str]: + with open(path) as f: + lines = f.readlines() + return [line.strip() for line in lines if len(line.strip()) > 0 and not line.startswith("#")] + + def extract(self, urls: list) -> list[dict[str]]: + info = {} + ydl_opts = {} + errors = [] + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + for url in urls: + try: + url_info = ydl.sanitize_info(ydl.extract_info(url, download=False)) + if url_info["_type"] == "video": + info[url_info["id"]] = url_info + if url_info["_type"] == "playlist": + for entry in url_info["entries"]: + info[entry["id"]] = entry + except yt_dlp.utils.DownloadError: + errors.append(url) + print(f"{errors=}") + return list(info.values()) + + def build_playlist_string(self, video_info: list[dict[str]]) -> str: + output = "" + for info in video_info: + artist = self.escape_quotes(self.get_artist(info)) + title = self.escape_quotes(self.get_title(info)) + output += f'{{ artist: "{artist}", name: "{title}", youtubeId: "{info["id"]}" }},\n' + return output + + def get_artist(self, info): + if info.get("artist"): + return info["artist"] + elif " - " in info["title"]: + return info["title"].split(" - ")[0] + else: + return info["uploader"] + + def get_title(self, info): + title = info["title"] + suffixes = ["(Official Music Video)", "(Official Video)", "(Official Lyric Video)", "(Official Audio)", "(Audio)"] + for suffix in suffixes: + title = title.removesuffix(suffix).strip() + if info.get("track"): + return info["track"] + elif " - " in title: + return title.split(" - ")[1] + else: + return title + + def escape_quotes(self, field: str) -> str: + return field.replace('"', r'\"') + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("urls_file") + parser.add_argument("output_file") + args = parser.parse_args() + + generator = PlaylistGenerator() + urls = generator.get_urls_from_file(args.urls_file) + video_info = generator.extract(urls) + shuffle(video_info) + js_playlist = generator.build_playlist_string(video_info) + with open(args.output_file, 'w') as f: + f.write(js_playlist) + + +if __name__ == "__main__": + main() diff --git a/playlist_generator/requirements.txt b/playlist_generator/requirements.txt new file mode 100644 index 0000000..d0d869d --- /dev/null +++ b/playlist_generator/requirements.txt @@ -0,0 +1 @@ +yt-dlp==2023.3.4 |
