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

Merge pull request #163 from krkk/import_listenbrainz

Implement importing scrobbles from ListenBrainz
This commit is contained in:
krateng 2022-08-20 16:55:32 +02:00 committed by GitHub
commit e3933e7dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -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)) * 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 [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 * 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. ⚠️ 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.

View File

@ -49,6 +49,11 @@ def import_scrobbles(inputf):
typeid,typedesc = "maloja","Maloja" typeid,typedesc = "maloja","Maloja"
importfunc = parse_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: else:
print("File",inputf,"could not be identified as a valid import source.") print("File",inputf,"could not be identified as a valid import source.")
return result return result
@ -84,7 +89,7 @@ def import_scrobbles(inputf):
"track":{ "track":{
"artists":scrobble['track_artists'], "artists":scrobble['track_artists'],
"title":scrobble['track_title'], "title":scrobble['track_title'],
"length":None "length":scrobble['track_length'],
}, },
"duration":scrobble['scrobble_duration'], "duration":scrobble['scrobble_duration'],
"origin":"import:" + typeid, "origin":"import:" + typeid,
@ -154,6 +159,7 @@ def parse_spotify_lite(inputf):
yield ("CONFIDENT_IMPORT",{ yield ("CONFIDENT_IMPORT",{
'track_title':title, 'track_title':title,
'track_artists': artist, 'track_artists': artist,
'track_length': None,
'scrobble_time': timestamp, 'scrobble_time': timestamp,
'scrobble_duration':played, 'scrobble_duration':played,
'album_name': None 'album_name': None
@ -262,6 +268,7 @@ def parse_spotify_full(inputf):
yield (status,{ yield (status,{
'track_title':title, 'track_title':title,
'track_artists': artist, 'track_artists': artist,
'track_length': None,
'album_name': album, 'album_name': album,
'scrobble_time': timestamp, 'scrobble_time': timestamp,
'scrobble_duration':played 'scrobble_duration':played
@ -294,6 +301,7 @@ def parse_lastfm(inputf):
yield ('CONFIDENT_IMPORT',{ yield ('CONFIDENT_IMPORT',{
'track_title': title, 'track_title': title,
'track_artists': artist, 'track_artists': artist,
'track_length': None,
'album_name': album, 'album_name': album,
'scrobble_time': int(datetime.datetime.strptime( 'scrobble_time': int(datetime.datetime.strptime(
time + '+0000', time + '+0000',
@ -305,6 +313,28 @@ def parse_lastfm(inputf):
yield ('FAIL',None,f"{row} (Line {line}) could not be parsed. Scrobble not imported. ({repr(e)})") yield ('FAIL',None,f"{row} (Line {line}) could not be parsed. Scrobble not imported. ({repr(e)})")
continue 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): def parse_maloja(inputf):
@ -318,6 +348,7 @@ def parse_maloja(inputf):
yield ('CONFIDENT_IMPORT',{ yield ('CONFIDENT_IMPORT',{
'track_title': s['track']['title'], 'track_title': s['track']['title'],
'track_artists': s['track']['artists'], 'track_artists': s['track']['artists'],
'track_length': s['track']['length'],
'album_name': s['track'].get('album',{}).get('name',''), 'album_name': s['track'].get('album',{}).get('name',''),
'scrobble_time': s['time'], 'scrobble_time': s['time'],
'scrobble_duration': s['duration'] 'scrobble_duration': s['duration']