From d45b42ff7d3ea58fb35c5cbdaa0d4b3ce013c69c Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Fri, 18 Nov 2022 09:01:09 +0100 Subject: [PATCH] refactored youtube --- .../audio_source/sources/musify.py | 3 +- .../audio_source/sources/youtube.py | 35 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/music_kraken/audio_source/sources/musify.py b/src/music_kraken/audio_source/sources/musify.py index fd3e986..be1e6d0 100644 --- a/src/music_kraken/audio_source/sources/musify.py +++ b/src/music_kraken/audio_source/sources/musify.py @@ -141,6 +141,7 @@ class Musify(AudioSource): return download_from_musify(file_, url) +""" def get_musify_url(row): title = row['title'] artists = row['artists'] @@ -252,7 +253,7 @@ def get_musify_url_slow(row): result = search_for_track(row) if result is not None: return result - +""" if __name__ == "__main__": pass diff --git a/src/music_kraken/audio_source/sources/youtube.py b/src/music_kraken/audio_source/sources/youtube.py index 44803ca..7dc3564 100644 --- a/src/music_kraken/audio_source/sources/youtube.py +++ b/src/music_kraken/audio_source/sources/youtube.py @@ -17,8 +17,23 @@ MAX_TRIES = 3 class Youtube(AudioSource): + @classmethod + def get_youtube_from_isrc(cls, isrc: str) -> List[dict]: + # https://stackoverflow.com/questions/63388364/searching-youtube-videos-using-youtube-dl + with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl: + try: + videos = ydl.extract_info(f"ytsearch:{isrc}", download=False)['entries'] + except youtube_dl.utils.DownloadError: + return [] + + return [{ + 'url': video[YOUTUBE_URL_KEY], + 'title': video[YOUTUBE_TITLE_KEY] + } for video in videos] + @classmethod def fetch_source(cls, row: dict): + # https://stackoverflow.com/questions/63388364/searching-youtube-videos-using-youtube-dl super().fetch_source(row) if row['isrc'] is None: @@ -27,7 +42,7 @@ class Youtube(AudioSource): real_title = row['title'].lower() final_result = None - results = get_youtube_from_isrc(row['isrc']) + results = cls.get_youtube_from_isrc(row['isrc']) for result in results: video_title = result['title'].lower() match, distance = phonetic_compares.match_titles(video_title, real_title) @@ -51,11 +66,6 @@ class Youtube(AudioSource): file_ = row['file'] options = { 'format': 'bestaudio/best', - 'postprocessors': [{ - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192', - }], 'keepvideo': False, 'outtmpl': file_ } @@ -64,13 +74,14 @@ class Youtube(AudioSource): with youtube_dl.YoutubeDL(options) as ydl: ydl.download([url]) except youtube_dl.utils.DownloadError: - logging.warning(f"youtube blocked downloading. ({trie}-{MAX_TRIES})") + logger.warning(f"youtube blocked downloading. ({trie}-{MAX_TRIES})") if trie >= MAX_TRIES: - logging.warning("too many tries, returning") - logging.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again") + logger.warning("too many tries, returning") + logger.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again") time.sleep(WAIT_BETWEEN_BLOCK) - return Youtube.fetch_audio(row, trie=trie + 1) + return cls.fetch_audio(row, trie=trie + 1) +""" def get_youtube_from_isrc(isrc: str) -> List[dict]: # https://stackoverflow.com/questions/63388364/searching-youtube-videos-using-youtube-dl with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl: @@ -98,7 +109,7 @@ def get_youtube_url(row): match, distance = phonetic_compares.match_titles(video_title, real_title) if match: - logging.warning( + logger.warning( f"dont downloading {result['url']} cuz the phonetic distance ({distance}) between {real_title} and {video_title} is to high.") continue @@ -133,7 +144,7 @@ def download(row, trie: int = 0): logging.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again") time.sleep(WAIT_BETWEEN_BLOCK) return download(row, trie=trie+1) - +""" if __name__ == "__main__": # example isrc that exists on YouTube music