Skip to content

Commit

Permalink
Implemented the retrieval of labels through brainzutils
Browse files Browse the repository at this point in the history
  • Loading branch information
spellew committed Aug 8, 2019
1 parent 680413d commit 2cd202d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion critiquebrainz/frontend/external/musicbrainz_db/entities.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from brainzutils.musicbrainz_db.artist import fetch_multiple_artists
from brainzutils.musicbrainz_db.label import fetch_multiple_labels
from brainzutils.musicbrainz_db.place import fetch_multiple_places
from brainzutils.musicbrainz_db.event import fetch_multiple_events
from brainzutils.musicbrainz_db.release_group import fetch_multiple_release_groups
from critiquebrainz.frontend.external.musicbrainz_db.release_group import get_release_group_by_id
from critiquebrainz.frontend.external.musicbrainz_db.place import get_place_by_id
from critiquebrainz.frontend.external.musicbrainz_db.event import get_event_by_id
from critiquebrainz.frontend.external.musicbrainz_db.label import get_label_by_id
from critiquebrainz.frontend.external.musicbrainz_db.artist import get_artist_by_id


def get_multiple_entities(entities):
"""Fetch multiple entities using their MBIDs.
Expand All @@ -26,6 +27,7 @@ def get_multiple_entities(entities):
entities_info = {}
release_group_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'release_group', entities)]
artist_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'artist', entities)]
label_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'label', entities)]
place_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'place', entities)]
event_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'event', entities)]
entities_info.update(fetch_multiple_release_groups(
Expand All @@ -35,6 +37,9 @@ def get_multiple_entities(entities):
entities_info.update(fetch_multiple_artists(
artist_mbids,
))
entities_info.update(fetch_multiple_labels(
label_mbids,
))
entities_info.update(fetch_multiple_places(
place_mbids,
))
Expand All @@ -50,6 +55,8 @@ def get_entity_by_id(id, type='release_group'):
entity = get_release_group_by_id(str(id))
elif type == 'artist':
entity = get_artist_by_id(str(id))
elif type == 'label':
entity = get_label_by_id(str(id))
elif type == 'place':
entity = get_place_by_id(str(id))
elif type == 'event':
Expand Down
22 changes: 22 additions & 0 deletions critiquebrainz/frontend/external/musicbrainz_db/label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from brainzutils import cache
from brainzutils.musicbrainz_db.label import fetch_multiple_labels
from critiquebrainz.frontend.external.musicbrainz_db import DEFAULT_CACHE_EXPIRATION


def get_label_by_id(mbid):
"""Get label with MusicBrainz ID.
Args:
mbid (uuid): MBID(gid) of the label.
Returns:
Dictionary containing the label information
"""
key = cache.gen_key(mbid)
label = cache.get(key)
if not label:
label = fetch_multiple_labels(
[mbid],
includes=['artist-rels', 'url-rels'],
).get(mbid)
cache.set(key=key, val=label, time=DEFAULT_CACHE_EXPIRATION)
return label

0 comments on commit 2cd202d

Please sign in to comment.