From 7bab6e30069062fe18ea44ef401ca228c736b05d Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Tue, 22 Nov 2022 13:34:05 +0100 Subject: [PATCH] refactored youtube using the song and src object instead of dict --- .../audio_source/sources/source.py | 2 +- .../audio_source/sources/youtube.py | 81 ++----------------- 2 files changed, 8 insertions(+), 75 deletions(-) diff --git a/src/music_kraken/audio_source/sources/source.py b/src/music_kraken/audio_source/sources/source.py index f2f0c65..b8df60c 100644 --- a/src/music_kraken/audio_source/sources/source.py +++ b/src/music_kraken/audio_source/sources/source.py @@ -20,4 +20,4 @@ class AudioSource: @classmethod def fetch_audio(cls, song: song_objects.Song, src: song_objects.Sourcet): - logger.info(f"downloading audio from {src.url} from {cls.__name__} to {song['file']}") + logger.info(f"downloading {song}: {cls.__name__} {src.url} -> {song.target.file}") diff --git a/src/music_kraken/audio_source/sources/youtube.py b/src/music_kraken/audio_source/sources/youtube.py index e462a4d..67edf59 100644 --- a/src/music_kraken/audio_source/sources/youtube.py +++ b/src/music_kraken/audio_source/sources/youtube.py @@ -64,18 +64,23 @@ class Youtube(AudioSource): @classmethod def fetch_audio(cls, song: song_objects.Song, src: song_objects.Source, trie: int=0): super().fetch_audio(song, src) + if song.target.file is None or song.target.path is None: + logger.warning(f"target hasn't been set. Can't download. Most likely a bug.") + return False - file_ = song['file'] options = { 'format': 'bestaudio/best', 'keepvideo': False, - 'outtmpl': file_ + 'outtmpl': song.target.file } + # downloading try: with youtube_dl.YoutubeDL(options) as ydl: ydl.download([src.url]) + except youtube_dl.utils.DownloadError: + # retry when failing logger.warning(f"youtube blocked downloading. ({trie}-{MAX_TRIES})") if trie >= MAX_TRIES: logger.warning("too many tries, returning") @@ -83,75 +88,3 @@ class Youtube(AudioSource): time.sleep(WAIT_BETWEEN_BLOCK) return cls.fetch_audio(song, src, 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: - 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] - - -def get_youtube_url(row): - if row['isrc'] is None: - return None - - real_title = row['title'].lower() - - final_result = None - results = 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) - - if match: - logger.warning( - f"dont downloading {result['url']} cuz the phonetic distance ({distance}) between {real_title} and {video_title} is to high.") - continue - - final_result = result - - if final_result is None: - return None - return final_result['url'] - - -def download(row, trie: int = 0): - url = row['url'] - file_ = row['file'] - options = { - 'format': 'bestaudio/best', - 'postprocessors': [{ - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192', - }], - 'keepvideo': False, - 'outtmpl': file_ - } - - try: - with youtube_dl.YoutubeDL(options) as ydl: - ydl.download([url]) - except youtube_dl.utils.DownloadError: - logging.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") - time.sleep(WAIT_BETWEEN_BLOCK) - return download(row, trie=trie+1) -""" - -if __name__ == "__main__": - # example isrc that exists on YouTube music - ISRC = "DEUM71500715" - result = get_youtube_from_isrc(ISRC) - print(result) - result = get_youtube_from_isrc("aslhfklasdhfjklasdfjkhasdjlfhlasdjfkuuiueiw") - print(result)