No description
Find a file
Šarūnas Nejus 1a899cc92a
Drop dependency on python-musicbrainzngs (#6234)
## Replace `python-musicbrainzngs` with Custom Lightweight MusicBrainz
Client

### Core Problem Solved

**Before**: Beets depended on the external `python-musicbrainzngs`
library (v0.7.1) for all MusicBrainz API interactions. This dependency
required separate installation for multiple plugins and introduced an
abstraction layer that obscured direct HTTP semantics.

**After**: Custom **lightweight MusicBrainz client**
(`beetsplug.utils.musicbrainz`) built directly on `requests` and
`requests-ratelimiter`, eliminating the external dependency while
maintaining full API compatibility.

---

### Architecture Overview

#### **1. New MusicBrainz Client Foundation**

Created `beetsplug/utils/musicbrainz.py` with three core components:

```
MusicBrainzAPI (base client)
├─ Configuration-driven initialization (from config['musicbrainz'])
├─ Rate-limited session via LimiterTimeoutSession
├─ Generic entity fetching (get_entity, search_entity)
├─ Specialized methods (get_release, get_recording, get_work)
└─ Recursive relation grouping (group_relations)

MusicBrainzUserAPI (authenticated operations)
├─ Extends MusicBrainzAPI with HTTPDigestAuth
├─ Collection management (get_collections)
└─ User-specific API operations

MBCollection (collection manipulation)
├─ Paginated release fetching
├─ Chunked PUT operations (add_releases)
└─ Chunked DELETE operations (remove_releases)
```

#### **2. Config parsing now centralized in
`MusicBrainzAPI.__post_init__()`**.

And applies to all plugins.

#### **3. Mixin Pattern for Plugin Integration**

```python
# OLD: Plugins directly instantiated musicbrainzngs
import musicbrainzngs
musicbrainzngs.auth(user, pass)
resp = musicbrainzngs.get_recording_by_id(id, includes=['releases'])

# NEW: Plugins inherit MusicBrainzAPIMixin
class MyPlugin(MusicBrainzAPIMixin, BeetsPlugin):
    def some_method(self):
        recording = self.mbapi.get_recording(id, includes=['releases'])
```

**Affected plugins**:

- `musicbrainz` → `MusicBrainzAPIMixin`
- `listenbrainz` → `MusicBrainzAPIMixin`
- `mbcollection` → `MusicBrainzUserAPIMixin` (requires authentication)
- `missing` → `MusicBrainzAPIMixin`
- `parentwork` → `MusicBrainzAPIMixin`

---

### Plugin-Specific Refactoring

#### **`mbcollection`: From Procedural to Object-Oriented**

**Before**: Module-level functions (`submit_albums`, `mb_call`) with
error handling scattered across multiple try-except blocks.

**After**: `MBCollection` dataclass encapsulating collection operations:

```python
# OLD approach
collection_id = get_collection()
albums_in_collection = get_albums_in_collection(collection_id)
submit_albums(collection_id, album_ids)

# NEW approach
collection = self.collection  # cached property with validation
collection.add_releases(album_ids)
collection.remove_releases(removed_ids)
```

**Key improvements**:

- Eliminated `mb_call` error wrapper (errors propagate naturally from
`RequestHandler`)
- Consolidated pagination logic into `MBCollection.releases` property
- Type hints for `Library`, `ImportSession`, `ImportTask` parameters

#### **`listenbrainz`: Search Query Simplification**

**Before**: Called `musicbrainzngs.search_recordings()` with constructed
query strings.

**After**: Uses `self.mbapi.search_entity()` which handles query
formatting:

```python
# OLD
resp = musicbrainzngs.search_recordings(
    query=f'track:{track_name} AND release:{album_name}',
    strict=True
)
if resp.get('recording-count') > 1:
    return resp['recording-list'][0].get('id')

# NEW
for recording in self.mbapi.search_entity('recording', 
                                           {'track': track_name, 
                                            'release': album_name}):
    return recording['id']
return None
```

#### **`parentwork`: Inline Traversal Logic**

**Before**: Module-level functions (`direct_parent_id`,
`work_parent_id`, `find_parent_work_info`) that made sequential API
calls.

**After**: Single method `find_parent_work_info` that traverses work
hierarchy inline:

```python
def find_parent_work_info(self, mbworkid: str) -> tuple[dict, str | None]:
    workdate = None
    parent_id = mbworkid
    
    while parent_id := current_id:  # walrus operator
        workinfo = self.mbapi.get_work(current_id, includes=['work-rels', 'artist-rels'])
        workdate = workdate or extract_composer_date(workinfo)
        parent_id = find_parent_in_relations(workinfo)
    
    return workinfo, workdate
```

Eliminates three function calls per traversal level.

---

### Dependency & Installation Impact

#### **Package Dependencies**

```diff
# pyproject.toml
-musicbrainzngs = {version = ">=0.4", optional = true}

# Removed from extras groups
-listenbrainz = ["musicbrainzngs"]
-mbcollection = ["musicbrainzngs"]
-missing = ["musicbrainzngs"]
-parentwork = ["musicbrainzngs"]
```

**Result**: Four plugin extras no longer require external package
installation beyond `requests` (already a core dependency).

#### **CI Workflow**

```diff
# .github/workflows/ci.yaml
-poetry install --extras=parentwork
+poetry install  # parentwork now part of core
```

The `parentwork` extra is removed entirely since it had no other
dependencies.

#### **Documentation Updates**

Removed "Installation" sections from four plugin docs that previously
required:

```bash
pip install beets[listenbrainz]  # NO LONGER NEEDED
pip install beets[mbcollection]  # NO LONGER NEEDED
pip install beets[missing]       # NO LONGER NEEDED
pip install beets[parentwork]    # NO LONGER NEEDED
```

Plugins now work **out-of-the-box** with just `plugins: [listenbrainz,
...]` in config.

---

### Testing Improvements

#### **New Shared Fixture: `requests_mock`**

Created `test/plugins/conftest.py` with fixture that disables rate
limiting during tests:

```python
@pytest.fixture
def requests_mock(requests_mock, monkeypatch):
    """Use plain session wherever MB requests are mocked."""
    monkeypatch.setattr(
        'beetsplug.utils.musicbrainz.MusicBrainzAPI.session',
        requests.Session,
    )
    return requests_mock
```

This avoids artificial delays when mocking HTTP responses.

#### **New Plugin Test Suites**

Added comprehensive tests for previously untested plugins:

1. **`test_listenbrainz.py`**: Tests recording ID lookup and track info
fetching
2. **`test_mbcollection.py`**: Tests collection validation, pagination,
and sync operations
3. **`test_missing.py`**: Tests missing album detection logic
4. **`test/plugins/utils/test_musicbrainz.py`**: Tests `group_relations`
transformation

#### **Test Migration**

Moved `test_group_relations` from `test_musicbrainz.py` to
`test/plugins/utils/test_musicbrainz.py` (84 lines) since
`group_relations` is now a utility function.

---

### Migration Benefits

| **Aspect** | **Before** | **After** |
| ------------------------- | ------------------------------------ |
------------------------------------------- |
| **External dependencies** | `python-musicbrainzngs` (0.7.1) | None
(uses existing `requests`) |
| **Plugin installation** | `pip install beets[plugin]` required | Works
with base install |
| **API surface area** | ~50 functions in musicbrainzngs | ~10 methods
tailored to Beets |
| **Error messages** | Generic exceptions with status codes | Full HTTP
response text included |
| **Response structure** | Raw MusicBrainz JSON | Normalized with
grouped relations |
| **Code ownership** | External maintenance dependency | Direct control
over API client |
| **Test speed** | Rate-limited even with mocks | Fixture disables
limits for mocked requests |
| **Type safety** | Minimal type hints in musicbrainzngs | Full type
hints (`JSONDict`, `list[str]`) |

---

### Backward Compatibility

** Fully backward compatible**:

- All existing plugin APIs unchanged from user perspective
- Configuration keys remain identical (`musicbrainz.user`,
`musicbrainz.pass`, etc.)
- MusicBrainz API responses maintain same structure (with additional
normalization)
- Test suite passes without modification to integration tests

**Breaking changes**: None from end-user perspective.


closes #6265
2026-01-07 11:25:00 +00:00
.github Migrate parentwork to use MusicBrainzAPI 2026-01-06 00:27:36 +00:00
beets Improve and simplify show_model_changes 2025-12-27 14:30:35 +00:00
beetsplug Add request handler utils to the docs 2026-01-06 09:54:02 +00:00
docs Fix changelog formatting 2026-01-06 09:54:02 +00:00
extra pyupgrade Python 3.10 2025-11-08 12:09:52 +00:00
test Retry on server errors too 2026-01-06 09:54:02 +00:00
.git-blame-ignore-revs Update git blame ignore revs 2026-01-06 09:54:02 +00:00
.gitignore fix transaction context manager signature 2025-10-19 15:07:17 +02:00
.pre-commit-config.yaml Configure docstrfmt 2025-08-10 16:25:04 +01:00
.readthedocs.yaml Fix path typo 2023-09-22 15:29:39 -04:00
CODE_OF_CONDUCT.rst Reformat all docs using docstrfmt 2025-08-10 16:25:05 +01:00
codecov.yml Make cov setup a bit more useful and upgrade cov upload action 2025-08-09 15:11:59 +01:00
CONTRIBUTING.rst Update python version references 2025-11-08 12:09:52 +00:00
LICENSE Update copyright dates to 2016 2015-12-30 15:42:06 +00:00
poetry.lock Add request handler utils to the docs 2026-01-06 09:54:02 +00:00
pyproject.toml Add request handler utils to the docs 2026-01-06 09:54:02 +00:00
README.rst docs: Fix link to plugin development docs 2025-12-02 11:40:18 +01:00
README_kr.rst docs: Fix link to plugin development docs 2025-12-02 11:40:18 +01:00
SECURITY.md Create security policy 2021-12-22 09:34:41 -08:00
setup.cfg Upload test results to codecov 2025-08-09 15:27:17 +01:00

.. image:: https://img.shields.io/pypi/v/beets.svg
    :target: https://pypi.python.org/pypi/beets

.. image:: https://img.shields.io/codecov/c/github/beetbox/beets.svg
    :target: https://codecov.io/github/beetbox/beets

.. image:: https://img.shields.io/github/actions/workflow/status/beetbox/beets/ci.yaml
    :target: https://github.com/beetbox/beets/actions

.. image:: https://repology.org/badge/tiny-repos/beets.svg
    :target: https://repology.org/project/beets/versions

beets
=====

Beets is the media library management system for obsessive music geeks.

The purpose of beets is to get your music collection right once and for all. It
catalogs your collection, automatically improving its metadata as it goes. It
then provides a suite of tools for manipulating and accessing your music.

Here's an example of beets' brainy tag corrector doing its thing:

::

    $ beet import ~/music/ladytron
    Tagging:
        Ladytron - Witching Hour
    (Similarity: 98.4%)
     * Last One Standing      -> The Last One Standing
     * Beauty                 -> Beauty*2
     * White Light Generation -> Whitelightgenerator
     * All the Way            -> All the Way...

Because beets is designed as a library, it can do almost anything you can
imagine for your music collection. Via plugins_, beets becomes a panacea:

- Fetch or calculate all the metadata you could possibly need: `album art`_,
  lyrics_, genres_, tempos_, ReplayGain_ levels, or `acoustic fingerprints`_.
- Get metadata from MusicBrainz_, Discogs_, and Beatport_. Or guess metadata
  using songs' filenames or their acoustic fingerprints.
- `Transcode audio`_ to any format you like.
- Check your library for `duplicate tracks and albums`_ or for `albums that are
  missing tracks`_.
- Clean up crufty tags left behind by other, less-awesome tools.
- Embed and extract album art from files' metadata.
- Browse your music library graphically through a Web browser and play it in any
  browser that supports `HTML5 Audio`_.
- Analyze music files' metadata from the command line.
- Listen to your library with a music player that speaks the MPD_ protocol and
  works with a staggering variety of interfaces.

If beets doesn't do what you want yet, `writing your own plugin`_ is shockingly
simple if you know a little Python.

.. _acoustic fingerprints: https://beets.readthedocs.org/page/plugins/chroma.html

.. _album art: https://beets.readthedocs.org/page/plugins/fetchart.html

.. _albums that are missing tracks: https://beets.readthedocs.org/page/plugins/missing.html

.. _beatport: https://www.beatport.com

.. _discogs: https://www.discogs.com/

.. _duplicate tracks and albums: https://beets.readthedocs.org/page/plugins/duplicates.html

.. _genres: https://beets.readthedocs.org/page/plugins/lastgenre.html

.. _html5 audio: https://html.spec.whatwg.org/multipage/media.html#the-audio-element

.. _lyrics: https://beets.readthedocs.org/page/plugins/lyrics.html

.. _mpd: https://www.musicpd.org/

.. _musicbrainz: https://musicbrainz.org/

.. _musicbrainz music collection: https://musicbrainz.org/doc/Collections/

.. _plugins: https://beets.readthedocs.org/page/plugins/

.. _replaygain: https://beets.readthedocs.org/page/plugins/replaygain.html

.. _tempos: https://beets.readthedocs.org/page/plugins/acousticbrainz.html

.. _transcode audio: https://beets.readthedocs.org/page/plugins/convert.html

.. _writing your own plugin: https://beets.readthedocs.org/page/dev/plugins/index.html

Install
-------

You can install beets by typing ``pip install beets`` or directly from Github
(see details here_). Beets has also been packaged in the `software
repositories`_ of several distributions. Check out the `Getting Started`_ guide
for more information.

.. _getting started: https://beets.readthedocs.org/page/guides/main.html

.. _here: https://beets.readthedocs.io/en/latest/faq.html#run-the-latest-source-version-of-beets

.. _software repositories: https://repology.org/project/beets/versions

Contribute
----------

Thank you for considering contributing to ``beets``! Whether you're a programmer
or not, you should be able to find all the info you need at CONTRIBUTING.rst_.

.. _contributing.rst: https://github.com/beetbox/beets/blob/master/CONTRIBUTING.rst

Read More
---------

Learn more about beets at `its Web site`_. Follow `@b33ts`_ on Mastodon for news
and updates.

.. _@b33ts: https://fosstodon.org/@beets

.. _its web site: https://beets.io/

Contact
-------

- Encountered a bug you'd like to report? Check out our `issue tracker`_!

  - If your issue hasn't already been reported, please `open a new ticket`_ and
    we'll be in touch with you shortly.
  - If you'd like to vote on a feature/bug, simply give a :+1: on issues you'd
    like to see prioritized over others.
  - Need help/support, would like to start a discussion, have an idea for a new
    feature, or would just like to introduce yourself to the team? Check out
    `GitHub Discussions`_!

.. _github discussions: https://github.com/beetbox/beets/discussions

.. _issue tracker: https://github.com/beetbox/beets/issues

.. _open a new ticket: https://github.com/beetbox/beets/issues/new/choose

Authors
-------

Beets is by `Adrian Sampson`_ with a supporting cast of thousands.

.. _adrian sampson: https://www.cs.cornell.edu/~asampson/