refactored youtube
This commit is contained in:
parent
445272389f
commit
d45b42ff7d
@ -141,6 +141,7 @@ class Musify(AudioSource):
|
|||||||
return download_from_musify(file_, url)
|
return download_from_musify(file_, url)
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
def get_musify_url(row):
|
def get_musify_url(row):
|
||||||
title = row['title']
|
title = row['title']
|
||||||
artists = row['artists']
|
artists = row['artists']
|
||||||
@ -252,7 +253,7 @@ def get_musify_url_slow(row):
|
|||||||
result = search_for_track(row)
|
result = search_for_track(row)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
return result
|
return result
|
||||||
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
pass
|
pass
|
||||||
|
@ -17,8 +17,23 @@ MAX_TRIES = 3
|
|||||||
|
|
||||||
|
|
||||||
class Youtube(AudioSource):
|
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
|
@classmethod
|
||||||
def fetch_source(cls, row: dict):
|
def fetch_source(cls, row: dict):
|
||||||
|
# https://stackoverflow.com/questions/63388364/searching-youtube-videos-using-youtube-dl
|
||||||
super().fetch_source(row)
|
super().fetch_source(row)
|
||||||
|
|
||||||
if row['isrc'] is None:
|
if row['isrc'] is None:
|
||||||
@ -27,7 +42,7 @@ class Youtube(AudioSource):
|
|||||||
real_title = row['title'].lower()
|
real_title = row['title'].lower()
|
||||||
|
|
||||||
final_result = None
|
final_result = None
|
||||||
results = get_youtube_from_isrc(row['isrc'])
|
results = cls.get_youtube_from_isrc(row['isrc'])
|
||||||
for result in results:
|
for result in results:
|
||||||
video_title = result['title'].lower()
|
video_title = result['title'].lower()
|
||||||
match, distance = phonetic_compares.match_titles(video_title, real_title)
|
match, distance = phonetic_compares.match_titles(video_title, real_title)
|
||||||
@ -51,11 +66,6 @@ class Youtube(AudioSource):
|
|||||||
file_ = row['file']
|
file_ = row['file']
|
||||||
options = {
|
options = {
|
||||||
'format': 'bestaudio/best',
|
'format': 'bestaudio/best',
|
||||||
'postprocessors': [{
|
|
||||||
'key': 'FFmpegExtractAudio',
|
|
||||||
'preferredcodec': 'mp3',
|
|
||||||
'preferredquality': '192',
|
|
||||||
}],
|
|
||||||
'keepvideo': False,
|
'keepvideo': False,
|
||||||
'outtmpl': file_
|
'outtmpl': file_
|
||||||
}
|
}
|
||||||
@ -64,13 +74,14 @@ class Youtube(AudioSource):
|
|||||||
with youtube_dl.YoutubeDL(options) as ydl:
|
with youtube_dl.YoutubeDL(options) as ydl:
|
||||||
ydl.download([url])
|
ydl.download([url])
|
||||||
except youtube_dl.utils.DownloadError:
|
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:
|
if trie >= MAX_TRIES:
|
||||||
logging.warning("too many tries, returning")
|
logger.warning("too many tries, returning")
|
||||||
logging.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again")
|
logger.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again")
|
||||||
time.sleep(WAIT_BETWEEN_BLOCK)
|
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]:
|
def get_youtube_from_isrc(isrc: str) -> List[dict]:
|
||||||
# https://stackoverflow.com/questions/63388364/searching-youtube-videos-using-youtube-dl
|
# https://stackoverflow.com/questions/63388364/searching-youtube-videos-using-youtube-dl
|
||||||
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
|
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)
|
match, distance = phonetic_compares.match_titles(video_title, real_title)
|
||||||
|
|
||||||
if match:
|
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.")
|
f"dont downloading {result['url']} cuz the phonetic distance ({distance}) between {real_title} and {video_title} is to high.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -133,7 +144,7 @@ def download(row, trie: int = 0):
|
|||||||
logging.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again")
|
logging.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again")
|
||||||
time.sleep(WAIT_BETWEEN_BLOCK)
|
time.sleep(WAIT_BETWEEN_BLOCK)
|
||||||
return download(row, trie=trie+1)
|
return download(row, trie=trie+1)
|
||||||
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# example isrc that exists on YouTube music
|
# example isrc that exists on YouTube music
|
||||||
|
Loading…
Reference in New Issue
Block a user