45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
from lyra_worker.browser import ArtistHit, ReleaseGroupInfo
|
|
|
|
|
|
class MusicBrainzBrowser:
|
|
"""Real MbBrowser via musicbrainzngs (imported lazily; not unit-tested offline)."""
|
|
|
|
def __init__(self, app_name: str = "Lyra", version: str = "0.1", contact: str = "lyra@localhost"):
|
|
self._app, self._version, self._contact = app_name, version, contact
|
|
|
|
def _ua(self):
|
|
import musicbrainzngs
|
|
|
|
musicbrainzngs.set_useragent(self._app, self._version, self._contact)
|
|
return musicbrainzngs
|
|
|
|
def search_artist(self, name: str) -> list[ArtistHit]:
|
|
mb = self._ua()
|
|
res = mb.search_artists(query=name, limit=8)
|
|
return [
|
|
ArtistHit(mbid=a["id"], name=a.get("name", ""), disambiguation=a.get("disambiguation", ""))
|
|
for a in res.get("artist-list", [])
|
|
]
|
|
|
|
def browse_release_groups(self, artist_mbid: str) -> list[ReleaseGroupInfo]:
|
|
mb = self._ua()
|
|
out: list[ReleaseGroupInfo] = []
|
|
offset = 0
|
|
while True:
|
|
res = mb.browse_release_groups(artist=artist_mbid, limit=100, offset=offset)
|
|
groups = res.get("release-group-list", [])
|
|
for g in groups:
|
|
out.append(
|
|
ReleaseGroupInfo(
|
|
rg_mbid=g["id"],
|
|
title=g.get("title", ""),
|
|
primary_type=g.get("primary-type", "") or "",
|
|
secondary_types=tuple(g.get("secondary-type-list", []) or ()),
|
|
first_release_date=g.get("first-release-date", "") or "",
|
|
)
|
|
)
|
|
offset += len(groups)
|
|
if len(groups) < 100 or offset >= int(res.get("release-group-count", offset)):
|
|
break
|
|
return out
|