From c150a57090614210d689228f2279163f1fed3021 Mon Sep 17 00:00:00 2001 From: krateng Date: Fri, 1 Apr 2022 19:28:13 +0200 Subject: [PATCH] Implemented importing from Spotify's one-year data export --- maloja/proccontrol/tasks/importer.py | 37 ++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/maloja/proccontrol/tasks/importer.py b/maloja/proccontrol/tasks/importer.py index 69e3d3d..9247c3c 100644 --- a/maloja/proccontrol/tasks/importer.py +++ b/maloja/proccontrol/tasks/importer.py @@ -85,6 +85,7 @@ def import_scrobbles(inputf): # Format fields for tsv scrobble['timestamp'] = str(scrobble['timestamp']) scrobble['duration'] = str(scrobble['duration']) if scrobble['duration'] is not None else '-' + scrobble['album'] = scrobble['album'] if scrobble['album'] is not None else '-' (artists,scrobble['title']) = c.fullclean(scrobble['artiststr'],scrobble['title']) scrobble['artiststr'] = "␟".join(artists) @@ -112,7 +113,39 @@ def parse_spotify_lite(inputf): if not ask("Import " + ", ".join(col['yellow'](i) for i in inputfiles) + "?",default=True): inputfiles = [inputf] - # TODO + for inputf in inputfiles: + + print("Importing",col['yellow'](inputf),"...") + with open(inputf,'r') as inputfd: + data = json.load(inputfd) + + for entry in data: + + try: + played = int(entry['msPlayed'] / 1000) + timestamp = int( + datetime.datetime.strptime(entry['endTime'],"%Y-%m-%d %H:%M").timestamp() + ) + artist = entry['artistName'] + title = entry['trackName'] + + if played < 30: + skip(f"{entry} is shorter than 30 seconds, skipping...") + yield ('CONFIDENT_SKIP',None) + continue + + yield ("CONFIDENT_IMPORT",{ + 'title':title, + 'artiststr': artist, + 'timestamp': timestamp, + 'duration':played, + 'album': None + }) + except Exception as e: + err(f"{entry} could not be parsed. Scrobble not imported. ({repr(e)})") + yield ('FAIL',None) + continue + def parse_spotify_full(inputf): @@ -178,7 +211,7 @@ def parse_spotify_full(inputf): else: timestamp = int( - datetime.datetime.strptime(entry['ts'].replace('Z','+0000',),"%Y-%m-%dT%H:%M:%S%z").timestamp() + datetime.datetime.strptime(entry['ts'].replace('Z','+0000'),"%Y-%m-%dT%H:%M:%S%z").timestamp() )