Debian Codegraph @

docs/plugins/lastgenre.rst

raw
1 LastGenre Plugin
2 ================
3
4 The ``lastgenre`` plugin fetches *tags* from Last.fm_ and assigns them as genres
5 to your albums and items.
6
7 .. _last.fm: https://www.last.fm/
8
9 Installation
10 ------------
11
12 To use the ``lastgenre`` plugin, first enable it in your configuration (see
13 :ref:`using-plugins`). Then, install ``beets`` with ``lastgenre`` extra
14
15 .. code-block:: bash
16
17 pip install "beets[lastgenre]"
18
19 Usage
20 -----
21
22 The plugin chooses genres based on a *whitelist*, meaning that only certain tags
23 can be considered genres. This way, tags like "my favorite music" or "seen live"
24 won't be considered genres. The plugin ships with a fairly extensive `internal
25 whitelist`_, but you can set your own in the config file using the ``whitelist``
26 configuration value or forgo a whitelist altogether by setting the option to
27 ``no``.
28
29 The genre list file should contain one genre per line. Blank lines are ignored.
30 For the curious, the default genre list is generated by a `script that scrapes
31 Wikipedia`_.
32
33 .. _internal whitelist: https://raw.githubusercontent.com/beetbox/beets/master/beetsplug/lastgenre/genres.txt
34
35 .. _script that scrapes wikipedia: https://gist.github.com/sampsyo/1241307
36
37 Canonicalization
38 ~~~~~~~~~~~~~~~~
39
40 The plugin can also *canonicalize* genres, meaning that more obscure genres can
41 be turned into coarser-grained ones that are present in the whitelist. This
42 works using a `tree of nested genre names`_, represented using YAML_, where the
43 leaves of the tree represent the most specific genres.
44
45 The most common way to use this would be with a custom whitelist containing only
46 a desired subset of genres. Consider for a example this minimal whitelist:
47
48 ::
49
50 rock
51 heavy metal
52 pop
53
54 together with the default genre tree. Then an item that has its genre specified
55 as *viking metal* would actually be tagged as *heavy metal* because neither
56 *viking metal* nor its parent *black metal* are in the whitelist. It always
57 tries to use the most specific genre that's available in the whitelist.
58
59 The relevant subtree path in the default tree looks like this:
60
61 ::
62
63 - rock:
64 - heavy metal:
65 - black metal:
66 - viking metal
67
68 Considering that, it's not very useful to use the default whitelist (which
69 contains about any genre contained in the tree) with canonicalization because
70 nothing would ever be matched to a more generic node since all the specific
71 subgenres are in the whitelist to begin with.
72
73 If you use canonicalization *without* a whitelist, the plugin will simply map
74 every genre to its top-most root category in the tree (e.g., ``Viking Metal`` →
75 ``Rock``). This is a great way to keep your library broad without needing to
76 maintain a manual list of allowed genres.
77
78 .. _tree of nested genre names: https://raw.githubusercontent.com/beetbox/beets/master/beetsplug/lastgenre/genres-tree.yaml
79
80 .. _yaml: https://yaml.org/
81
82 Genre Source
83 ~~~~~~~~~~~~
84
85 When looking up genres for albums or individual tracks, you can choose whether
86 to use Last.fm tags on the album, the artist, or the track. For example, you
87 might want all the albums for a certain artist to carry the same genre. The
88 default is "album". When set to "track", the plugin will fetch *both*
89 album-level and track-level genres for your music when importing albums.
90
91 Multiple Genres
92 ~~~~~~~~~~~~~~~
93
94 By default, the plugin chooses the most popular tag on Last.fm as a genre. If
95 you prefer to use a *list* of popular genre tags, you can increase the number of
96 the ``count`` config option.
97
98 Lists of up to *count* genres will be stored in the ``genres`` field as a list
99 and written to your media files as separate genre tags.
100
101 Last.fm_ provides a popularity factor, a.k.a. *weight*, for each tag ranging
102 from 100 for the most popular tag down to 0 for the least popular. The plugin
103 uses this weight to discard unpopular tags. The default is to ignore tags with a
104 weight less then 10. You can change this by setting the ``min_weight`` config
105 option.
106
107 Specific vs. Popular Genres
108 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
109
110 By default, the plugin sorts genres by popularity. However, you can use the
111 ``prefer_specific`` option to override this behavior and instead sort genres by
112 specificity, as determined by your whitelist and canonicalization tree.
113
114 For instance, say you have both ``folk`` and ``americana`` in your whitelist and
115 canonicalization tree and ``americana`` is a leaf within ``folk``. If Last.fm
116 returns both of those tags, lastgenre is going to use the most popular, which is
117 often the most generic (in this case ``folk``). By setting ``prefer_specific``
118 to true, lastgenre would use ``americana`` instead.
119
120 Handling pre-populated tags
121 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
122
123 The ``force``, ``keep_existing`` and ``whitelist`` options control how
124 pre-existing genres are handled.
125
126 As you would assume, setting ``force: no`` **won't touch pre-existing genre
127 tags** and will only **fetch new genres for empty tags**. When ``force`` is
128 ``yes`` the setting of the ``whitelist`` option (as documented in Usage_)
129 applies to any existing or newly fetched genres.
130
131 The following configurations are possible:
132
133 **Setup 1** (default)
134
135 Add new last.fm genres when **empty**. Any present tags stay **untouched**.
136
137 .. code-block:: yaml
138
139 force: no
140 keep_existing: no
141
142 **Setup 2**
143
144 **Overwrite all**. Only fresh last.fm genres remain.
145
146 .. code-block:: yaml
147
148 force: yes
149 keep_existing: no
150
151 **Setup 3**
152
153 **Combine** genres in present tags with new ones (be aware of that with an
154 enabled ``whitelist`` setting, of course some genres might get cleaned up -
155 existing genres take precedence over new ones though. To make sure any existing
156 genres remain, set ``whitelist: no``).
157
158 .. code-block:: yaml
159
160 force: yes
161 keep_existing: yes
162
163 .. attention::
164
165 If ``force`` is disabled the ``keep_existing`` option is simply ignored
166 (since ``force: no`` means ``not touching`` existing tags anyway).
167
168 Genre Ignorelist
169 ----------------
170
171 Last.fm tags are crowd-sourced, so they can be wrong — especially for artists
172 whose names are shared with or confused with others. For example, a "Drum And
173 Bass" artist named "Fracture" might incorrectly receive "Metal" tags. The
174 ignorelist lets you reject specific genres globally or per-artist.
175
176 Another example for the ignorelist is to exclude genres that are technically
177 correct but not useful to you. For example, you might want to exclude "Ska" for
178 "Bob Marley", even though it is a valid genre for his music.
179
180 Filtering is done in two places: when fetching genres from Last.fm and when
181 resolving to a final genre list (during canonicalization and whitelisting).
182
183 This means that existing genres are also filtered when ``force`` and
184 ``keep_existing`` options are enabled (or ``cleanup_existing`` is enabled with
185 ``force`` disabled).
186
187 To enable this feature, add an ``ignorelist`` section to your ``lastgenre``
188 configuration:
189
190 .. code-block:: yaml
191
192 lastgenre:
193 ignorelist:
194 fracture:
195 - ^(heavy|black|power|death)?\s?(metal|rock)$|\w+-metal\d*$
196 - progressive metal
197 bob marley:
198 - ska
199 '*':
200 - electronic
201
202 A combination of regex patterns and plain genre names is possible. The ``'*'``
203 key applies globally to all artists — use it to block genres you never want,
204 regardless of artist. Patterns are matched against the full genre string, so a
205 plain ``metal`` will not match ``heavy metal`` unless you write a regex like
206 ``.*metal``.
207
208 .. attention::
209
210 - The global key ``'*'`` **must** be surrounded by single quotes so that
211 YAML does not interpret it as an anchor.
212 - Any regex pattern that starts with a special YAML character (``[``, ``*``,
213 or ``:``) or ends with ``:`` **must** be surrounded by quotes.
214 - Prefer **single quotes** (``'...'``) when quoting is necessary, as they
215 treat backslashes literally (no double-escaping required).
216 - Because the ignorelist uses plain YAML, you do **not** need to
217 double-escape backslashes in unquoted or single-quoted strings (e.g., use
218 ``\w``, not ``\\w``).
219
220 Genre Normalization (Aliases)
221 -----------------------------
222
223 Last.fm tags often contain variant spellings, abbreviations, or inconsistent
224 formatting (e.g., "hip-hop", "hiphop", and "hip hop"). The normalization feature
225 uses an ordered list of regular expression aliases to map these variants to a
226 single canonical name *before* any other filtering or canonicalization takes
227 place.
228
229 This feature is enabled by default and uses a built-in alias table that covers
230 many common cases, such as mapping "dnb" to "drum and bass" or "r&b" to "rhythm
231 and blues". Set ``aliases: no`` to turn it off entirely.
232
233 You can replace these aliases with your own by setting ``aliases`` to an inline
234 mapping in your configuration. The keys are the canonical genre names (which
235 support ``\1`` / ``\g<N>`` back-references to regex capture groups) and the
236 values are lists of regex patterns.
237
238 .. note::
239
240 The same formatting and quoting rules regarding YAML special characters and
241 backslashes mentioned in the **Attention** box in the **Genre Ignorelist**
242 section apply here as well.
243
244 The full default mappings are shown below - copy and paste this into your config
245 below the ``lastgenre.aliases`` section to customize it:
246
247 .. literalinclude:: ../../beetsplug/lastgenre/aliases.yaml
248 :language: yaml
249
250 Choosing the Right Tool
251 -----------------------
252
253 With multiple ways to filter and map genres, here is a quick guide on when to
254 use what:
255
256 - **Aliases**: Use these first to fix spelling variants and abbreviations (e.g.,
257 ``dnb`` → ``drum and bass``).
258 - **Ignorelist**: Use this for error correction when Last.fm results are not
259 accurate, or for precise per-artist or global exclusions (e.g., rejecting
260 ``Metal`` for specific electronic artists).
261 - **Canonicalization**: Use this to automatically map specific sub-genres to
262 broader categories (e.g., ``Grindcore`` → ``Metal``).
263 - **Whitelist**: Use this to finally limit your library to a predefined set of
264 genres. When combined with canonicalization, the plugin will try to map a
265 sub-genre to its closest whitelisted parent. Anything else is dropped.
266
267 Configuration
268 -------------
269
270 To configure the plugin, make a ``lastgenre:`` section in your configuration
271 file. Default configuration:
272
273 .. code-block:: yaml
274
275 lastgenre:
276 auto: yes
277 canonical: no
278 cleanup_existing: no
279 count: 1
280 fallback: null
281 force: no
282 keep_existing: no
283 min_weight: 10
284 prefer_specific: no
285 source: album
286 whitelist: yes
287 title_case: yes
288 ignorelist: no
289 aliases: yes
290
291 The available options are:
292
293 - **auto**: Fetch genres automatically during import. Default: ``yes``.
294 - **canonical**: Use a canonicalization tree. Setting this to ``yes`` will use a
295 built-in tree. You can also set it to a path, like the ``whitelist`` config
296 value, to use your own tree. Default: ``no`` (disabled).
297 - **cleanup_existing**: This option only takes effect with ``force: no``,
298 Setting this to ``yes`` will result in cleanup of existing genres. That
299 includes canonicalization and whitelisting, if enabled. If no matching genre
300 can be determined, the ``fallback`` is used instead. Default: ``no``
301 (disabled).
302 - **count**: Number of genres to fetch. Default: 1
303 - **fallback**: A string to use as a fallback genre when no genre is found
304 ``or`` the original genre is not desired to be kept (``keep_existing: no``).
305 You can use the empty string ``''`` to reset the genre. Default: None.
306 - **force**: By default, lastgenre will fetch new genres for empty tags only,
307 enable this option to always try to fetch new last.fm genres. Enable the
308 ``keep_existing`` option to combine existing and new genres. (see `Handling
309 pre-populated tags`_). Default: ``no``.
310 - **keep_existing**: This option alters the ``force`` behavior. If both
311 ``force`` and ``keep_existing`` are enabled, existing genres are combined with
312 new ones. Depending on the ``whitelist`` setting, existing and new genres are
313 filtered accordingly. To ensure only fresh last.fm genres, disable this
314 option. (see `Handling pre-populated tags`_) Default: ``no``.
315 - **min_weight**: Minimum popularity factor below which genres are discarded.
316 Default: 10.
317 - **prefer_specific**: Sort genres by the most to least specific, rather than
318 most to least popular. Note that this option requires a ``canonical`` tree,
319 and if not configured it will automatically enable and use the built-in tree.
320 Default: ``no``.
321 - **source**: Which entity to look up in Last.fm. Can be either ``artist``,
322 ``album`` or ``track``. Default: ``album``.
323 - **whitelist**: The filename of a custom genre list, ``yes`` to use the
324 internal whitelist, or ``no`` to consider all genres valid. Default: ``yes``.
325 - **title_case**: Convert the new tags to TitleCase before saving. Default:
326 ``yes``.
327 - **ignorelist**: A mapping of artist names (or the global ``'*'`` key) to lists
328 of genres to exclude. See `Genre Ignorelist`_ for more details. Default:
329 ``no``.
330 - **aliases**: Controls genre alias normalization. Set to ``yes`` (default) to
331 use the built-in alias table, ``no`` to disable normalization entirely, or an
332 inline mapping of canonical genre names to lists of regex patterns to replace
333 the built-in table with your own. See `Genre Normalization (Aliases)`_ for
334 details.
335
336 Running Manually
337 ----------------
338
339 In addition to running automatically on import, the plugin can also be run
340 manually from the command line. Use the command ``beet lastgenre [QUERY]`` to
341 fetch genres for albums or items matching a certain query.
342
343 By default, ``beet lastgenre`` matches albums. To match individual tracks or
344 singletons, use the ``-A`` switch: ``beet lastgenre -A [QUERY]``.
345
346 To preview the changes that would be made without applying them, use the ``-p``
347 or ``--pretend`` flag. This shows which genres would be set but does not write
348 or store any changes.
349
350 To disable automatic genre fetching on import, set the ``auto`` config option to
351 false.
352
353 Tuning Logs
354 -----------
355
356 To enable tuning logs, run ``beet -vvv lastgenre ...`` or ``beet -vvv import
357 ...``. This enables additional messages at the ``DEBUG`` log level, showing for
358 example what data was received from last.fm at each stage of genre fetching
359 (artist, album, and track levels) before any canonicalization or whitelist
360 filtering is applied. Tuning logs are useful for adjusting the plugin’s settings
361 and understanding its behavior, though they can be quite verbose.