| 1 |
Extending the Autotagger |
| 2 |
======================== |
| 3 |
|
| 4 |
.. currentmodule:: beets.metadata_plugins |
| 5 |
|
| 6 |
Beets supports **metadata source plugins**, which allow it to fetch and match |
| 7 |
metadata from external services (such as Spotify, Discogs, or Deezer). This |
| 8 |
guide explains how to build your own metadata source plugin by extending either |
| 9 |
:py:class:`MetadataSourcePlugin` or :py:class:`SearchApiMetadataSourcePlugin`. |
| 10 |
|
| 11 |
These plugins integrate directly with the autotagger, providing candidate |
| 12 |
metadata during lookups. To implement one, you must subclass |
| 13 |
:py:class:`MetadataSourcePlugin` (or the search API helper base class) and |
| 14 |
implement its abstract methods. |
| 15 |
|
| 16 |
Overview |
| 17 |
-------- |
| 18 |
|
| 19 |
Creating a metadata source plugin is very similar to writing a standard plugin |
| 20 |
(see :ref:`basic-plugin-setup`). The main difference is that your plugin must: |
| 21 |
|
| 22 |
1. Subclass :py:class:`MetadataSourcePlugin` or |
| 23 |
:py:class:`SearchApiMetadataSourcePlugin`. |
| 24 |
2. Implement all required abstract methods. |
| 25 |
|
| 26 |
Here`s a minimal example: |
| 27 |
|
| 28 |
.. code-block:: python |
| 29 |
|
| 30 |
# beetsplug/myawesomeplugin.py |
| 31 |
from typing import Sequence |
| 32 |
from beets.library import Item |
| 33 |
from beets.metadata_plugins import MetadataSourcePlugin |
| 34 |
|
| 35 |
|
| 36 |
class MyAwesomePlugin(MetadataSourcePlugin): |
| 37 |
|
| 38 |
def candidates( |
| 39 |
self, |
| 40 |
items: Sequence[Item], |
| 41 |
artist: str, |
| 42 |
album: str, |
| 43 |
va_likely: bool, |
| 44 |
): ... |
| 45 |
|
| 46 |
def item_candidates(self, item: Item, artist: str, title: str): ... |
| 47 |
|
| 48 |
def track_for_id(self, track_id: str): ... |
| 49 |
|
| 50 |
def album_for_id(self, album_id: str): ... |
| 51 |
|
| 52 |
For API-backed metadata sources, prefer |
| 53 |
:py:class:`SearchApiMetadataSourcePlugin` to reuse shared search orchestration: |
| 54 |
|
| 55 |
.. code-block:: python |
| 56 |
|
| 57 |
from beets.metadata_plugins import SearchApiMetadataSourcePlugin, SearchParams |
| 58 |
|
| 59 |
|
| 60 |
class MyApiPlugin(SearchApiMetadataSourcePlugin): |
| 61 |
|
| 62 |
def get_search_query_with_filters(self, query_type, items, artist, name, va_likely): |
| 63 |
query = f'album:"{name}"' if query_type == "album" else name |
| 64 |
if query_type == "track" or not va_likely: |
| 65 |
query += f' artist:"{artist}"' |
| 66 |
return query, {} |
| 67 |
|
| 68 |
def get_search_response(self, params: SearchParams): |
| 69 |
# Execute provider API request and return results with "id" fields. |
| 70 |
... |
| 71 |
|
| 72 |
def track_for_id(self, track_id: str): ... |
| 73 |
|
| 74 |
def album_for_id(self, album_id: str): ... |
| 75 |
|
| 76 |
The shared base class centralizes query normalization, ``search_limit`` |
| 77 |
handling, candidate wiring, and consistent error logging for search requests. |
| 78 |
Provider-specific behavior is implemented in |
| 79 |
:py:meth:`~SearchApiMetadataSourcePlugin.get_search_query_with_filters` and |
| 80 |
:py:meth:`~SearchApiMetadataSourcePlugin.get_search_response`. |
| 81 |
|
| 82 |
Each metadata source plugin automatically gets a unique identifier. You can |
| 83 |
access this identifier using the :py:meth:`~MetadataSourcePlugin.data_source` |
| 84 |
class property to tell plugins apart. |
| 85 |
|
| 86 |
Metadata lookup |
| 87 |
--------------- |
| 88 |
|
| 89 |
When beets runs the autotagger, it queries **all enabled metadata source |
| 90 |
plugins** for potential matches: |
| 91 |
|
| 92 |
- For **albums**, it calls :py:meth:`~MetadataSourcePlugin.candidates`. |
| 93 |
- For **singletons**, it calls :py:meth:`~MetadataSourcePlugin.item_candidates`. |
| 94 |
|
| 95 |
The results are combined and scored. By default, candidate ranking is handled |
| 96 |
automatically by the beets core, but you can customize weighting by overriding: |
| 97 |
|
| 98 |
- :py:meth:`~MetadataSourcePlugin.album_distance` |
| 99 |
- :py:meth:`~MetadataSourcePlugin.track_distance` |
| 100 |
|
| 101 |
This is optional, if not overridden, both methods return a constant distance of |
| 102 |
`0.5`. |
| 103 |
|
| 104 |
ID-based lookups |
| 105 |
---------------- |
| 106 |
|
| 107 |
Your plugin must also define: |
| 108 |
|
| 109 |
- :py:meth:`~MetadataSourcePlugin.album_for_id` — fetch album metadata by ID. |
| 110 |
- :py:meth:`~MetadataSourcePlugin.track_for_id` — fetch track metadata by ID. |
| 111 |
|
| 112 |
IDs are expected to be strings. If your source uses specific formats, consider |
| 113 |
contributing an extractor regex to the core module: |
| 114 |
:py:mod:`beets.util.id_extractors`. |
| 115 |
|
| 116 |
When beets matches by explicit IDs (for example via ``--search-id`` or existing |
| 117 |
``mb_*id`` fields), it asks every enabled metadata source plugin for candidates |
| 118 |
using :py:meth:`~MetadataSourcePlugin.albums_for_ids` and |
| 119 |
:py:meth:`~MetadataSourcePlugin.tracks_for_ids`. Candidate identity is tracked |
| 120 |
by ``(data_source, id)``, so identical IDs from different providers remain |
| 121 |
separate options. |
| 122 |
|
| 123 |
If you need to query one specific provider, use the module helpers |
| 124 |
:py:func:`beets.metadata_plugins.album_for_id` and |
| 125 |
:py:func:`beets.metadata_plugins.track_for_id` and pass both the ID and the |
| 126 |
provider ``data_source`` name. |
| 127 |
|
| 128 |
Best practices |
| 129 |
-------------- |
| 130 |
|
| 131 |
Beets already ships with several metadata source plugins. Studying these |
| 132 |
implementations can help you follow conventions and avoid pitfalls. Good |
| 133 |
starting points include: |
| 134 |
|
| 135 |
- ``spotify`` |
| 136 |
- ``deezer`` |
| 137 |
- ``discogs`` |
| 138 |
|
| 139 |
Migration guidance |
| 140 |
------------------ |
| 141 |
|
| 142 |
Older metadata plugins that extend |BeetsPlugin| should be migrated to |
| 143 |
:py:class:`MetadataSourcePlugin`. API-backed sources should generally migrate to |
| 144 |
:py:class:`SearchApiMetadataSourcePlugin` to avoid duplicating search |
| 145 |
orchestration. Legacy support will be removed in **beets v3.0.0**. |
| 146 |
|
| 147 |
.. seealso:: |
| 148 |
|
| 149 |
- :py:mod:`beets.autotag` |
| 150 |
- :py:mod:`beets.metadata_plugins` |
| 151 |
- :ref:`autotagger_extensions` |
| 152 |
- :ref:`using-the-auto-tagger` |