refactored youtube

This commit is contained in:
Lars Noack 2022-11-18 09:01:09 +01:00
parent 445272389f
commit d45b42ff7d
2 changed files with 25 additions and 13 deletions

View File

@ -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

View File

@ -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