refactored a bit
This commit is contained in:
		
							
								
								
									
										14
									
								
								src/goof.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/goof.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| import music_kraken as mk | ||||
| print(mk.__file__) | ||||
|  | ||||
| from music_kraken.audio_source import ( | ||||
|     fetch_sources, | ||||
|     fetch_audios | ||||
| ) | ||||
| print(fetch_sources) | ||||
|  | ||||
| cache = mk.database.temp_database.temp_database | ||||
| print(cache, len(cache.get_tracks_without_src())) | ||||
|  | ||||
| fetch_sources(cache.get_tracks_without_src(), skip_existing_files=False) | ||||
| fetch_audios(cache.get_tracks_to_download(), override_existing=True) | ||||
| @@ -0,0 +1,14 @@ | ||||
| from typing import List | ||||
|  | ||||
| from ..database.song import Song as song_object | ||||
| from . import ( | ||||
|     fetch_source, | ||||
|     fetch_audio | ||||
| ) | ||||
|  | ||||
| def fetch_sources(songs: List[song_object], skip_existing_files: bool = False): | ||||
|     fetch_source.Download.fetch_sources(songs=songs, skip_existing_files=skip_existing_files) | ||||
|  | ||||
| def fetch_audios(songs: List[song_object], override_existing: bool = False): | ||||
|     fetch_audio.Download.fetch_audios(songs=songs, override_existing=override_existing) | ||||
|  | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| from typing import List | ||||
| import mutagen.id3 | ||||
| import requests | ||||
| import os.path | ||||
| @@ -10,7 +11,11 @@ from .sources import ( | ||||
|     musify, | ||||
|     local_files | ||||
| ) | ||||
| from ..database import song as song_objects | ||||
| from ..database.song import ( | ||||
|     Song as song_object, | ||||
|     Target as target_object, | ||||
|     Source as source_object | ||||
| ) | ||||
| from ..database.temp_database import temp_database | ||||
|  | ||||
| logger = DOWNLOAD_LOGGER | ||||
| @@ -34,41 +39,38 @@ print(EasyID3.valid_keys.keys()) | ||||
|  | ||||
| class Download: | ||||
|     def __init__(self): | ||||
|         for song in temp_database.get_tracks_to_download(): | ||||
|         Download.fetch_audios(temp_database.get_tracks_to_download()) | ||||
|  | ||||
|             if self.path_stuff(song.target): | ||||
|                 self.write_metadata(song) | ||||
|     @classmethod | ||||
|     def fetch_audios(cls, songs: List[song_object], override_existing: bool = False): | ||||
|         for song in songs: | ||||
|             if not cls.path_stuff(song.target) and not override_existing: | ||||
|                 cls.write_metadata(song) | ||||
|                 continue | ||||
|  | ||||
|             # download_success = Download.download_from_src(song['src'], song) | ||||
|             is_downloaded = False | ||||
|             for source in song.sources: | ||||
|                 download_success = Download.download_from_src(song, source) | ||||
|                 if download_success != -1: | ||||
|                     break | ||||
|                 else: | ||||
|  | ||||
|                 if download_success == -1: | ||||
|                     logger.warning(f"couldn't download {song['url']} from {song['src']}") | ||||
|                 else: | ||||
|                     is_downloaded = True | ||||
|                     break | ||||
|  | ||||
|             """ | ||||
|             download_success = None | ||||
|             src = song['src'] | ||||
|             if src == 'musify': | ||||
|                 download_success = musify.download(song) | ||||
|             elif src == 'youtube': | ||||
|                 download_success = youtube.download(song) | ||||
|             """ | ||||
|             if is_downloaded: | ||||
|                 cls.write_metadata(song) | ||||
|  | ||||
|             self.write_metadata(song) | ||||
|     @classmethod | ||||
|     def download_from_src(cls, song: song_object, source: source_object): | ||||
|         if source.src not in sources: | ||||
|             raise ValueError(f"source {source.src} seems to not exist") | ||||
|         source_subclass = sources[source.src] | ||||
|  | ||||
|     @staticmethod | ||||
|     def download_from_src(song, src): | ||||
|         if src.src not in sources: | ||||
|             raise ValueError(f"source {src.src} seems to not exist") | ||||
|         source_subclass = sources[src.src] | ||||
|         return source_subclass.fetch_audio(song, source) | ||||
|  | ||||
|         return source_subclass.fetch_audio(song, src) | ||||
|  | ||||
|     @staticmethod | ||||
|     def write_metadata(song: song_objects.Song): | ||||
|     @classmethod | ||||
|     def write_metadata(cls, song: song_object): | ||||
|         if not os.path.exists(song.target.file): | ||||
|             logger.warning(f"file {song.target.file} doesn't exist") | ||||
|             return False | ||||
| @@ -88,14 +90,14 @@ class Download: | ||||
|         logger.info("saving") | ||||
|         audiofile.save(song.target.file, v1=2) | ||||
|  | ||||
|     @staticmethod | ||||
|     def path_stuff(target: song_objects.Target) -> bool: | ||||
|         # returns true if it shouldn't be downloaded | ||||
|     @classmethod | ||||
|     def path_stuff(cls, target: target_object) -> bool: | ||||
|         # returns true if it should be downloaded | ||||
|         if os.path.exists(target.file): | ||||
|             logger.info(f"'{target.file}' does already exist, thus not downloading.") | ||||
|             return True | ||||
|         os.makedirs(target.path, exist_ok=True) | ||||
|             return False | ||||
|         os.makedirs(target.path, exist_ok=True) | ||||
|         return True | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|   | ||||
| @@ -1,11 +1,12 @@ | ||||
| from ..utils.shared import * | ||||
| from typing import List | ||||
|  | ||||
| from ..utils.shared import * | ||||
| from .sources import ( | ||||
|     youtube, | ||||
|     musify, | ||||
|     local_files | ||||
| ) | ||||
|  | ||||
| from ..database.song import Song as song_object | ||||
| from ..database.temp_database import temp_database | ||||
|  | ||||
| logger = URL_DOWNLOAD_LOGGER | ||||
| @@ -19,42 +20,12 @@ sources = { | ||||
|  | ||||
| class Download: | ||||
|     def __init__(self) -> None: | ||||
|         self.urls = [] | ||||
|  | ||||
|         for song in temp_database.get_tracks_without_src(): | ||||
|  | ||||
|             id_ = song['id'] | ||||
|             if os.path.exists(song.target.file): | ||||
|                 logger.info(f"skipping the fetching of the download links, cuz {song['file']} already exists.") | ||||
|                 logger.info(f"skipping the fetching of the download links, cuz {song.target.file} already exists.") | ||||
|                 continue | ||||
|  | ||||
|             """ | ||||
|             not implemented yet, will in one point crashe everything | ||||
|             # check File System | ||||
|             file_path = file_system.get_path(song) | ||||
|             if file_path is not None: | ||||
|                 self.add_url(file_path, 'file', id_) | ||||
|                 continue | ||||
|             """ | ||||
|             """ | ||||
|             # check YouTube | ||||
|             youtube_url = youtube.Youtube.fetch_source(song) | ||||
|             if youtube_url is not None: | ||||
|                 self.add_url(youtube_url, 'youtube', id_) | ||||
|                 continue | ||||
|  | ||||
|             # check musify | ||||
|             musify_url = musify.Musify.fetch_source(song) | ||||
|             if musify_url is not None: | ||||
|                 self.add_url(musify_url, 'musify', id_) | ||||
|                 continue | ||||
|  | ||||
|             # check musify again, but with a different methode that takes longer | ||||
|             musify_url = musify.get_musify_url_slow(song) | ||||
|             if musify_url is not None: | ||||
|                 self.add_url(musify_url, 'musify', id_) | ||||
|                 continue | ||||
|             """ | ||||
|             sucess = False | ||||
|             for src in AUDIO_SOURCES: | ||||
|                 res = Download.fetch_from_src(song, src) | ||||
| @@ -65,16 +36,34 @@ class Download: | ||||
|             if not sucess: | ||||
|                 logger.warning(f"Didn't find any sources for {song}") | ||||
|  | ||||
|     @staticmethod | ||||
|     def fetch_from_src(song, src): | ||||
|     @classmethod | ||||
|     def fetch_sources(cls, songs: List[song_object], skip_existing_files: bool = False): | ||||
|         for song in songs: | ||||
|             if os.path.exists(song.target.file) and skip_existing_files: | ||||
|                 logger.info(f"skipping the fetching of the download links, cuz {song.target.file} already exists.") | ||||
|                 continue | ||||
|  | ||||
|             sucess = False | ||||
|             for src in AUDIO_SOURCES: | ||||
|                 res = cls.fetch_from_src(song, src) | ||||
|                 if res is not None: | ||||
|                     sucess = True | ||||
|                     cls.add_url(res, src, song.id) | ||||
|  | ||||
|             if not sucess: | ||||
|                 logger.warning(f"Didn't find any sources for {song}") | ||||
|      | ||||
|  | ||||
|     @classmethod | ||||
|     def fetch_from_src(cls, song, src): | ||||
|         if src not in sources: | ||||
|             raise ValueError(f"source {src} seems to not exist") | ||||
|  | ||||
|         source_subclass = sources[src] | ||||
|         return source_subclass.fetch_source(song) | ||||
|  | ||||
|     @staticmethod | ||||
|     def add_url(url: str, src: str, id_: str): | ||||
|     @classmethod | ||||
|     def add_url(cls, url: str, src: str, id_: str): | ||||
|         temp_database.set_download_data(id_, url, src) | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -78,6 +78,7 @@ class Song: | ||||
|         self.json_data = json_response | ||||
|  | ||||
|         # initialize the data | ||||
|         self.id = self.json_data['id'] | ||||
|         self.title = self.json_data['title'] | ||||
|         self.artists = [] | ||||
|         for a in self.json_data['artists']: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user