maloja/maloja/upgrade.py

91 lines
2.9 KiB
Python
Raw Permalink 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
import csv
2022-01-03 04:08:02 +03:00
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 .pkg_global.conf import data_dir, dir_settings
from .apis import _apikeys
2022-01-03 04:08:02 +03:00
# Dealing with old style tsv files - these should be phased out everywhere
def read_tsvs(path,types):
result = []
for f in os.listdir(path):
if f.split('.')[-1].lower() != 'tsv': continue
filepath = os.path.join(path,f)
result += read_tsv(filepath,types)
return result
def read_tsv(filename,types):
with open(filename,'r') as filed:
reader = csv.reader(filed,delimiter="\t")
rawentries = [[col for col in entry if col] for entry in reader if len(entry)>0 and not entry[0].startswith('#')]
converted_entries = [[coltype(col) for col,coltype in zip(entry,types)] for entry in rawentries]
return converted_entries
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:
entries = read_tsv(oldfile)
for key,identifier in entries:
_apikeys.apikeystore[identifier] = key
os.remove(oldfile)
2022-04-24 20:41:55 +03:00
except Exception:
2022-01-03 04:08:02 +03:00
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-04-12 21:33:36 +03:00
newfolder = os.path.join(dir_settings['state'],".v2scrobbles")
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:
2022-04-08 22:17:17 +03:00
log("Upgrading v2 Database to v3 Database. This could take a while...",color='yellow')
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":
2022-04-07 07:09:07 +03:00
origin = 'import:lastfm'
elif sf == "spotifyimport.tsv":
origin = 'import:spotify'
2022-01-03 04:08:02 +03:00
else:
origin = 'unknown'
scrobbles = read_tsv(os.path.join(oldfolder,sf),[int,str,str,str,str])
#scrobbles = tsv.parse(os.path.join(oldfolder,sf),"int","string","string","string","string",comments=False)
2022-01-03 04:08:02 +03:00
scrobblelist = []
2022-04-08 22:17:17 +03:00
log(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 + [None,None]
2022-01-03 04:08:02 +03:00
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":{
2022-04-07 07:09:07 +03:00
"album_name":album
2022-01-03 10:01:49 +03:00
# 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-04-08 22:17:17 +03:00
log("Done!",color='yellow')