From 58626a7d99783c8f16cebb5173fb983934ef8503 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Mon, 21 Nov 2022 14:27:18 +0100 Subject: [PATCH] refactored and added for possibility for multiple sources per track --- src/music_kraken/audio_source/fetch_audio.py | 21 ++++++++++++------- .../audio_source/sources/musify.py | 5 ++--- .../audio_source/sources/source.py | 2 +- .../audio_source/sources/youtube.py | 7 +++---- .../{utils => database}/database.py | 0 src/music_kraken/utils/shared.py | 2 +- src/try-programming-interface.py | 2 ++ 7 files changed, 22 insertions(+), 17 deletions(-) rename src/music_kraken/{utils => database}/database.py (100%) diff --git a/src/music_kraken/audio_source/fetch_audio.py b/src/music_kraken/audio_source/fetch_audio.py index c372e87..224304d 100644 --- a/src/music_kraken/audio_source/fetch_audio.py +++ b/src/music_kraken/audio_source/fetch_audio.py @@ -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 diff --git a/src/music_kraken/audio_source/sources/musify.py b/src/music_kraken/audio_source/sources/musify.py index f918a2d..6709c43 100644 --- a/src/music_kraken/audio_source/sources/musify.py +++ b/src/music_kraken/audio_source/sources/musify.py @@ -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) diff --git a/src/music_kraken/audio_source/sources/source.py b/src/music_kraken/audio_source/sources/source.py index 71ffaac..a6429a7 100644 --- a/src/music_kraken/audio_source/sources/source.py +++ b/src/music_kraken/audio_source/sources/source.py @@ -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']}") diff --git a/src/music_kraken/audio_source/sources/youtube.py b/src/music_kraken/audio_source/sources/youtube.py index 7dc3564..2c35b10 100644 --- a/src/music_kraken/audio_source/sources/youtube.py +++ b/src/music_kraken/audio_source/sources/youtube.py @@ -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]: diff --git a/src/music_kraken/utils/database.py b/src/music_kraken/database/database.py similarity index 100% rename from src/music_kraken/utils/database.py rename to src/music_kraken/database/database.py diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 31acb18..ce2dbe5 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -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" diff --git a/src/try-programming-interface.py b/src/try-programming-interface.py index 2d3caf9..a90f1b6 100644 --- a/src/try-programming-interface.py +++ b/src/try-programming-interface.py @@ -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([]):