started on an sqllite db

This commit is contained in:
Lars Noack 2022-10-27 15:00:24 +02:00
parent 595eeac880
commit 035a45b7d8
3 changed files with 63 additions and 6 deletions

View File

@ -18,10 +18,10 @@ TEMP_DIR = get_temp_dir()
DATABASE_FILE = "metadata.db"
db_path = os.path.join(TEMP_DIR, DATABASE_FILE)
sqliteConnection = sqlite3.connect(db_path)
cursor = sqliteConnection.cursor()
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
def init_db(cursor, reset_anyways: bool = False):
def init_db(cursor, connection, reset_anyways: bool = False):
# check if db exists
exists = True
try:
@ -43,7 +43,37 @@ def init_db(cursor, reset_anyways: bool = False):
query = database_structure_file.read()
cursor.executescript(query)
init_db(cursor=cursor)
init_db(cursor=cursor, connection=connection, reset_anyways=True)
def add_artist(
musicbrainz_artistid: str,
artist: str = None
):
query = "INSERT INTO artist (id, name) VALUES (?, ?);"
values = musicbrainz_artistid, artist
cursor.execute(query, values)
connection.commit()
def add_release_group(
musicbrainz_releasegroupid: str,
artist_ids: list,
albumartist: str = None,
albumsort: int = None,
musicbrainz_albumtype: str = None,
compilation: str = None
):
# add adjacency
adjacency_list = []
for artist_id in artist_ids:
adjacency_list.append((musicbrainz_releasegroupid, artist_id))
adjacency_values = tuple(adjacency_list)
adjacency_query = "INSERT INTO artist_release_group (artist_id, release_group_id) VALUES (?, ?);"
cursor.executemany(adjacency_query, adjacency_values)
connection.commit()
# add release group
query = "INSERT INTO release_group (id, albumartist, albumsort, musicbrainz_albumtype, compilation) VALUES (?, ?);"
if __name__ == "__main__":
pass

View File

@ -9,6 +9,7 @@ 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,
@ -18,7 +19,10 @@ CREATE TABLE artist_track (
DROP TABLE IF EXISTS release_group;
CREATE TABLE release_group (
id TEXT PRIMARY KEY NOT NULL,
name TEXT
albumartist TEXT,
albumsort INT,
musicbrainz_albumtype TEXT,
compilation TEXT
);
DROP TABLE IF EXISTS release_;

View File

@ -1,3 +1,4 @@
import imp
from typing import List
import musicbrainzngs
@ -8,6 +9,7 @@ from datetime import date
import sqlite3
from object_handeling import get_elem_from_obj, parse_music_brainz_date
import database
# I don't know if it would be feesable to set up my own mb instance
# https://github.com/metabrainz/musicbrainz-docker
@ -39,7 +41,9 @@ class Artist:
self.artist = get_elem_from_obj(artist_data, ['name'])
logging.info(f"artist: {self}")
self.save()
# STARTING TO FETCH' RELEASE GROUPS. IMPORTANT: DON'T WRITE ANYTHING BESIDES THAT HERE
if not new_release_groups:
return
# sort all release groups by date and add album sort to have them in chronological order.
@ -55,6 +59,13 @@ class Artist:
albumsort=i + 1
))
def save(self):
logging.info(f"artist: {self}")
database.add_artist(
musicbrainz_artistid=self.musicbrainz_artistid,
artist=self.artist
)
def __str__(self):
newline = "\n"
return f"id: {self.musicbrainz_artistid}\nname: {self.artist}\n{newline.join([str(release_group) for release_group in self.release_groups])}"
@ -94,6 +105,8 @@ class ReleaseGroup:
self.musicbrainz_albumtype = get_elem_from_obj(release_group_data, ['primary-type'])
self.compilation = "1" if self.musicbrainz_albumtype == "Compilation" else None
self.save()
if only_download_distinct_releases:
self.append_distinct_releases(release_datas)
else:
@ -111,6 +124,16 @@ class ReleaseGroup:
self.artists.append(new_artist)
return new_artist
def save(self):
database.add_release_group(
musicbrainz_releasegroupid=self.musicbrainz_releasegroupid,
artist_ids=[artist.musicbrainz_artistid for artist in self.artists],
albumartist = self.albumartist,
albumsort = self.albumsort,
musicbrainz_albumtype = self.musicbrainz_albumtype,
compilation=self.compilation
)
def append_release(self, release_data: dict):
musicbrainz_albumid = get_elem_from_obj(release_data, ['id'])
if musicbrainz_albumid is None: