Skip to content

listenbrainz: add option to import listens from ListenBrainz data export zip#6606

Open
Maxr1998 wants to merge 5 commits intobeetbox:masterfrom
Maxr1998:lbimport-file
Open

listenbrainz: add option to import listens from ListenBrainz data export zip#6606
Maxr1998 wants to merge 5 commits intobeetbox:masterfrom
Maxr1998:lbimport-file

Conversation

@Maxr1998
Copy link
Copy Markdown
Contributor

@Maxr1998 Maxr1998 commented May 3, 2026

Description

Since importing listens from the API is rather slow and puts unnecessary load on the servers, importing a dump exported from https://listenbrainz.org/settings/export/ should also be possible. This change introduces the export-file parameter to the lbimport command, which takes all listens from the zipfile to update the play counts.

I also fixed an issue where mbid_mapping could be None if explicitly set to null in the JSON (which happens for these exports sometimes), and changed the recording mbid logic to prefer one explicitly supplied by the player in the additional_data, instead of relying on the (sometimes inaccurate) mapper.

Since the --max parameter isn't documented either, I'm unsure whether I should add the parameter to the plugin docs. In theory, the --help parameter already tells you enough.

I'm also not sure if the --max parameter should apply to exported files, especially since the order of zip file entries isn't deterministic.

To Do

  • Documentation. (If you've added a new command-line flag, for example, find the appropriate page under docs/ to describe it.)
  • Changelog. (Add an entry to docs/changelog.rst to the bottom of one of the lists near the top of the document.)
  • Tests. (Very much encouraged but not strictly required.)

Copilot AI review requested due to automatic review settings May 3, 2026 11:09
@Maxr1998 Maxr1998 requested a review from a team as a code owner May 3, 2026 11:09
@github-actions github-actions Bot added the listenbrainz listenbrainz plugin label May 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 3, 2026

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

grug see PR try make lbimport faster and nicer to ListenBrainz server. it add way to read listens from ListenBrainz export zip, then update play counts in beets.

Changes:

  • add lbimport --export-file PATH to read listens from ListenBrainz data export zip instead of API
  • add zip/jsonl import helper and wire it into _lbupdate
  • tweak MBID pick logic to handle mbid_mapping: null and prefer explicit MBID when present

Comment thread beetsplug/listenbrainz.py
Comment thread beetsplug/listenbrainz.py
Comment on lines +152 to +170
def import_listenbrainz_data_export(self, log, export_file):
"""Import ListenBrainz data from a .zip file."""
export_file = syspath(normpath(export_file))

all_listens = []

with zipfile.ZipFile(export_file, "r") as zip_file:
for file_name in zip_file.namelist():
if file_name.startswith("listens/") and file_name.endswith(
".jsonl"
):
log.info("Reading listens from {}", file_name)
with zip_file.open(file_name) as file:
data = file.read()
listens = [
json.loads(line) for line in data.splitlines()
]
all_listens.extend(listens)
return all_listens
Comment thread beetsplug/listenbrainz.py
Comment on lines +50 to +56
lbupdate_cmd.parser.add_option(
"--export-file",
dest="export_file",
metavar="PATH",
default=None,
help="path to a ListenBrainz data export .zip file (instead of fetching from the API)",
)
Comment thread beetsplug/listenbrainz.py
"""Update play counts from ListenBrainz listening history."""
if export_file is not None:
log.info("Importing ListenBrainz data from {}...", export_file)
listens = self.import_listenbrainz_data_export(log, export_file)
Comment thread beetsplug/listenbrainz.py Outdated
Comment thread beetsplug/listenbrainz.py Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 3, 2026

Codecov Report

❌ Patch coverage is 27.02703% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.99%. Comparing base (3d3f5e3) to head (6211961).

Files with missing lines Patch % Lines
beetsplug/listenbrainz.py 27.02% 25 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6606      +/-   ##
==========================================
- Coverage   72.07%   71.99%   -0.08%     
==========================================
  Files         159      159              
  Lines       20633    20664      +31     
  Branches     3273     3279       +6     
==========================================
+ Hits        14871    14878       +7     
- Misses       5053     5076      +23     
- Partials      709      710       +1     
Files with missing lines Coverage Δ
beetsplug/listenbrainz.py 49.29% <27.02%> (-4.56%) ⬇️
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

listenbrainz listenbrainz plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants