works again

This commit is contained in:
lars 2022-11-22 18:25:35 +01:00
parent 09fbb57380
commit 01c27efda5
6 changed files with 100 additions and 35 deletions

View File

@ -0,0 +1,66 @@
DROP TABLE IF EXISTS artist;
CREATE TABLE artist (
id TEXT PRIMARY KEY NOT NULL,
name TEXT
);
DROP TABLE IF EXISTS artist_release_group;
CREATE TABLE artist_release_group (
artist_id TEXT NOT NULL,
release_group_id TEXT NOT NULL
);
DROP TABLE IF EXISTS artist_track;
CREATE TABLE artist_track (
artist_id TEXT NOT NULL,
track_id TEXT NOT NULL
);
DROP TABLE IF EXISTS release_group;
CREATE TABLE release_group (
id TEXT PRIMARY KEY NOT NULL,
albumartist TEXT,
albumsort INT,
musicbrainz_albumtype TEXT,
compilation TEXT,
album_artist_id TEXT
);
DROP TABLE IF EXISTS release_;
CREATE TABLE release_ (
id TEXT PRIMARY KEY NOT NULL,
release_group_id TEXT NOT NULL,
title TEXT,
copyright TEXT,
album_status TEXT,
language TEXT,
year TEXT,
date TEXT,
country TEXT,
barcode TEXT
);
DROP TABLE IF EXISTS track;
CREATE TABLE track (
id TEXT PRIMARY KEY NOT NULL,
downloaded BOOLEAN NOT NULL DEFAULT 0,
release_id TEXT NOT NULL,
track TEXT,
length INT,
tracknumber TEXT,
isrc TEXT,
genre TEXT,
lyrics TEXT,
path TEXT,
file TEXT,
url TEXT,
src TEXT
);
DROP TABLE IF EXISTS source;
CREATE TABLE source (
track_id TEXT NOT NULL,
src TEXT NOT NULL,
url TEXT NOT NULL,
valid BOOLEAN NOT NULL DEFAULT 1
);

View File

@ -37,7 +37,7 @@ class Download:
for song in temp_database.get_tracks_to_download():
if self.path_stuff(song.target):
self.write_metadata(song, song.target.file)
self.write_metadata(song)
continue
# download_success = Download.download_from_src(song['src'], song)
@ -57,7 +57,7 @@ class Download:
download_success = youtube.download(song)
"""
self.write_metadata(song, song['file'])
self.write_metadata(song)
@staticmethod
def download_from_src(song, src):
@ -68,31 +68,28 @@ class Download:
return source_subclass.fetch_audio(song, src)
@staticmethod
def write_metadata(song, file_path):
if not os.path.exists(file_path):
logger.warning(f"file {file_path} doesn't exist")
def write_metadata(song: song_objects.Song):
if not os.path.exists(song.target.file):
logger.warning(f"file {song.target.file} doesn't exist")
return False
# only convert the file to the proper format if mutagen doesn't work with it due to time
try:
audiofile = EasyID3(file_path)
audiofile = EasyID3(song.target.file)
except mutagen.id3.ID3NoHeaderError:
AudioSegment.from_file(file_path).export(file_path, format="mp3")
audiofile = EasyID3(file_path)
AudioSegment.from_file(song.target.file).export(song.target.file, format="mp3")
audiofile = EasyID3(song.target.file)
valid_keys = list(EasyID3.valid_keys.keys())
for key in list(song.keys()):
if key in valid_keys and song[key] is not None:
if type(song[key]) != list:
song[key] = str(song[key])
audiofile[key] = song[key]
for key, value in song.get_metadata():
if type(value) != list:
value = str(value)
audiofile[key] = value
logger.info("saving")
audiofile.save(file_path, v1=2)
audiofile.save(song.target.file, v1=2)
@staticmethod
def path_stuff(target: song_objects.Target):
def path_stuff(target: song_objects.Target) -> bool:
# returns true if it shouldn't be downloaded
if os.path.exists(target.file):
logger.info(f"'{target.file}' does already exist, thus not downloading.")

View File

@ -40,7 +40,7 @@ class Youtube(AudioSource):
super().fetch_source(song)
if not song.has_isrc():
return
return None
real_title = song.title.lower()
@ -58,7 +58,8 @@ class Youtube(AudioSource):
final_result = result
if final_result is None:
return False
return None
logger.info(f"found video {final_result}")
return final_result['url']
@classmethod
@ -84,6 +85,7 @@ class Youtube(AudioSource):
logger.warning(f"youtube blocked downloading. ({trie}-{MAX_TRIES})")
if trie >= MAX_TRIES:
logger.warning("too many tries, returning")
return False
logger.warning(f"retrying in {WAIT_BETWEEN_BLOCK} seconds again")
time.sleep(WAIT_BETWEEN_BLOCK)
return cls.fetch_audio(song, src, trie=trie + 1)

View File

@ -31,6 +31,7 @@ class Target:
file = property(fget=get_file, fset=set_file)
path = property(fget=get_path, fset=set_path)
class Artist:
def __init__(self, artist_data) -> None:
self.artist_data = artist_data
@ -41,6 +42,7 @@ class Artist:
def __str__(self) -> str:
return self.name
class Source:
def __init__(self, src_data) -> None:
self.src_data = src_data
@ -48,12 +50,13 @@ class Source:
self.src = self.src_data['src']
self.url = self.src_data['url']
class Metadata:
def __init__(self) -> None:
self.data = {}
def get_all_metadata(self):
pass
return list(self.data.items())
def __setitem__(self, item, value):
if item in EasyID3.valid_keys.keys():
@ -88,11 +91,17 @@ class Song:
# initialize id3 metadata
self.metadata = Metadata()
for key, value in self.json_data.items():
self.metadata[key] = value
self.metadata['artist'] = self.get_artist_names()
# EasyID3.valid_keys.keys()
def __str__(self) -> str:
return f"\"{self.title}\" by {', '.join([str(a) for a in self.artists])}"
def get_metadata(self):
return self.metadata.get_all_metadata()
def has_isrc(self) -> bool:
return self.isrc is not None

View File

@ -4,6 +4,8 @@ from mutagen.id3 import ID3, USLT
from ..utils.shared import *
from . import genius
from ..database.temp_database import temp_database
logger = LYRICS_LOGGER
"""
@ -61,12 +63,12 @@ def fetch_single_lyrics(row: dict):
return
logger.info("found lyrics")
database.add_lyrics(id_, lyrics=lyrics[0])
temp_database.add_lyrics(id_, lyrics=lyrics[0])
add_lyrics(row['file'], lyrics[0])
def fetch_lyrics():
for row in database.get_tracks_for_lyrics():
for row in temp_database.get_tracks_for_lyrics():
fetch_single_lyrics(row)

View File

@ -3,17 +3,6 @@ print(mk.__path__)
# mk.cli()
mk.fetch_source.Download()
mk.lyrics.fetch_lyrics()
# db = mk.utils.shared.database
# if len(db.get_custom_track([])) == 0:
mk.target.set_target.UrlPath(genre="test")
mk.fetch_audio.Download()
if __name__ == "__main__":
db = mk.utils.shared.database
for elem in db.get_custom_track([]):
print(elem)
print()