From bd29c1e1ba927afb9f37f4875c17855d3af34faf Mon Sep 17 00:00:00 2001 From: krateng Date: Tue, 5 Apr 2022 20:51:14 +0200 Subject: [PATCH] Implemented extra information field in DB --- maloja/database/sqldb.py | 12 ++++++++---- maloja/proccontrol/tasks/import_scrobbles.py | 12 +++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index d47ab47..eed75ed 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -68,10 +68,10 @@ DBTABLES = { DB = {} - engine = sql.create_engine(f"sqlite:///{data_dir['scrobbles']('malojadb.sqlite')}", echo = False) meta = sql.MetaData() +# create table definitions for tablename in DBTABLES: DB[tablename] = sql.Table( @@ -81,8 +81,10 @@ for tablename in DBTABLES: **DBTABLES[tablename]['extrakwargs'] ) +# actually create tables for new databases meta.create_all(engine) +# upgrade database with new columns with engine.begin() as conn: for tablename in DBTABLES: info = DBTABLES[tablename] @@ -159,7 +161,8 @@ def scrobbles_db_to_dict(rows): "time":row.timestamp, "track":tracks[row.track_id], "duration":row.duration, - "origin":row.origin + "origin":row.origin, + "extra":json.loads(row.extra or '{}') } for row in rows ] @@ -198,11 +201,12 @@ def artist_db_to_dict(row): def scrobble_dict_to_db(info): return { - "rawscrobble":json.dumps(info), "timestamp":info['time'], "origin":info['origin'], "duration":info['duration'], - "track_id":get_track_id(info['track']) + "track_id":get_track_id(info['track']), + "extra":json.dumps(info.get('extra',{})), + "rawscrobble":json.dumps(info) } def track_dict_to_db(info): diff --git a/maloja/proccontrol/tasks/import_scrobbles.py b/maloja/proccontrol/tasks/import_scrobbles.py index 2b4e92a..b068070 100644 --- a/maloja/proccontrol/tasks/import_scrobbles.py +++ b/maloja/proccontrol/tasks/import_scrobbles.py @@ -71,6 +71,12 @@ def import_scrobbles(inputf): # clean up (scrobble['artists'],scrobble['title']) = c.fullclean(scrobble['artists'],scrobble['title']) + # extra info + extrainfo = {} + if scrobble.get('album'): extrainfo['album_name'] = scrobble['album'] + # saving this in the scrobble instead of the track because for now it's not meant + # to be authorative information, just payload of the scrobble + scrobblebuffer.append({ "time":scrobble['timestamp'], "track":{ @@ -80,11 +86,7 @@ def import_scrobbles(inputf): }, "duration":scrobble['duration'], "origin":"import:" + typeid, - "extra":{ - "album":scrobble['album'] - # saving this in the scrobble instead of the track because for now it's not meant - # to be authorative information, just payload of the scrobble - } + "extra":extrainfo }) if (result['CONFIDENT_IMPORT'] + result['UNCERTAIN_IMPORT']) % 1000 == 0: