Pylistenbrainz import lastfm csv with this csv format

Per Feature request - Import loved tracks from LastFM to ListenBrainz plus questions about doing it manually and Usage Examples — ListenBrainz 0.1.0 documentation, it sounds like there’s no way to import loved tracks without having recording MBIDs.

I don’t use LB so I haven’t tested that the submission code works, but I think that something similar to this should work for importing the listens without the “loved” data:

#!/usr/bin/env python3

import csv
import pylistenbrainz
import sys
import time

MAX_LISTENS_PER_REQUEST = 1000

if len(sys.argv) != 2:
    print("Usage: %s <csv-file>" % sys.argv[0], file=sys.stderr)
    sys.exit(2)

auth_token = input('Please enter your auth token: ')
client = pylistenbrainz.ListenBrainz()
client.set_auth_token(auth_token)

def submit_listens(listens):
    print("Submitting %d listen(s)..." % len(listens))
    response = client.submit_multiple_listens(listens)
    assert response['status'] == 'ok'

listens = []
with open(sys.argv[1], newline='') as f:
    reader = csv.reader(f, dialect='unix')
    next(reader, None)  # skip header row
    for row in reader:
        tm = time.strptime(row[3], '%Y-%m-%d %H:%M:%S')
        listens.append(pylistenbrainz.Listen(
            track_name=row[0],
            artist_name=row[1],
            release_name=row[2],
            listened_at=int(time.mktime(tm)),
        ))
        if len(listens) >= MAX_LISTENS_PER_REQUEST:
            submit_listens(listens)
            listens = []

if len(listens) > 0:
    submit_listens(listens)

Presumably it’d be possible to run a separate small script that uses the metadata endpoint as described at Usage Examples — ListenBrainz 0.1.0 documentation to look up MBIDs and submit the “loved” data.

1 Like