refactored

This commit is contained in:
Lars Noack 2022-11-22 09:21:28 +01:00
parent fe1c849852
commit e158b1e15c
3 changed files with 33 additions and 32 deletions

View File

@ -32,47 +32,44 @@ print(EasyID3.valid_keys.keys())
class Download: class Download:
def __init__(self): def __init__(self):
for row in database.get_tracks_to_download(): for song in database.get_tracks_to_download():
row['artist'] = [i['name'] for i in row['artists']] song['artist'] = [i['name'] for i in song['artists']]
row['file'] = os.path.join(MUSIC_DIR, row['file']) song['file'] = os.path.join(MUSIC_DIR, song['file'])
row['path'] = os.path.join(MUSIC_DIR, row['path']) song['path'] = os.path.join(MUSIC_DIR, song['path'])
if self.path_stuff(row['path'], row['file']): if self.path_stuff(song['path'], song['file']):
self.write_metadata(row, row['file']) self.write_metadata(song, song['file'])
continue continue
# download_success = Download.download_from_src(row['src'], row) # download_success = Download.download_from_src(song['src'], song)
sources = row['source'] for source in song.sources:
for source in sources: download_success = Download.download_from_src(source.src, source.url, song)
if source['src'] is None:
continue
download_success = Download.download_from_src(source['src'], source['url'], row)
if download_success != -1: if download_success != -1:
break break
else: else:
logger.warning(f"couldn't download {row['url']} from {row['src']}") logger.warning(f"couldn't download {song['url']} from {song['src']}")
""" """
download_success = None download_success = None
src = row['src'] src = song['src']
if src == 'musify': if src == 'musify':
download_success = musify.download(row) download_success = musify.download(song)
elif src == 'youtube': elif src == 'youtube':
download_success = youtube.download(row) download_success = youtube.download(song)
""" """
self.write_metadata(row, row['file']) self.write_metadata(song, song['file'])
@staticmethod @staticmethod
def download_from_src(src, url, row): def download_from_src(src, url, song):
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(url, row) return source_subclass.fetch_audio(url, song)
@staticmethod @staticmethod
def write_metadata(row, file_path): def write_metadata(song, file_path):
if not os.path.exists(file_path): if not os.path.exists(file_path):
logger.warning(f"file {file_path} doesn't exist") logger.warning(f"file {file_path} doesn't exist")
return False return False
@ -86,11 +83,11 @@ class Download:
valid_keys = list(EasyID3.valid_keys.keys()) valid_keys = list(EasyID3.valid_keys.keys())
for key in list(row.keys()): for key in list(song.keys()):
if key in valid_keys and row[key] is not None: if key in valid_keys and song[key] is not None:
if type(row[key]) != list: if type(song[key]) != list:
row[key] = str(row[key]) song[key] = str(song[key])
audiofile[key] = row[key] audiofile[key] = song[key]
logger.info("saving") logger.info("saving")
audiofile.save(file_path, v1=2) audiofile.save(file_path, v1=2)

View File

@ -1,3 +1,4 @@
from typing import List
import sqlite3 import sqlite3
import os import os
import logging import logging
@ -193,7 +194,7 @@ GROUP BY track.id;
""" """
return query return query
def get_custom_track(self, custom_where: list): def get_custom_track(self, custom_where: list) -> List[song.Song]:
query = Database.get_custom_track_query(custom_where=custom_where) query = Database.get_custom_track_query(custom_where=custom_where)
return [song.Song(json.loads(i[0])) for i in self.cursor.execute(query)] return [song.Song(json.loads(i[0])) for i in self.cursor.execute(query)]
@ -205,19 +206,19 @@ GROUP BY track.id;
return resulting_tracks[0] return resulting_tracks[0]
def get_tracks_to_download(self): def get_tracks_to_download(self) -> List[song.Song]:
return self.get_custom_track(['track.downloaded == 0']) return self.get_custom_track(['track.downloaded == 0'])
def get_tracks_without_src(self): def get_tracks_without_src(self) -> List[song.Song]:
return self.get_custom_track(["(track.url IS NULL OR track.src IS NULL)"]) return self.get_custom_track(["(track.url IS NULL OR track.src IS NULL)"])
def get_tracks_without_isrc(self): def get_tracks_without_isrc(self) -> List[song.Song]:
return self.get_custom_track(["track.isrc IS NULL"]) return self.get_custom_track(["track.isrc IS NULL"])
def get_tracks_without_filepath(self): def get_tracks_without_filepath(self) -> List[song.Song]:
return self.get_custom_track(["(track.file IS NULL OR track.path IS NULL OR track.genre IS NULL)"]) return self.get_custom_track(["(track.file IS NULL OR track.path IS NULL OR track.genre IS NULL)"])
def get_tracks_for_lyrics(self): def get_tracks_for_lyrics(self) -> List[song.Song]:
return self.get_custom_track(["track.lyrics IS NULL"]) return self.get_custom_track(["track.lyrics IS NULL"])
def add_lyrics(self, track_id: str, lyrics: str): def add_lyrics(self, track_id: str, lyrics: str):

View File

@ -1,3 +1,6 @@
from typing import List
class Artist: class Artist:
def __init__(self, artist_data) -> None: def __init__(self, artist_data) -> None:
self.artist_data self.artist_data
@ -20,7 +23,7 @@ class Song:
self.title = self.json_data['title'] self.title = self.json_data['title']
self.artists = [Artist(a) for a in self.json_data['artists']] self.artists = [Artist(a) for a in self.json_data['artists']]
self.sources = [] self.sources: List[Source] = []
for src in self.json_data['source']: for src in self.json_data['source']:
if src['src'] is None: if src['src'] is None:
continue continue