1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00
maloja/maloja/upgrade.py

72 lines
2.2 KiB
Python
Raw Normal View History

2022-01-03 04:08:02 +03:00
# This module should take care of recognizing old install data and upgrading it before the actual server deals with it
import os
import re
from doreah.logging import log
2022-01-03 05:04:36 +03:00
from doreah.io import col
2022-01-03 04:08:02 +03:00
from .globalconf import data_dir, dir_settings
from . import apis
2022-01-03 04:08:02 +03:00
def upgrade_apikeys():
oldfile = os.path.join(dir_settings['config'],"clients","authenticated_machines.tsv")
if os.path.exists(oldfile):
try:
from doreah import tsv
clients = tsv.parse(oldfile,"string","string")
for key,identifier in clients:
apis.apikeystore[identifier] = key
2022-01-03 04:08:02 +03:00
os.remove(oldfile)
except:
pass
def upgrade_db(callback_add_scrobbles):
2022-01-07 23:47:55 +03:00
2022-01-03 04:08:02 +03:00
oldfolder = os.path.join(dir_settings['state'],"scrobbles")
2022-01-03 10:01:49 +03:00
newfolder = os.path.join(dir_settings['state'],".oldscrobbles")
2022-01-04 09:55:07 +03:00
os.makedirs(newfolder,exist_ok=True)
2022-01-03 04:08:02 +03:00
if os.path.exists(oldfolder):
2022-01-07 23:47:55 +03:00
scrobblefiles = [f for f in os.listdir(oldfolder) if f.endswith(".tsv")]
if len(scrobblefiles) > 0:
print(col['yellow']("Upgrading v2 Database to v3 Database. This could take a while..."))
idx = 0
2022-01-07 23:47:55 +03:00
for sf in scrobblefiles:
idx += 1
2022-01-03 04:08:02 +03:00
if re.match(r"[0-9]+_[0-9]+\.tsv",sf):
2022-01-03 10:01:49 +03:00
origin = 'legacy'
2022-01-03 04:08:02 +03:00
elif sf == "lastfmimport.tsv":
origin = 'lastfm-import'
else:
origin = 'unknown'
from doreah import tsv
scrobbles = tsv.parse(os.path.join(oldfolder,sf),"int","string","string","string","string",comments=False)
scrobblelist = []
print(f"\tImporting from {sf} ({idx}/{len(scrobblefiles)}) - {len(scrobbles)} Scrobbles")
2022-01-03 04:08:02 +03:00
for scrobble in scrobbles:
timestamp, artists, title, album, duration = scrobble
if album in ('-',''): album = None
if duration in ('-',''): duration = None
scrobblelist.append({
"time":int(timestamp),
"track":{
"artists":artists.split(''),
"title":title,
"length":None
},
"duration":duration,
2022-01-03 10:01:49 +03:00
"origin":origin,
"extra":{
"album":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
}
2022-01-03 04:08:02 +03:00
})
callback_add_scrobbles(scrobblelist)
2022-01-03 10:01:49 +03:00
os.rename(os.path.join(oldfolder,sf),os.path.join(newfolder,sf))
2022-01-07 23:47:55 +03:00
print(col['yellow']("Done!"))