From 9b10ca4a5d7b547951ced5c7feda9741c0dd9d93 Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Tue, 16 Aug 2022 19:13:10 +0200 Subject: [PATCH] Implement importing scrobbles from ListenBrainz Closes: #162 --- README.md | 1 + maloja/proccontrol/tasks/import_scrobbles.py | 27 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 7e8f83b..9d54108 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ If you would like to import your previous scrobbles, use the command `maloja imp * a Last.fm export generated by [benfoxall's website](https://benjaminbenben.com/lastfm-to-csv/) ([GitHub page](https://github.com/benfoxall/lastfm-to-csv)) * an official [Spotify data export file](https://www.spotify.com/us/account/privacy/) +* an official [ListenBrainz export file](https://listenbrainz.org/profile/export/) * the export of another Maloja instance ⚠️ Never import your data while maloja is running. When you need to do import inside docker container start it in shell mode instead and perform import before starting the container as mentioned above. diff --git a/maloja/proccontrol/tasks/import_scrobbles.py b/maloja/proccontrol/tasks/import_scrobbles.py index 748d993..b5bf620 100644 --- a/maloja/proccontrol/tasks/import_scrobbles.py +++ b/maloja/proccontrol/tasks/import_scrobbles.py @@ -49,6 +49,11 @@ def import_scrobbles(inputf): typeid,typedesc = "maloja","Maloja" importfunc = parse_maloja + # username_lb-YYYY-MM-DD.json + elif re.match(".*_lb-[0-9-]+\.json",filename): + typeid,typedesc = "listenbrainz","ListenBrainz" + importfunc = parse_listenbrainz + else: print("File",inputf,"could not be identified as a valid import source.") return result @@ -308,6 +313,28 @@ def parse_lastfm(inputf): yield ('FAIL',None,f"{row} (Line {line}) could not be parsed. Scrobble not imported. ({repr(e)})") continue +def parse_listenbrainz(inputf): + + with open(inputf,'r') as inputfd: + data = json.load(inputfd) + + for entry in data: + + try: + track_metadata = entry['track_metadata'] + additional_info = track_metadata.get('additional_info', {}) + + yield ("CONFIDENT_IMPORT",{ + 'track_title': track_metadata['track_name'], + 'track_artists': additional_info.get('artist_names') or track_metadata['artist_name'], + 'track_length': int(additional_info.get('duration_ms', 0) / 1000) or additional_info.get('duration'), + 'album_name': track_metadata.get('release_name'), + 'scrobble_time': entry['listened_at'], + 'scrobble_duration': None, + },'') + except Exception as e: + yield ('FAIL',None,f"{entry} could not be parsed. Scrobble not imported. ({repr(e)})") + continue def parse_maloja(inputf):