-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from spellew/additional-entity-support
Implemented retrieval and/or testing of additional entities
- Loading branch information
Showing
22 changed files
with
961 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from collections import defaultdict | ||
from mbdata import models | ||
from brainzutils.musicbrainz_db import mb_session | ||
from brainzutils.musicbrainz_db.utils import get_entities_by_gids | ||
from brainzutils.musicbrainz_db.includes import check_includes | ||
from brainzutils.musicbrainz_db.serialize import serialize_events | ||
from brainzutils.musicbrainz_db.helpers import get_relationship_info | ||
|
||
|
||
def get_event_by_id(mbid, includes=None): | ||
"""Get event with the MusicBrainz ID. | ||
Args: | ||
mbid (uuid): MBID(gid) of the event. | ||
Returns: | ||
Dictionary containing the event information. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
|
||
return fetch_multiple_events( | ||
[mbid], | ||
includes=includes, | ||
).get(mbid) | ||
|
||
|
||
def fetch_multiple_events(mbids, includes=None): | ||
"""Get info related to multiple events using their MusicBrainz IDs. | ||
Args: | ||
mbids (list): List of MBIDs of events. | ||
includes (list): List of information to be included. | ||
Returns: | ||
Dictionary containing info of multiple events keyed by their mbid. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
includes_data = defaultdict(dict) | ||
check_includes('event', includes) | ||
with mb_session() as db: | ||
query = db.query(models.Event) | ||
events = get_entities_by_gids( | ||
query=query, | ||
entity_type='event', | ||
mbids=mbids, | ||
) | ||
event_ids = [event.id for event in events.values()] | ||
|
||
if 'artist-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='artist', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'place-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='place', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'series-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='series', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'url-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='url', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'release-group-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='release_group', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
return {str(mbid): serialize_events(events[mbid], includes_data[events[mbid].id]) for mbid in mbids} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from collections import defaultdict | ||
from mbdata import models | ||
from sqlalchemy.orm import joinedload | ||
from brainzutils.musicbrainz_db import mb_session | ||
from brainzutils.musicbrainz_db.utils import get_entities_by_gids | ||
from brainzutils.musicbrainz_db.includes import check_includes | ||
from brainzutils.musicbrainz_db.serialize import serialize_labels | ||
from brainzutils.musicbrainz_db.helpers import get_relationship_info | ||
|
||
|
||
def get_label_by_id(mbid, includes=None): | ||
"""Get label with the MusicBrainz ID. | ||
Args: | ||
mbid (uuid): MBID(gid) of the label. | ||
Returns: | ||
Dictionary containing the label information. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
|
||
return fetch_multiple_labels( | ||
[mbid], | ||
includes=includes, | ||
).get(mbid) | ||
|
||
|
||
def fetch_multiple_labels(mbids, includes=None): | ||
"""Get info related to multiple labels using their MusicBrainz IDs. | ||
Args: | ||
mbids (list): List of MBIDs of labels. | ||
includes (list): List of information to be included. | ||
Returns: | ||
Dictionary containing info of multiple labels keyed by their mbid. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
includes_data = defaultdict(dict) | ||
check_includes('label', includes) | ||
with mb_session() as db: | ||
query = db.query(models.Label).\ | ||
options(joinedload("type")).\ | ||
options(joinedload("area")) | ||
labels = get_entities_by_gids( | ||
query=query, | ||
entity_type='label', | ||
mbids=mbids, | ||
) | ||
label_ids = [label.id for label in labels.values()] | ||
|
||
if 'artist-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='artist', | ||
source_type='label', | ||
source_entity_ids=label_ids, | ||
includes_data=includes_data, | ||
) | ||
|
||
if 'url-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='url', | ||
source_type='label', | ||
source_entity_ids=label_ids, | ||
includes_data=includes_data, | ||
) | ||
|
||
for label in labels.values(): | ||
includes_data[label.id]['type'] = label.type | ||
includes_data[label.id]['area'] = label.area | ||
|
||
return {str(mbid): serialize_labels(labels[mbid], includes_data[labels[mbid].id]) for mbid in mbids} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from collections import defaultdict | ||
from mbdata import models | ||
from sqlalchemy.orm import joinedload | ||
from brainzutils.musicbrainz_db import mb_session | ||
from brainzutils.musicbrainz_db.includes import check_includes | ||
from brainzutils.musicbrainz_db.serialize import serialize_places | ||
from brainzutils.musicbrainz_db.helpers import get_relationship_info | ||
from brainzutils.musicbrainz_db.utils import get_entities_by_gids | ||
|
||
|
||
def get_place_by_id(mbid, includes=None): | ||
"""Get place with the MusicBrainz ID. | ||
Args: | ||
mbid (uuid): MBID(gid) of the place. | ||
Returns: | ||
Dictionary containing the place information. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
|
||
return fetch_multiple_places( | ||
[mbid], | ||
includes=includes, | ||
).get(mbid) | ||
|
||
|
||
def fetch_multiple_places(mbids, includes=None): | ||
"""Get info related to multiple places using their MusicBrainz IDs. | ||
Args: | ||
mbids (list): List of MBIDs of places. | ||
includes (list): List of information to be included. | ||
Returns: | ||
Dictionary containing info of multiple places keyed by their mbid. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
includes_data = defaultdict(dict) | ||
check_includes('place', includes) | ||
with mb_session() as db: | ||
query = db.query(models.Place).\ | ||
options(joinedload("area")).\ | ||
options(joinedload("type")) | ||
places = get_entities_by_gids( | ||
query=query, | ||
entity_type='place', | ||
mbids=mbids, | ||
) | ||
place_ids = [place.id for place in places.values()] | ||
|
||
if 'artist-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='artist', | ||
source_type='place', | ||
source_entity_ids=place_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'place-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='place', | ||
source_type='place', | ||
source_entity_ids=place_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'url-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='url', | ||
source_type='place', | ||
source_entity_ids=place_ids, | ||
includes_data=includes_data, | ||
) | ||
|
||
for place in places.values(): | ||
includes_data[place.id]['area'] = place.area | ||
includes_data[place.id]['type'] = place.type | ||
places = {str(mbid): serialize_places(places[mbid], includes_data[places[mbid].id]) for mbid in mbids} | ||
return places |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.