refactored a bit

This commit is contained in:
Lars Noack 2022-11-24 14:13:55 +01:00
parent 42feb5397a
commit cd9d67102b
5 changed files with 88 additions and 68 deletions

14
src/goof.py Normal file
View 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)

View File

@ -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)

View File

@ -1,3 +1,4 @@
from typing import List
import mutagen.id3 import mutagen.id3
import requests import requests
import os.path import os.path
@ -10,7 +11,11 @@ from .sources import (
musify, musify,
local_files 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 from ..database.temp_database import temp_database
logger = DOWNLOAD_LOGGER logger = DOWNLOAD_LOGGER
@ -34,41 +39,38 @@ print(EasyID3.valid_keys.keys())
class Download: class Download:
def __init__(self): 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): @classmethod
self.write_metadata(song) 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 continue
# download_success = Download.download_from_src(song['src'], song) is_downloaded = False
for source in song.sources: for source in song.sources:
download_success = Download.download_from_src(song, source) download_success = Download.download_from_src(song, source)
if download_success != -1:
break if download_success == -1:
else:
logger.warning(f"couldn't download {song['url']} from {song['src']}") logger.warning(f"couldn't download {song['url']} from {song['src']}")
else:
is_downloaded = True
break
""" if is_downloaded:
download_success = None cls.write_metadata(song)
src = song['src']
if src == 'musify':
download_success = musify.download(song)
elif src == 'youtube':
download_success = youtube.download(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 return source_subclass.fetch_audio(song, source)
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, src) @classmethod
def write_metadata(cls, song: song_object):
@staticmethod
def write_metadata(song: song_objects.Song):
if not os.path.exists(song.target.file): if not os.path.exists(song.target.file):
logger.warning(f"file {song.target.file} doesn't exist") logger.warning(f"file {song.target.file} doesn't exist")
return False return False
@ -88,14 +90,14 @@ class Download:
logger.info("saving") logger.info("saving")
audiofile.save(song.target.file, v1=2) audiofile.save(song.target.file, v1=2)
@staticmethod @classmethod
def path_stuff(target: song_objects.Target) -> bool: def path_stuff(cls, target: target_object) -> bool:
# returns true if it shouldn't be downloaded # returns true if it should be downloaded
if os.path.exists(target.file): if os.path.exists(target.file):
logger.info(f"'{target.file}' does already exist, thus not downloading.") logger.info(f"'{target.file}' does already exist, thus not downloading.")
return True return False
os.makedirs(target.path, exist_ok=True) os.makedirs(target.path, exist_ok=True)
return False return True
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,11 +1,12 @@
from ..utils.shared import * from typing import List
from ..utils.shared import *
from .sources import ( from .sources import (
youtube, youtube,
musify, musify,
local_files local_files
) )
from ..database.song import Song as song_object
from ..database.temp_database import temp_database from ..database.temp_database import temp_database
logger = URL_DOWNLOAD_LOGGER logger = URL_DOWNLOAD_LOGGER
@ -19,42 +20,12 @@ sources = {
class Download: class Download:
def __init__(self) -> None: def __init__(self) -> None:
self.urls = []
for song in temp_database.get_tracks_without_src(): for song in temp_database.get_tracks_without_src():
id_ = song['id'] id_ = song['id']
if os.path.exists(song.target.file): 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 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 sucess = False
for src in AUDIO_SOURCES: for src in AUDIO_SOURCES:
res = Download.fetch_from_src(song, src) res = Download.fetch_from_src(song, src)
@ -65,16 +36,34 @@ class Download:
if not sucess: if not sucess:
logger.warning(f"Didn't find any sources for {song}") logger.warning(f"Didn't find any sources for {song}")
@staticmethod @classmethod
def fetch_from_src(song, src): 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: 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_source(song) return source_subclass.fetch_source(song)
@staticmethod @classmethod
def add_url(url: str, src: str, id_: str): def add_url(cls, url: str, src: str, id_: str):
temp_database.set_download_data(id_, url, src) temp_database.set_download_data(id_, url, src)

View File

@ -78,6 +78,7 @@ class Song:
self.json_data = json_response self.json_data = json_response
# initialize the data # initialize the data
self.id = self.json_data['id']
self.title = self.json_data['title'] self.title = self.json_data['title']
self.artists = [] self.artists = []
for a in self.json_data['artists']: for a in self.json_data['artists']: