-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmusicbrainz.py
More file actions
745 lines (612 loc) · 18.8 KB
/
musicbrainz.py
File metadata and controls
745 lines (612 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
"""Helpers for communicating with the MusicBrainz webservice.
Provides rate-limited HTTP session and convenience methods to fetch and
normalize API responses.
This module centralizes request handling and response shaping so callers can
work with consistently structured data without embedding HTTP or rate-limit
logic throughout the codebase.
"""
from __future__ import annotations
import operator
import re
from dataclasses import dataclass, field
from functools import cached_property, singledispatchmethod, wraps
from itertools import groupby, starmap
from typing import TYPE_CHECKING, Any, Literal, ParamSpec, TypedDict, TypeVar
from requests_ratelimiter import LimiterMixin
from typing_extensions import NotRequired, Unpack
from beets import config, logging
from .requests import RequestHandler, TimeoutAndRetrySession
if TYPE_CHECKING:
from collections.abc import Callable
from requests import Response
from .._typing import JSONDict
log = logging.getLogger("beets")
LUCENE_SPECIAL_CHAR_PAT = re.compile(r'([-+&|!(){}[\]^"~*?:\\/])')
RELEASE_INCLUDES = [
"artists",
"media",
"recordings",
"release-groups",
"labels",
"artist-credits",
"aliases",
"recording-level-rels",
"work-rels",
"work-level-rels",
"artist-rels",
"isrcs",
"url-rels",
"release-rels",
"genres",
"tags",
]
RECORDING_INCLUDES = [
"artists",
"aliases",
"isrcs",
"work-level-rels",
"artist-rels",
]
class LimiterTimeoutSession(LimiterMixin, TimeoutAndRetrySession):
"""HTTP session that enforces rate limits."""
Entity = Literal[
"area",
"artist",
"collection",
"event",
"genre",
"instrument",
"label",
"place",
"recording",
"release",
"release-group",
"series",
"work",
"url",
]
class LookupKwargs(TypedDict, total=False):
includes: NotRequired[list[str]]
class PagingKwargs(TypedDict, total=False):
limit: NotRequired[int]
offset: NotRequired[int]
class SearchKwargs(PagingKwargs):
query: NotRequired[str]
class BrowseKwargs(LookupKwargs, PagingKwargs, total=False):
pass
class BrowseReleaseGroupsKwargs(BrowseKwargs, total=False):
artist: NotRequired[str]
collection: NotRequired[str]
release: NotRequired[str]
class BrowseRecordingsKwargs(BrowseReleaseGroupsKwargs, total=False):
work: NotRequired[str]
P = ParamSpec("P")
R = TypeVar("R")
class _Period(TypedDict):
begin: str | None
end: str | None
ended: bool
class Alias(_Period):
locale: str | None
name: str
primary: bool | None
sort_name: str
type: (
Literal[
"Artist name",
"Label name",
"Legal name",
"Recording name",
"Release name",
"Release group name",
"Search hint",
]
| None
)
type_id: str | None
class Artist(TypedDict):
country: str | None
disambiguation: str
id: str
name: str
sort_name: str
type: (
Literal["Character", "Choir", "Group", "Orchestra", "Other", "Person"]
| None
)
type_id: str | None
aliases: NotRequired[list[Alias]]
genres: NotRequired[list[Genre]]
tags: NotRequired[list[Tag]]
class ArtistCredit(TypedDict):
artist: Artist
joinphrase: str
name: str
class Genre(TypedDict):
count: int
disambiguation: str
id: str
name: str
class Tag(TypedDict):
count: int
name: str
ReleaseStatus = Literal[
"Bootleg",
"Cancelled",
"Expunged",
"Official",
"Promotion",
"Pseudo-Release",
"Withdrawn",
]
ReleasePackaging = Literal[
"Book",
"Box",
"Cardboard/Paper Sleeve",
"Cassette Case",
"Clamshell Case",
"Digibook",
"Digifile",
"Digipak",
"Discbox Slider",
"Fatbox",
"Gatefold Cover",
"Jewel Case",
"None",
"Keep Case",
"Longbox",
"Metal Tin",
"Other",
"Plastic Sleeve",
"Slidepack",
"Slipcase",
"Snap Case",
"SnapPack",
"Slim Jewel Case",
"Super Jewel Box",
]
ReleaseQuality = Literal["high", "low", "normal"]
class ReleaseGroup(TypedDict):
aliases: list[Alias]
artist_credit: list[ArtistCredit]
disambiguation: str
first_release_date: str
genres: list[Genre]
id: str
primary_type: Literal["Album", "Broadcast", "EP", "Other", "Single"] | None
primary_type_id: str | None
secondary_type_ids: list[str]
secondary_types: list[
Literal[
"Audiobook",
"Audio drama",
"Compilation",
"DJ-mix",
"Demo",
"Field recording",
"Interview",
"Live",
"Mixtape/Street",
"Remix",
"Soundtrack",
"Spokenword",
]
]
tags: list[Tag]
title: str
class CoverArtArchive(TypedDict):
artwork: bool
back: bool
count: int
darkened: bool
front: bool
class TextRepresentation(TypedDict):
language: str | None
script: str | None
class Area(TypedDict):
disambiguation: str
id: str
iso_3166_1_codes: list[str]
iso_3166_2_codes: NotRequired[list[str]]
name: str
sort_name: str
type: None
type_id: None
class ReleaseEvent(TypedDict):
area: Area | None
date: str
class Label(TypedDict):
aliases: list[Alias]
disambiguation: str
genres: list[Genre]
id: str
label_code: int | None
name: str
sort_name: str
tags: list[Tag]
type: (
Literal[
"Bootleg Production",
"Broadcaster",
"Distributor",
"Holding",
"Imprint",
"Manufacturer",
"Original Production",
"Publisher",
"Reissue Production",
"Rights Society",
]
| None
)
type_id: str | None
class LabelInfo(TypedDict):
catalog_number: str | None
label: Label
class Url(TypedDict):
id: str
resource: str
class RelationBase(_Period):
attribute_ids: dict[str, str]
attribute_values: dict[str, str]
attributes: list[str]
direction: Literal["backward", "forward"]
source_credit: str
target_credit: str
type_id: str
ArtistRelationType = Literal[
"arranger",
"art direction",
"artwork",
"composer",
"conductor",
"copyright",
"design",
"design/illustration",
"editor",
"engineer",
"graphic design",
"illustration",
"instrument",
"instrument arranger",
"liner notes",
"lyricist",
"mastering",
"misc",
"mix",
"mix-DJ",
"performer",
"phonographic copyright",
"photography",
"previous attribution",
"producer",
"programming",
"recording",
"remixer",
"sound",
"vocal",
"vocal arranger",
"writer",
]
class ArtistRelation(RelationBase):
type: ArtistRelationType
artist: Artist
attribute_credits: NotRequired[dict[str, str]]
class UrlRelation(RelationBase):
type: Literal[
"IMDB samples",
"IMDb",
"allmusic",
"amazon asin",
"discography entry",
"discogs",
"download for free",
"fanpage",
"free streaming",
"lyrics",
"other databases",
"purchase for download",
"purchase for mail-order",
"secondhandsongs",
"show notes",
"songfacts",
"streaming",
"wikidata",
"wikipedia",
]
url: Url
class WorkRelation(RelationBase):
type: Literal[
"adaptation",
"arrangement",
"based on",
"included works",
"lyrical quotation",
"medley",
"musical quotation",
"named after work",
"orchestration",
"other version",
"parts",
"revision of",
]
ordering_key: NotRequired[int]
work: Work
class Work(TypedDict):
attributes: list[str]
disambiguation: str
id: str
iswcs: list[str]
language: str | None
languages: list[str]
title: str
type: str | None
type_id: str | None
artist_relations: NotRequired[list[ArtistRelation]]
url_relations: NotRequired[list[UrlRelation]]
work_relations: NotRequired[list[WorkRelation]]
class Recording(TypedDict):
aliases: list[Alias]
artist_credit: list[ArtistCredit]
disambiguation: str
id: str
isrcs: list[str]
length: int | None
title: str
video: bool
artist_relations: NotRequired[list[ArtistRelation]]
first_release_date: NotRequired[str]
genres: NotRequired[list[Genre]]
tags: NotRequired[list[Tag]]
url_relations: NotRequired[list[UrlRelation]]
work_relations: NotRequired[list[WorkRelation]]
class Track(TypedDict):
artist_credit: list[ArtistCredit]
id: str
length: int | None
number: str
position: int
recording: Recording
title: str
class Medium(TypedDict):
format: str | None
format_id: str | None
id: str
position: int
title: str
track_count: int
data_tracks: NotRequired[list[Track]]
pregap: NotRequired[Track]
track_offset: NotRequired[int]
tracks: NotRequired[list[Track]]
class ReleaseRelationRelease(TypedDict):
artist_credit: list[ArtistCredit]
barcode: str | None
country: str | None
date: str
disambiguation: str
id: str
media: list[Medium]
packaging: ReleasePackaging | None
packaging_id: str | None
quality: ReleaseQuality
release_events: list[ReleaseEvent]
release_group: ReleaseGroup
status: ReleaseStatus | None
status_id: str | None
text_representation: TextRepresentation
title: str
class ReleaseRelation(RelationBase):
type: Literal["remaster", "transl-tracklisting", "replaced by"]
release: ReleaseRelationRelease
class Release(TypedDict):
aliases: list[Alias]
artist_credit: list[ArtistCredit]
asin: str | None
barcode: str | None
cover_art_archive: CoverArtArchive
disambiguation: str
genres: list[Genre]
id: str
label_info: list[LabelInfo]
media: list[Medium]
packaging: ReleasePackaging | None
packaging_id: str | None
quality: ReleaseQuality
release_group: ReleaseGroup
status: ReleaseStatus | None
status_id: str | None
tags: list[Tag]
text_representation: TextRepresentation
title: str
artist_relations: NotRequired[list[ArtistRelation]]
country: NotRequired[str | None]
date: NotRequired[str]
release_events: NotRequired[list[ReleaseEvent]]
release_relations: NotRequired[list[ReleaseRelation]]
url_relations: NotRequired[list[UrlRelation]]
def require_one_of(*keys: str) -> Callable[[Callable[P, R]], Callable[P, R]]:
required = frozenset(keys)
def deco(func: Callable[P, R]) -> Callable[P, R]:
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
# kwargs is a real dict at runtime; safe to inspect here
if not required & kwargs.keys():
required_str = ", ".join(sorted(required))
raise ValueError(
f"At least one of {required_str} filter is required"
)
return func(*args, **kwargs)
return wrapper
return deco
@dataclass
class MusicBrainzAPI(RequestHandler):
"""High-level interface to the MusicBrainz WS/2 API.
Responsibilities:
- Configure the API host and request rate from application configuration.
- Offer helpers to fetch common entity types and to run searches.
- Normalize MusicBrainz responses so relation lists are grouped by target
type for easier downstream consumption.
Documentation: https://musicbrainz.org/doc/MusicBrainz_API
"""
api_host: str = field(init=False)
rate_limit: float = field(init=False)
def __post_init__(self) -> None:
mb_config = config["musicbrainz"]
mb_config.add(
{
"host": "musicbrainz.org",
"https": False,
"ratelimit": 1,
"ratelimit_interval": 1,
}
)
hostname = mb_config["host"].as_str()
if hostname == "musicbrainz.org":
self.api_host, self.rate_limit = "https://musicbrainz.org", 1.0
else:
https = mb_config["https"].get(bool)
self.api_host = f"http{'s' if https else ''}://{hostname}"
self.rate_limit = (
mb_config["ratelimit"].get(int)
/ mb_config["ratelimit_interval"].as_number()
)
@cached_property
def api_root(self) -> str:
return f"{self.api_host}/ws/2"
def create_session(self) -> LimiterTimeoutSession:
return LimiterTimeoutSession(per_second=self.rate_limit)
def request(self, *args, **kwargs) -> Response:
"""Ensure all requests specify JSON response format by default."""
kwargs.setdefault("params", {})
kwargs["params"]["fmt"] = "json"
return super().request(*args, **kwargs)
def _get_resource(
self, resource: str, includes: list[str] | None = None, **kwargs
) -> JSONDict:
"""Retrieve and normalize data from the API resource endpoint.
If requested, includes are appended to the request. The response is
passed through a normalizer that groups relation entries by their
target type so that callers receive a consistently structured mapping.
"""
if includes:
kwargs["inc"] = "+".join(includes)
return self._normalize_data(
self.get_json(f"{self.api_root}/{resource}", params=kwargs)
)
def _lookup(
self, entity: Entity, id_: str, **kwargs: Unpack[LookupKwargs]
) -> Any:
return self._get_resource(f"{entity}/{id_}", **kwargs)
def _browse(self, entity: Entity, **kwargs) -> list[Any]:
return self._get_resource(entity, **kwargs).get(f"{entity}s", [])
@staticmethod
def format_search_term(field: str, term: str) -> str:
"""Format a search term for the MusicBrainz API.
See https://lucene.apache.org/core/4_3_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html
"""
if not (term := term.lower().strip()):
return ""
term = LUCENE_SPECIAL_CHAR_PAT.sub(r"\\\1", term)
if field:
term = f"{field}:({term})"
return term
def search(
self,
entity: Entity,
filters: dict[str, str],
**kwargs: Unpack[SearchKwargs],
) -> list[JSONDict]:
"""Search for MusicBrainz entities matching the given filters.
* Query is constructed by combining the provided filters using AND logic
* Each filter key-value pair is formatted as 'key:"value"' unless
- 'key' is empty, in which case only the value is used, '"value"'
- 'value' is empty, in which case the filter is ignored
* Values are lowercased and stripped of whitespace.
"""
query = " ".join(
filter(None, starmap(self.format_search_term, filters.items()))
)
log.debug("Searching for MusicBrainz {}s with: {!r}", entity, query)
kwargs["query"] = query
return self._get_resource(entity, **kwargs)[f"{entity}s"]
def get_release(self, id_: str, **kwargs: Unpack[LookupKwargs]) -> Release:
"""Retrieve a release by its MusicBrainz ID."""
kwargs.setdefault("includes", RELEASE_INCLUDES)
return self._lookup("release", id_, **kwargs)
def get_recording(
self, id_: str, **kwargs: Unpack[LookupKwargs]
) -> Recording:
"""Retrieve a recording by its MusicBrainz ID."""
kwargs.setdefault("includes", RECORDING_INCLUDES)
return self._lookup("recording", id_, **kwargs)
def get_work(self, id_: str, **kwargs: Unpack[LookupKwargs]) -> Work:
"""Retrieve a work by its MusicBrainz ID."""
return self._lookup("work", id_, **kwargs)
@require_one_of("artist", "collection", "release", "work")
def browse_recordings(
self, **kwargs: Unpack[BrowseRecordingsKwargs]
) -> list[Recording]:
"""Browse recordings related to the given entities.
At least one of artist, collection, release, or work must be provided.
"""
return self._browse("recording", **kwargs)
@require_one_of("artist", "collection", "release")
def browse_release_groups(
self, **kwargs: Unpack[BrowseReleaseGroupsKwargs]
) -> list[ReleaseGroup]:
"""Browse release groups related to the given entities.
At least one of artist, collection, or release must be provided.
"""
return self._get_resource("release-group", **kwargs)["release-groups"]
@singledispatchmethod
@classmethod
def _normalize_data(cls, data: Any) -> Any:
"""Normalize MusicBrainz relation structures into easier-to-use shapes.
This default handler is a no-op that returns non-container values
unchanged. Specialized handlers for sequences and mappings perform the
actual transformations described below.
"""
return data
@_normalize_data.register(list)
@classmethod
def _(cls, data: list[Any]) -> list[Any]:
"""Apply normalization to each element of a sequence recursively.
Sequences received from the MusicBrainz API may contain nested mappings
that require transformation. This handler maps the normalization step
over the sequence and preserves order.
"""
return [cls._normalize_data(i) for i in data]
@_normalize_data.register(dict)
@classmethod
def _(cls, data: JSONDict) -> JSONDict:
"""Transform mappings by regrouping relationships and normalizing keys.
When a mapping contains a generic 'relations' list, entries are grouped
by their 'target-type' and placed under keys like
'<target-type>_relations' with the 'target-type' field removed from each
entry. All other mapping keys have hyphens converted to underscores and
their values are normalized recursively to ensure a consistent shape
throughout the payload.
"""
output_data = {}
for k, v in list(data.items()):
if k == "relations":
get_target_type = operator.methodcaller("get", "target-type")
for target_type, group in groupby(
sorted(v, key=get_target_type), get_target_type
):
relations = [
{k: v for k, v in item.items() if k != "target-type"}
for item in group
]
output_data[f"{target_type}_relations"] = (
cls._normalize_data(relations)
)
else:
output_data[k.replace("-", "_")] = cls._normalize_data(v)
return output_data
class MusicBrainzAPIMixin:
"""Mixin that provides a cached MusicBrainzAPI helper instance."""
@cached_property
def mb_api(self) -> MusicBrainzAPI:
return MusicBrainzAPI()