continued the database integration

This commit is contained in:
Lars Noack 2022-12-01 16:14:59 +01:00
parent 13e026d1e5
commit ca8a3970a6
4 changed files with 58 additions and 43 deletions

View File

@ -7,7 +7,11 @@ from pkg_resources import resource_string
from .song import (
Song,
Lyrics
Lyrics,
Metadata,
Target,
Artist,
Source
)
from .get_song import get_song_from_response
from ..utils.shared import (
@ -285,31 +289,40 @@ WHERE '{track_id}' == id;
self.cursor.execute(query, (file, path, genre))
self.connection.commit()
def write_target(self, song_id: str, target: Target):
query = f"UPDATE track SET file = ?, path = ? WHERE '{song_id}' == id;"
self.cursor.execute(query, (target.file, target.path))
self.connection.commit()
def write_artist(self, artist: Artist, song_id: str = None, release_group_id: str = None):
artist_id = artist.id
query = "INSERT OR REPLACE INTO artist (id, mb_id, name) VALUES (?, ?, ?);"
self.cursor.execute(query, (artist_id, artist.mb_id, artist.name))
self.connection.commit()
if song_id is not None:
adjacency_query = "INSERT OR REPLACE INTO artist_track (artist_id, track_id) VALUES (?, ?);"
self.cursor.execute(adjacency_query, (artist_id, song_id))
self.connection.commit()
if release_group_id is not None:
adjacency_query = "INSERT OR REPLACE INTO artist_release_group (artist_id, release_group_id) VALUES (?, ?);"
self.cursor.execute(adjacency_query, (artist_id, release_group_id))
self.connection.commit()
def write_many_artists(self, song_id: str, artist_list: List[Artist]):
for artist in artist_list:
self.write_artist(song_id=song_id, artist=artist)
def write_song(self, song: Song):
pass
song_id = song.id
# write artists
self.write_many_artists(song_id=song_id, artist_list=song.artists)
# write target
self.write_target(song_id=song_id, target=song.target)
def write_many_song(self, songs: List[Song]):
for song in songs:
self.write_song(song=song)
if __name__ == "__main__":
import tempfile
temp_folder = "music-downloader"
temp_dir = os.path.join(tempfile.gettempdir(), temp_folder)
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
temp_dir = get_temp_dir()
DATABASE_FILE = "metadata.db"
DATABASE_STRUCTURE_FILE = "database_structure.sql"
db_path = os.path.join(TEMP_DIR, DATABASE_FILE)
logging.basicConfig()
logger = logging.getLogger("database")
logger.setLevel(logging.DEBUG)
database = Database(os.path.join(temp_dir, "metadata.db"), os.path.join(temp_dir, "database_structure.sql"), logger,
reset_anyways=True)

View File

@ -13,7 +13,7 @@ from .song import (
def get_song_from_response(response: dict) -> Song:
# artists
artists = [Artist(id_=a['id'], name=a['name']) for a in response['artists']]
artists = [Artist(id_=a['id'], mb_id=a['id'], name=a['name']) for a in response['artists']]
# metadata
metadata = Metadata()
@ -26,12 +26,10 @@ def get_song_from_response(response: dict) -> Song:
for src in response['source']:
if src['src'] is None:
continue
sources.append(Source(src))
sources.append(Source(src=src['src'], url=src['url']))
# target
target = Target()
target.set_file(response['file'])
target.set_path(response['path'])
target = Target(file=response['file'], path=response['path'])
# Lyrics
lyrics_container = LyricsContainer()

View File

@ -26,18 +26,19 @@ class Metadata:
return self.data[item]
class Source:
def __init__(self, src_data) -> None:
self.src_data = src_data
class Source(DatabaseObject):
def __init__(self, id_: str = None, src: str = None, url: str = None) -> None:
super().__init__(id_=id_)
self.src = self.src_data['src']
self.url = self.src_data['url']
self.src = src
self.url = url
class Target:
def __init__(self) -> None:
self._file = None
self._path = None
class Target(DatabaseObject):
def __init__(self, id_:str = None, file: str = None, path: str = None) -> None:
super().__init__(id_=id_)
self._file = file
self._path = path
def set_file(self, _file: str):
self._file = _file
@ -74,9 +75,10 @@ class Target:
exists_on_disc = property(fget=get_exists_on_disc)
class Artist:
def __init__(self, id_: str = None, name: str = None) -> None:
self.id = id_
class Artist(DatabaseObject):
def __init__(self, id_: str = None, mb_id: str = None, name: str = None) -> None:
super().__init__(id_=id_)
self.mb_id = mb_id
self.name = name
def __eq__(self, __o: object) -> bool:
@ -88,8 +90,9 @@ class Artist:
return self.name
class Lyrics:
def __init__(self, text: str, language: str) -> None:
class Lyrics(DatabaseObject):
def __init__(self, text: str, language: str, id_: str = None) -> None:
super().__init__(id_=id_)
self.text = text
self.language = language

View File

@ -1,6 +1,7 @@
DROP TABLE IF EXISTS artist;
CREATE TABLE artist (
id TEXT PRIMARY KEY NOT NULL,
mb_id TEXT,
name TEXT
);