diff --git a/sql.db b/sql.db new file mode 100644 index 0000000..e69de29 diff --git a/src/metadata/database.py b/src/metadata/database.py new file mode 100644 index 0000000..25b4e11 --- /dev/null +++ b/src/metadata/database.py @@ -0,0 +1,49 @@ +import sqlite3 +import os +import logging + +logging.basicConfig(level=logging.DEBUG) + +def get_temp_dir(): + 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) + return temp_dir + +DATABASE_STRUCTURE_FILE = "src/metadata/database_structure.sql" +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() + +def init_db(cursor, reset_anyways: bool = False): + # check if db exists + exists = True + try: + query = 'SELECT * FROM track;' + cursor.execute(query) + _ = cursor.fetchall() + except sqlite3.OperationalError: + exists = False + + if not exists: + logging.info("Database does not exist yet.") + + if reset_anyways or not exists: + # reset the database if reset_anyways is true or if an error has been thrown previously. + logging.info("Creating/Reseting Database.") + + # read the file + with open(DATABASE_STRUCTURE_FILE, "r") as database_structure_file: + query = database_structure_file.read() + cursor.executescript(query) + +init_db(cursor=cursor) + +if __name__ == "__main__": + pass diff --git a/src/metadata/database_structure.sql b/src/metadata/database_structure.sql new file mode 100644 index 0000000..91c7c5f --- /dev/null +++ b/src/metadata/database_structure.sql @@ -0,0 +1,37 @@ +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, + name TEXT +); + +DROP TABLE IF EXISTS release_; +CREATE TABLE release_ ( + id TEXT PRIMARY KEY NOT NULL, + release_group TEXT NOT NULL, + name TEXT +); + +DROP TABLE IF EXISTS track; +CREATE TABLE track ( + id TEXT PRIMARY KEY NOT NULL, + release_id TEXT NOT NULL, + name TEXT +); + diff --git a/src/metadata/download.py b/src/metadata/download.py index 08ed57d..f800c60 100644 --- a/src/metadata/download.py +++ b/src/metadata/download.py @@ -5,8 +5,13 @@ import pandas as pd import logging from datetime import date +import sqlite3 + from object_handeling import get_elem_from_obj, parse_music_brainz_date +# I don't know if it would be feesable to set up my own mb instance +# https://github.com/metabrainz/musicbrainz-docker + mb_log = logging.getLogger("musicbrainzngs") mb_log.setLevel(logging.WARNING) musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader") @@ -34,6 +39,7 @@ class Artist: self.artist = get_elem_from_obj(artist_data, ['name']) + logging.info(f"artist: {self}") if not new_release_groups: return # sort all release groups by date and add album sort to have them in chronological order. @@ -150,6 +156,7 @@ class Release: self.title = get_elem_from_obj(release_data, ['title']) self.copyright = get_elem_from_obj(label_data, [0, 'label', 'name']) + logging.info(f"release {self}") self.append_recordings(recording_datas) def append_recordings(self, recording_datas: dict): @@ -417,7 +424,17 @@ def download_track(mb_id, is_various_artist: bool = None, track: int = None, tot if __name__ == "__main__": + """ + import tempfile + import os + + TEMP_FOLDER = "music-downloader" + TEMP_DIR = os.path.join(tempfile.gettempdir(), TEMP_FOLDER) + if not os.path.exists(TEMP_DIR): + os.mkdir(TEMP_DIR) + """ logging.basicConfig(level=logging.DEBUG) + sqliteConnection = sqlite3.connect('sql.db') download({'id': '5cfecbe4-f600-45e5-9038-ce820eedf3d1', 'type': 'artist'}) # download({'id': '4b9af532-ef7e-42ab-8b26-c466327cb5e0', 'type': 'release'})