refactored and added for possibility for multiple sources per track

This commit is contained in:
Lars Noack 2022-11-21 14:27:18 +01:00
parent 0f25833da0
commit 58626a7d99
7 changed files with 22 additions and 17 deletions

View File

@ -41,7 +41,16 @@ class Download:
self.write_metadata(row, row['file']) self.write_metadata(row, row['file'])
continue continue
download_success = Download.download_from_src(row['src'], row) # download_success = Download.download_from_src(row['src'], row)
sources = row['source']
for source in sources:
if source['src'] is None:
continue
download_success = Download.download_from_src(source['src'], source['url'], row)
if download_success != -1:
break
else:
logger.warning(f"couldn't download {row['url']} from {row['src']}")
""" """
download_success = None download_success = None
@ -52,24 +61,20 @@ class Download:
download_success = youtube.download(row) download_success = youtube.download(row)
""" """
if download_success == -1:
logger.warning(f"couldn't download {row['url']} from {row['src']}")
continue
self.write_metadata(row, row['file']) self.write_metadata(row, row['file'])
@staticmethod @staticmethod
def download_from_src(src, row): def download_from_src(src, url, row):
if src not in sources: if src not in sources:
raise ValueError(f"source {src} seems to not exist") raise ValueError(f"source {src} seems to not exist")
source_subclass = sources[src] source_subclass = sources[src]
return source_subclass.fetch_audio(row) return source_subclass.fetch_audio(url, row)
@staticmethod @staticmethod
def write_metadata(row, file_path): def write_metadata(row, file_path):
if not os.path.exists(file_path): if not os.path.exists(file_path):
logger.warning("something went really wrong") logger.warning(f"file {file_path} doesn't exist")
return False return False
# only convert the file to the proper format if mutagen doesn't work with it due to time # only convert the file to the proper format if mutagen doesn't work with it due to time

View File

@ -156,10 +156,9 @@ class Musify(AudioSource):
return True return True
@classmethod @classmethod
def fetch_audio(cls, row: dict): def fetch_audio(cls, url: str, row: dict):
super().fetch_audio(row) super().fetch_audio(url, row)
url = row['url']
file_ = row['file'] file_ = row['file']
return cls.download_from_musify(file_, url) return cls.download_from_musify(file_, url)

View File

@ -16,5 +16,5 @@ class AudioSource:
logger.info(f"try getting source {row['title']} from {cls.__name__}") logger.info(f"try getting source {row['title']} from {cls.__name__}")
@classmethod @classmethod
def fetch_audio(cls, row: dict): def fetch_audio(cls, url: str,row: dict):
logger.info(f"downloading audio from {row['url']} from {cls.__name__} to {row['file']}") logger.info(f"downloading audio from {row['url']} from {cls.__name__} to {row['file']}")

View File

@ -59,10 +59,9 @@ class Youtube(AudioSource):
return final_result['url'] return final_result['url']
@classmethod @classmethod
def fetch_audio(cls, row: dict, trie: int=0): def fetch_audio(cls, url: str, row: dict, trie: int=0):
super().fetch_audio(row) super().fetch_audio(url, row)
url = row['url']
file_ = row['file'] file_ = row['file']
options = { options = {
'format': 'bestaudio/best', 'format': 'bestaudio/best',
@ -79,7 +78,7 @@ class Youtube(AudioSource):
logger.warning("too many tries, returning") logger.warning("too many tries, returning")
logger.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 cls.fetch_audio(row, trie=trie + 1) return cls.fetch_audio(url, row, trie=trie + 1)
""" """
def get_youtube_from_isrc(isrc: str) -> List[dict]: def get_youtube_from_isrc(isrc: str) -> List[dict]:

View File

@ -3,7 +3,7 @@ import logging
import tempfile import tempfile
import os import os
from .database import Database from ..database.database import Database
TEMP_FOLDER = "music-downloader" TEMP_FOLDER = "music-downloader"
LOG_FILE = "download_logs.log" LOG_FILE = "download_logs.log"

View File

@ -6,6 +6,8 @@ db = mk.utils.shared.database
if len(db.get_custom_track([])) == 0: if len(db.get_custom_track([])) == 0:
mk.cli() mk.cli()
mk.fetch_audio.Download()
if __name__ == "__main__": if __name__ == "__main__":
db = mk.utils.shared.database db = mk.utils.shared.database
for elem in db.get_custom_track([]): for elem in db.get_custom_track([]):