refactored youtube using the song and src object instead of dict
This commit is contained in:
parent
4bd3e39a75
commit
7bab6e3006
@ -20,4 +20,4 @@ class AudioSource:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def fetch_audio(cls, song: song_objects.Song, src: song_objects.Sourcet):
|
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}")
|
||||||
|
@ -64,18 +64,23 @@ class Youtube(AudioSource):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def fetch_audio(cls, song: song_objects.Song, src: song_objects.Source, trie: int=0):
|
def fetch_audio(cls, song: song_objects.Song, src: song_objects.Source, trie: int=0):
|
||||||
super().fetch_audio(song, src)
|
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 = {
|
options = {
|
||||||
'format': 'bestaudio/best',
|
'format': 'bestaudio/best',
|
||||||
'keepvideo': False,
|
'keepvideo': False,
|
||||||
'outtmpl': file_
|
'outtmpl': song.target.file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# downloading
|
||||||
try:
|
try:
|
||||||
with youtube_dl.YoutubeDL(options) as ydl:
|
with youtube_dl.YoutubeDL(options) as ydl:
|
||||||
ydl.download([src.url])
|
ydl.download([src.url])
|
||||||
|
|
||||||
except youtube_dl.utils.DownloadError:
|
except youtube_dl.utils.DownloadError:
|
||||||
|
# retry when failing
|
||||||
logger.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:
|
||||||
logger.warning("too many tries, returning")
|
logger.warning("too many tries, returning")
|
||||||
@ -83,75 +88,3 @@ class Youtube(AudioSource):
|
|||||||
time.sleep(WAIT_BETWEEN_BLOCK)
|
time.sleep(WAIT_BETWEEN_BLOCK)
|
||||||
return cls.fetch_audio(song, src, trie=trie + 1)
|
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)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user