continued the database integration
This commit is contained in:
parent
13e026d1e5
commit
ca8a3970a6
@ -7,7 +7,11 @@ from pkg_resources import resource_string
|
|||||||
|
|
||||||
from .song import (
|
from .song import (
|
||||||
Song,
|
Song,
|
||||||
Lyrics
|
Lyrics,
|
||||||
|
Metadata,
|
||||||
|
Target,
|
||||||
|
Artist,
|
||||||
|
Source
|
||||||
)
|
)
|
||||||
from .get_song import get_song_from_response
|
from .get_song import get_song_from_response
|
||||||
from ..utils.shared import (
|
from ..utils.shared import (
|
||||||
@ -285,31 +289,40 @@ WHERE '{track_id}' == id;
|
|||||||
self.cursor.execute(query, (file, path, genre))
|
self.cursor.execute(query, (file, path, genre))
|
||||||
self.connection.commit()
|
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):
|
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]):
|
def write_many_song(self, songs: List[Song]):
|
||||||
for song in songs:
|
for song in songs:
|
||||||
self.write_song(song=song)
|
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)
|
|
||||||
|
@ -13,7 +13,7 @@ from .song import (
|
|||||||
|
|
||||||
def get_song_from_response(response: dict) -> Song:
|
def get_song_from_response(response: dict) -> Song:
|
||||||
# artists
|
# 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 = Metadata()
|
metadata = Metadata()
|
||||||
@ -26,12 +26,10 @@ def get_song_from_response(response: dict) -> Song:
|
|||||||
for src in response['source']:
|
for src in response['source']:
|
||||||
if src['src'] is None:
|
if src['src'] is None:
|
||||||
continue
|
continue
|
||||||
sources.append(Source(src))
|
sources.append(Source(src=src['src'], url=src['url']))
|
||||||
|
|
||||||
# target
|
# target
|
||||||
target = Target()
|
target = Target(file=response['file'], path=response['path'])
|
||||||
target.set_file(response['file'])
|
|
||||||
target.set_path(response['path'])
|
|
||||||
|
|
||||||
# Lyrics
|
# Lyrics
|
||||||
lyrics_container = LyricsContainer()
|
lyrics_container = LyricsContainer()
|
||||||
|
@ -26,18 +26,19 @@ class Metadata:
|
|||||||
return self.data[item]
|
return self.data[item]
|
||||||
|
|
||||||
|
|
||||||
class Source:
|
class Source(DatabaseObject):
|
||||||
def __init__(self, src_data) -> None:
|
def __init__(self, id_: str = None, src: str = None, url: str = None) -> None:
|
||||||
self.src_data = src_data
|
super().__init__(id_=id_)
|
||||||
|
|
||||||
self.src = self.src_data['src']
|
self.src = src
|
||||||
self.url = self.src_data['url']
|
self.url = url
|
||||||
|
|
||||||
|
|
||||||
class Target:
|
class Target(DatabaseObject):
|
||||||
def __init__(self) -> None:
|
def __init__(self, id_:str = None, file: str = None, path: str = None) -> None:
|
||||||
self._file = None
|
super().__init__(id_=id_)
|
||||||
self._path = None
|
self._file = file
|
||||||
|
self._path = path
|
||||||
|
|
||||||
def set_file(self, _file: str):
|
def set_file(self, _file: str):
|
||||||
self._file = _file
|
self._file = _file
|
||||||
@ -74,9 +75,10 @@ class Target:
|
|||||||
exists_on_disc = property(fget=get_exists_on_disc)
|
exists_on_disc = property(fget=get_exists_on_disc)
|
||||||
|
|
||||||
|
|
||||||
class Artist:
|
class Artist(DatabaseObject):
|
||||||
def __init__(self, id_: str = None, name: str = None) -> None:
|
def __init__(self, id_: str = None, mb_id: str = None, name: str = None) -> None:
|
||||||
self.id = id_
|
super().__init__(id_=id_)
|
||||||
|
self.mb_id = mb_id
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def __eq__(self, __o: object) -> bool:
|
def __eq__(self, __o: object) -> bool:
|
||||||
@ -88,8 +90,9 @@ class Artist:
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class Lyrics:
|
class Lyrics(DatabaseObject):
|
||||||
def __init__(self, text: str, language: str) -> None:
|
def __init__(self, text: str, language: str, id_: str = None) -> None:
|
||||||
|
super().__init__(id_=id_)
|
||||||
self.text = text
|
self.text = text
|
||||||
self.language = language
|
self.language = language
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
DROP TABLE IF EXISTS artist;
|
DROP TABLE IF EXISTS artist;
|
||||||
CREATE TABLE artist (
|
CREATE TABLE artist (
|
||||||
id TEXT PRIMARY KEY NOT NULL,
|
id TEXT PRIMARY KEY NOT NULL,
|
||||||
|
mb_id TEXT,
|
||||||
name TEXT
|
name TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user