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'])
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
@ -52,24 +61,20 @@ class Download:
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'])
@staticmethod
def download_from_src(src, row):
def download_from_src(src, url, row):
if src not in sources:
raise ValueError(f"source {src} seems to not exist")
source_subclass = sources[src]
return source_subclass.fetch_audio(row)
return source_subclass.fetch_audio(url, row)
@staticmethod
def write_metadata(row, 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
# 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
@classmethod
def fetch_audio(cls, row: dict):
super().fetch_audio(row)
def fetch_audio(cls, url: str, row: dict):
super().fetch_audio(url, row)
url = row['url']
file_ = row['file']
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__}")
@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']}")

View File

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

View File

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

View File

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