コード例 #1
0
def get_releases_using_recording_mbid(recording_mbid):
    """Returns a list of releases that contain the recording with
       the given recording MBID.

       Args:
           recording_mbid (UUID): recording MBID for which releases are to be fetched.

       Returns:
           serial_releases (list): list with dictionary elements of following format:
           {
               'id': <release MBID>,
               'name': <release Title>,
           }
    """

    # First fetch the recording so that redirects don't create any problem
    recording_redirect = recording.get_recording_by_mbid(recording_mbid)
    recording_mbid = recording_redirect['id']
    with mb_session() as db:
        releases = db.query(models.Release).\
                    join(models.Medium).\
                    join(models.Track).\
                    join(models.Recording).\
                    filter(models.Recording.gid == recording_mbid).all()

        serial_releases = [serialize_releases(release) for release in releases]
        if not serial_releases:
            raise mb_exceptions.NoDataFoundException("Couldn't find release for recording with MBID: %s." % str(recording_mbid))

        return serial_releases
コード例 #2
0
def get_mapped_event_types(event_types: list) -> list:
    """ Get event types mapped to their case sensitive name in musicbrainz.
    event_type table in the database.

    Args:
        event_types (list): List of event types.
    Returns:
        List of mapped event types.

    """
    event_types = [event_type.lower() for event_type in event_types]
    mapped_event_types = []
    with mb_session() as db:
        supported_types = [
            event_type.name for event_type in db.query(models.EventType).all()
        ]
        event_type_mapping = {
            supported_type.lower(): supported_type
            for supported_type in supported_types
        }

        for event_type in event_types:
            if event_type in event_type_mapping:
                mapped_event_types.append(event_type_mapping[event_type])
            else:
                raise mb_exceptions.InvalidTypeError(
                    "Bad event_type: {etype} is not supported".format(
                        etype=event_type))

        return mapped_event_types
コード例 #3
0
def get_releases_using_recording_mbid(recording_mbid):
    """Returns a list of releases that contain the recording with
       the given recording MBID.

       Args:
           recording_mbid (UUID): recording MBID for which releases are to be fetched.

       Returns:
           serial_releases (list): list with dictionary elements of following format:
           {
               'id': <release MBID>,
               'name': <release Title>,
           }
    """

    # First fetch the recording so that redirects don't create any problem
    recording_redirect = recording.get_recording_by_mbid(recording_mbid)
    recording_mbid = recording_redirect['id']
    with mb_session() as db:
        releases = db.query(models.Release).\
                    join(models.Medium).\
                    join(models.Track).\
                    join(models.Recording).\
                    filter(models.Recording.gid == recording_mbid).all()

        serial_releases = [serialize_releases(release) for release in releases]
        if not serial_releases:
            raise mb_exceptions.NoDataFoundException("Couldn't find release for recording with MBID: %s." % str(recording_mbid))

        return serial_releases
コード例 #4
0
def fetch_multiple_editors(editor_ids, includes=None):
    """Get info related to multiple editors using their editor IDs.
    Args:
        editor_ids (list): List of IDs of editors.
        includes (list): List of information to be included.
    Returns:
        Dictionary containing info of multiple editors keyed by their editor_id.
    """
    if includes is None:
        includes = []

    includes_data = defaultdict(dict)
    check_includes('editor', includes)
    with mb_session() as db:
        query = db.query(models.Editor)
        editors = get_entities_by_ids(
            query=query,
            entity_type='editor',
            ids=editor_ids,
        )
        editor_ids = [editor.id for editor in editors.values()]
        editors = {
            editor_id: serialize_editor(editors[editor_id], includes_data)
            for editor_id in editor_ids
        }

    return editors
コード例 #5
0
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
コード例 #6
0
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:
        A dictionary containing info of multiple places keyed by their MBID.
        If an MBID doesn't exist in the database, it isn't returned.
        If an MBID is a redirect, the dictionary key will be the MBID given as an argument,
         but the returned object will contain the new MBID in the 'mbid' key.
    """
    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,
            )

        places = {
            str(mbid): serialize_places(place, includes_data[place.id])
            for mbid, place in places.items()
        }
    return places
コード例 #7
0
def fetch_multiple_labels(mbids,
                          includes=None,
                          unknown_entities_for_missing=False):
    """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,
            unknown_entities_for_missing=unknown_entities_for_missing,
        )
        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

    if 'rating' in includes:
        for label in labels.values():
            includes_data[label.id]['rating'] = label.rating

    return {
        str(mbid): serialize_labels(labels[mbid],
                                    includes_data[labels[mbid].id])
        for mbid in mbids
    }
コード例 #8
0
def _fetch_multiple_recordings(mbids, includes=None):
    """ Fetch multiple recordings with MusicBrainz IDs.

    Args:
        mbids (list): list of uuid (MBID(gid)) of the recordings.
        includes (list): List of values to be included.
                        For list of possible values visit https://bitbucket.org/lalinsky/mbdata/wiki/API/v1/includes#!recording
    Returns:
        Dictionary containing the recording information with MBIDs as keys.
            - id: Recording mbid
            - name: Name of the recording
            - length: length of the recording
            - artists:
                - artist information: id, name, credited_name and join_phrase
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('recording', includes)

    with mb_session() as db:
        query = db.query(Recording)

        if 'artist' in includes or 'artists' in includes:
            query = query.options(joinedload("artist_credit", innerjoin=True))

        if 'artists' in includes:
            query = query.\
            options(subqueryload("artist_credit.artists")).\
            options(joinedload("artist_credit.artists.artist", innerjoin=True))

        recordings = get_entities_by_gids(
            query=query,
            entity_type='recording',
            mbids=mbids,
        )

        recording_ids = [recording.id for recording in recordings.values()]

        if 'artist' in includes:
            for recording in recordings.values():
                includes_data[recording.id]['artist'] = recording.artist_credit

        if 'artists' in includes:
            for recording in recordings.values():
                includes_data[
                    recording.id]['artists'] = recording.artist_credit.artists

        serial_recordings = {
            str(mbid): serialize_recording(recordings[mbid],
                                           includes_data[recordings[mbid].id])
            for mbid in mbids
        }

    return serial_recordings
コード例 #9
0
def fetch_multiple_releases(mbids,
                            includes=None,
                            unknown_entities_for_missing=False):
    """Get info related to multiple releases using their MusicBrainz IDs.
    Args:
        mbids (list): List of MBIDs of releases.
        includes (list): List of information to be included.
    Returns:
        Dictionary containing info of multiple releases keyed by their mbid.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('release', includes)
    with mb_session() as db:
        query = db.query(models.Release)
        if 'release-groups' in includes:
            query = query.options(joinedload('release_group'))
        if 'media' in includes:
            # Fetch media with tracks
            query = query.options(joinedload('mediums')).\
                    options(joinedload('mediums.tracks')).\
                    options(joinedload('mediums.format')).\
                    options(joinedload('mediums.tracks.recording'))
        releases = get_entities_by_gids(
            query=query,
            entity_type='release',
            mbids=mbids,
            unknown_entities_for_missing=unknown_entities_for_missing,
        )
        release_ids = [release.id for release in releases.values()]

        if 'release-groups' in includes:
            for release in releases.values():
                includes_data[
                    release.id]['release-groups'] = release.release_group

        if 'media' in includes:
            for release in releases.values():
                includes_data[release.id]['media'] = release.mediums

        if 'url-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='url',
                source_type='release',
                source_entity_ids=release_ids,
                includes_data=includes_data,
            )
        releases = {
            str(mbid): serialize_releases(releases[mbid],
                                          includes_data[releases[mbid].id])
            for mbid in mbids
        }
    return releases
コード例 #10
0
def browse_releases(release_group_id, includes=None):
    """Get all the releases by a certain release group.
    You need to provide the Release Group's MusicBrainz ID.
    """
    if includes is None:
        includes = []
    with mb_session() as db:
        release_ids = db.query(models.Release.gid).\
                      join(models.ReleaseGroup).\
                      filter(models.ReleaseGroup.gid == release_group_id).all()
        release_ids = [release_id[0] for release_id in release_ids]
    releases = fetch_multiple_releases(release_ids, includes=includes)
    return releases
コード例 #11
0
def browse_releases(release_group_id, includes=None):
    """Get all the releases by a certain release group.
    You need to provide the Release Group's MusicBrainz ID.
    """
    if includes is None:
        includes = []
    with mb_session() as db:
        release_ids = db.query(models.Release.gid).\
                      join(models.ReleaseGroup).\
                      filter(models.ReleaseGroup.gid == release_group_id).all()
        release_ids = [release_id[0] for release_id in release_ids]
    releases = fetch_multiple_releases(release_ids, includes=includes)
    return releases
コード例 #12
0
def _fetch_multiple_recordings(mbids, includes=None):
    """ Fetch multiple recordings with MusicBrainz IDs.

    Args:
        mbids (list): list of uuid (MBID(gid)) of the recordings.
        includes (list): List of values to be included.
                        For list of possible values visit https://bitbucket.org/lalinsky/mbdata/wiki/API/v1/includes#!recording
    Returns:
        Dictionary containing the recording information with MBIDs as keys.
            - id: Recording mbid
            - name: Name of the recording
            - length: length of the recording
            - artists:
                - artist information: id, name, credited_name and join_phrase
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('recording', includes)

    with mb_session() as db:
        query = db.query(Recording)
        
        if 'artist' in includes or 'artists' in includes:
            query = query.options(joinedload("artist_credit", innerjoin=True))

        if 'artists' in includes:
            query = query.\
            options(subqueryload("artist_credit.artists")).\
            options(joinedload("artist_credit.artists.artist", innerjoin=True))

        recordings = get_entities_by_gids(
            query=query,
            entity_type='recording',
            mbids=mbids,
        )

        recording_ids = [recording.id for recording in recordings.values()]

        if 'artist' in includes:
            for recording in recordings.values():
                includes_data[recording.id]['artist'] = recording.artist_credit

        if 'artists' in includes:
            for recording in recordings.values():
                includes_data[recording.id]['artists'] = recording.artist_credit.artists

        serial_recordings = {str(mbid): serialize_recording(recordings[mbid], includes_data[recordings[mbid].id]) for mbid in mbids}

    return serial_recordings
コード例 #13
0
def fetch_multiple_works(mbids, includes=None, unknown_entities_for_missing=False):
    """Get info related to multiple works using their MusicBrainz IDs.

    Args:
        mbids (list): List of MBIDs of works.
        includes (list): List of information to be included.

    Returns:
        Dictionary containing info of multiple works keyed by their mbid.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('work', includes)
    with mb_session() as db:
        query = db.query(models.Work).\
            options(joinedload("type"))
        works = get_entities_by_gids(
            query=query,
            entity_type='work',
            mbids=mbids,
            unknown_entities_for_missing=unknown_entities_for_missing,
        )
        work_ids = [work.id for work in works.values()]

        if 'artist-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='artist',
                source_type='work',
                source_entity_ids=work_ids,
                includes_data=includes_data,
            )

        if 'recording-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='recording',
                source_type='work',
                source_entity_ids=work_ids,
                includes_data=includes_data,
            )

        if 'rating' in includes:
            for work in works.values():
                includes_data[work.id]['rating'] = work.rating    

        for work in works.values():
            includes_data[work.id]['type'] = work.type
    return {str(mbid): serialize_works(works[mbid], includes_data[works[mbid].id]) for mbid in mbids}
コード例 #14
0
def get_events_for_place(place_id: UUID,
                         event_types: List[str] = [],
                         include_null_type: bool = True,
                         limit: int = None,
                         offset: int = None) -> tuple:
    """Get all events that occurred at a place.

    Args:
        place_id: MBID of the place.
        event_types: List of types of events to be fetched. The supported event_types are
        'Concert', 'Festival', 'Convention/Expo', 'Launch event', 'Award ceremony', 'Stage performance', and 'Masterclass/Clinic'.
        include_null_type: Whether to include events with no type.
        limit: Max number of events to return.
        offset: Offset that can be used in conjunction with the limit.

    Returns:
        Tuple containing the list of dictionaries of events and the total count of the events.
        The list of dictionaries of events is ordered by event begin year, begin month, begin date
        begin time, and begin name. In case one of these is set to NULL, it will be ordered last.
    """

    place_id = str(place_id)
    event_types = get_mapped_event_types(event_types)

    with mb_session() as db:
        event_query = db.query(models.Event).outerjoin(models.EventType).\
            options(contains_eager(models.Event.type)).\
            join(models.LinkEventPlace, models.Event.id == models.LinkEventPlace.entity0_id).\
            join(models.Place, models.LinkEventPlace.entity1_id == models.Place.id).\
            filter(models.Place.gid == place_id)

        if include_null_type and event_types:
            event_query = event_query.filter(
                or_(models.Event.type == None,
                    models.EventType.name.in_(event_types)))
        elif event_types:
            event_query = event_query.filter(
                models.EventType.name.in_(event_types))

        event_query = event_query.order_by(
            nullslast(models.Event.begin_date_year.desc()),
            nullslast(models.Event.begin_date_month.desc()),
            nullslast(models.Event.begin_date_day.desc()),
            nullslast(models.Event.time.desc()),
            nullslast(models.Event.name.asc()))
        count = event_query.count()
        events = event_query.limit(limit).offset(offset).all()

        return ([serialize_events(event) for event in events], count)
コード例 #15
0
def fetch_multiple_releases(mbids, includes=None):
    """Get info related to multiple releases using their MusicBrainz IDs.
    Args:
        mbids (list): List of MBIDs of releases.
        includes (list): List of information to be included.
    Returns:
        Dictionary containing info of multiple releases keyed by their mbid.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('release', includes)
    with mb_session() as db:
        query = db.query(models.Release)
        if 'release-groups' in includes:
            query = query.options(joinedload('release_group'))
        if 'media' in includes:
            # Fetch media with tracks
            query = query.options(joinedload('mediums')).\
                    options(joinedload('mediums.tracks')).\
                    options(joinedload('mediums.format')).\
                    options(joinedload('mediums.tracks.recording'))
        releases = get_entities_by_gids(
            query=query,
            entity_type='release',
            mbids=mbids,
        )
        release_ids = [release.id for release in releases.values()]

        if 'release-groups' in includes:
            for release in releases.values():
                includes_data[release.id]['release-groups'] = release.release_group

        if 'media' in includes:
            for release in releases.values():
                includes_data[release.id]['media'] = release.mediums

        if 'url-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='url',
                source_type='release',
                source_entity_ids=release_ids,
                includes_data=includes_data,
            )
        releases = {str(mbid): serialize_releases(releases[mbid], includes_data[releases[mbid].id]) for mbid in mbids}
    return releases
コード例 #16
0
ファイル: work.py プロジェクト: metabrainz/brainzutils-python
def fetch_multiple_works(mbids, includes=None):
    """Get info related to multiple works using their MusicBrainz IDs.

    Args:
        mbids (list): List of MBIDs of works.
        includes (list): List of information to be included.

    Returns:
        A dictionary containing info of multiple works keyed by their MBID.
        If an MBID doesn't exist in the database, it isn't returned.
        If an MBID is a redirect, the dictionary key will be the MBID given as an argument,
         but the returned object will contain the new MBID in the 'mbid' key.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('work', includes)
    with mb_session() as db:
        query = db.query(models.Work).options(joinedload("type"))

        works = get_entities_by_gids(
            query=query,
            entity_type='work',
            mbids=mbids,
        )
        work_ids = [work.id for work in works.values()]

        if 'artist-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='artist',
                source_type='work',
                source_entity_ids=work_ids,
                includes_data=includes_data,
            )

        if 'recording-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='recording',
                source_type='work',
                source_entity_ids=work_ids,
                includes_data=includes_data,
            )

        return {str(mbid): serialize_works(work, includes_data[work.id]) for mbid, work in works.items()}
コード例 #17
0
def fetch_multiple_artists(mbids, includes=None):
    """Get info related to multiple artists using their MusicBrainz IDs.
    Args:
        mbids (list): List of MBIDs of artists.
        includes (list): List of information to be included.
    Returns:
        Dictionary containing info of multiple artists keyed by their mbid.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('artist', includes)
    with mb_session() as db:
        query = db.query(models.Artist).\
                options(joinedload("type"))
        artists = get_entities_by_gids(
            query=query,
            entity_type='artist',
            mbids=mbids,
        )
        artist_ids = [artist.id for artist in artists.values()]

        if 'artist-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='artist',
                source_type='artist',
                source_entity_ids=artist_ids,
                includes_data=includes_data,
            )
        if 'url-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='url',
                source_type='artist',
                source_entity_ids=artist_ids,
                includes_data=includes_data,
            )

    for artist in artists.values():
        includes_data[artist.id]['type'] = artist.type
    artists = {str(mbid): serialize_artists(artists[mbid], includes_data[artists[mbid].id]) for mbid in mbids}
    return artists
コード例 #18
0
def get_release_groups_for_artist(artist_id,
                                  release_types=None,
                                  limit=None,
                                  offset=None):
    """Get all release groups linked to an artist.

    Args:
        artist_id (uuid): MBID of the artist.
        release_types (list): List of types of release groups to be fetched.
        limit (int): Max number of release groups to return.
        offset (int): Offset that can be used in conjunction with the limit.

    Returns:
        Tuple containing the list of dictionaries of release groups ordered by release year
        and the total count of the release groups.
    """
    artist_id = str(artist_id)
    includes_data = defaultdict(dict)
    if release_types is None:
        release_types = []
    release_types = [
        release_type.capitalize() for release_type in release_types
    ]
    with mb_session() as db:
        release_groups_query = _get_release_groups_for_artist_query(
            db, artist_id, release_types)
        count = release_groups_query.count()
        release_groups = release_groups_query.order_by(
            case([
                (models.ReleaseGroupMeta.first_release_date_year.is_(None), 1)
            ],
                 else_=0),
            models.ReleaseGroupMeta.first_release_date_year.desc()).limit(
                limit).offset(offset).all()
    for release_group in release_groups:
        includes_data[release_group.id]['meta'] = release_group.meta
    release_groups = ([
        serialize_release_groups(release_group,
                                 includes_data[release_group.id])
        for release_group in release_groups
    ], count)
    return release_groups
コード例 #19
0
def get_release_groups_for_artist(artist_id, release_types=None, limit=None, offset=None):
    """Get all release groups linked to an artist.

    Args:
        artist_id (uuid): MBID of the artist.
        release_types (list): List of types of release groups to be fetched.
        limit (int): Max number of release groups to return.
        offset (int): Offset that can be used in conjunction with the limit.

    Returns:
        Tuple containing the list of dictionaries of release groups ordered by release year
        and the total count of the release groups.
    """
    artist_id = str(artist_id)
    includes_data = defaultdict(dict)
    if release_types is None:
        release_types = []
    release_types = [release_type.lower() for release_type in release_types]
    # map release types to their case sensitive name in musicbrainz.release_group_primary_type table in the database
    release_types_mapping = {
        'album': 'Album',
        'single': 'Single',
        'ep': 'EP',
        'broadcast': 'Broadcast',
        'other': 'Other'
    }
    release_types = [release_types_mapping[release_type] for release_type in release_types]
    with mb_session() as db:
        release_groups_query = _get_release_groups_for_artist_query(db, artist_id, release_types)
        count = release_groups_query.count()
        release_groups = release_groups_query.order_by(
            nullslast(models.ReleaseGroupMeta.first_release_date_year.desc())
        ).limit(limit).offset(offset).all()

        for release_group in release_groups:
            includes_data[release_group.id]['meta'] = release_group.meta
        release_groups = ([serialize_release_groups(release_group, includes_data[release_group.id])
                            for release_group in release_groups], count)
        return release_groups
コード例 #20
0
def fetch_multiple_editors(editor_ids, includes=None):
    """Get info related to multiple editors using their editor IDs.
    Args:
        editor_ids (list): List of IDs of editors.
        includes (list): List of information to be included.
    Returns:
        Dictionary containing info of multiple editors keyed by their editor_id.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('editor', includes)
    with mb_session() as db:
        query = db.query(models.Editor)
        editors = get_entities_by_ids(
            query=query,
            entity_type='editor',
            ids=editor_ids,
        )
        editor_ids = [editor.id for editor in editors.values()]
        editors = {editor_id: serialize_editor(editors[editor_id], includes_data) for editor_id in editor_ids}

    return editors
コード例 #21
0
def get_mapped_release_types(release_types):
    """Get release types mapped to their case sensitive name in musicbrainz.
    release_group_primary_type table.
    
    Args:
        release_types (list): List of release types.
    Returns:
        List of mapped release types.
    """
    
    release_types = [release_type.lower() for release_type in release_types]
    mapped_release_types = []
    with mb_session() as db:
        supported_types = [release_group_type.name for release_group_type in db.query(models.ReleaseGroupPrimaryType)]
        release_type_mapping = {supported_type.lower(): supported_type for supported_type in supported_types}

        for release_type in release_types:
            if release_type not in release_type_mapping:
                raise mb_exceptions.InvalidTypeError("Bad release_types: {rtype} is not supported".format(rtype = release_type))
            else:
                mapped_release_types.append(release_type_mapping[release_type])
        
        return mapped_release_types
コード例 #22
0
def get_release_groups_for_label(label_mbid, release_types=None, limit=None, offset=None):
    """Get all release groups linked to a label.

    Args:
        label_id (uuid): MBID of the label.
        release_types (list): List of types of release groups to be fetched. The supported release_types are 
        'album', 'single', 'ep', 'broadcast', and 'other'.
        limit (int): Max number of release groups to return.
        offset (int): Offset that can be used in conjunction with the limit.

    Returns:
        Tuple containing the list of dictionaries of release groups and the total count of the release groups.
        The list of dictionaries of release groups is ordered by release year, release month,
        release date, and release name. In case one of these is set to NULL, it will be ordered last.
        List also contains release groups with null type if 'Other' is in the list of release types.
    """
    label_mbid = str(label_mbid)
    includes_data = defaultdict(dict)
    if release_types is None:
        release_types = []
    release_types = get_mapped_release_types(release_types)
    include_null_type = True if "Other" in release_types else False
    with mb_session() as db:
        release_groups_query = _get_release_groups_for_label_query(db, label_mbid, release_types, include_null_type)
        count = release_groups_query.count()
        release_groups = release_groups_query.order_by(
            nullslast(models.ReleaseGroupMeta.first_release_date_year.desc()),
            nullslast(models.ReleaseGroupMeta.first_release_date_month.desc()),
            nullslast(models.ReleaseGroupMeta.first_release_date_day.desc()),
            nullslast(models.ReleaseGroup.name.asc())
        ).limit(limit).offset(offset).all()

        for release_group in release_groups:
            includes_data[release_group.id]['meta'] = release_group.meta
        release_groups = [serialize_release_groups(release_group, includes_data[release_group.id])
                            for release_group in release_groups]
        return release_groups, count
コード例 #23
0
def fetch_multiple_release_groups(mbids, includes=None):
    """Get info related to multiple release groups using their MusicBrainz IDs.
    Args:
        mbids (list): List of MBIDs of releases groups.
        includes (list): List of information to be included.
    Returns:
        Dictionary containing info of multiple release groups keyed by their mbid.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('release_group', includes)
    with mb_session() as db:
        # Join table meta which contains release date for a release group
        query = db.query(models.ReleaseGroup).options(joinedload("meta")).\
                options(joinedload("type"))

        if 'artists' in includes:
            query = query.\
                options(joinedload("artist_credit")).\
                options(joinedload("artist_credit.artists")).\
                options(joinedload("artist_credit.artists.artist"))

        release_groups = get_entities_by_gids(
            query=query,
            entity_type='release_group',
            mbids=mbids,
        )
        release_group_ids = [
            release_group.id for release_group in release_groups.values()
        ]

        if 'artists' in includes:
            for release_group in release_groups.values():
                artist_credit_names = release_group.artist_credit.artists
                includes_data[release_group.
                              id]['artist-credit-names'] = artist_credit_names
                includes_data[release_group.id][
                    'artist-credit-phrase'] = release_group.artist_credit.name

        if 'releases' in includes:
            query = db.query(models.Release).filter(
                getattr(models.Release,
                        "release_group_id").in_(release_group_ids))
            for release in query:
                includes_data[release.release_group_id].setdefault(
                    'releases', []).append(release)

        if 'release-group-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='release_group',
                source_type='release_group',
                source_entity_ids=release_group_ids,
                includes_data=includes_data,
            )

        if 'url-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='url',
                source_type='release_group',
                source_entity_ids=release_group_ids,
                includes_data=includes_data,
            )

        if 'work-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='work',
                source_type='release_group',
                source_entity_ids=release_group_ids,
                includes_data=includes_data,
            )

        if 'tags' in includes:
            release_group_tags = get_tags(
                db=db,
                entity_model=models.ReleaseGroup,
                tag_model=models.ReleaseGroupTag,
                foreign_tag_id=models.ReleaseGroupTag.release_group_id,
                entity_ids=release_group_ids,
            )
            for release_group_id, tags in release_group_tags:
                includes_data[release_group_id]['tags'] = tags

        for release_group in release_groups.values():
            includes_data[release_group.id]['meta'] = release_group.meta
            includes_data[release_group.id]['type'] = release_group.type
        release_groups = {
            str(mbid):
            serialize_release_groups(release_groups[mbid],
                                     includes_data[release_groups[mbid].id])
            for mbid in mbids
        }
        return release_groups
コード例 #24
0
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:
        A dictionary containing info of multiple events keyed by their MBID.
        If an MBID doesn't exist in the database, it isn't returned.
        If an MBID is a redirect, the dictionary key will be the MBID given as an argument,
         but the returned object will contain the new MBID in the 'mbid' key.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('event', includes)
    with mb_session() as db:
        query = db.query(models.Event).options(joinedload('type'))
        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(event, includes_data[event.id])
            for mbid, event in events.items()
        }
コード例 #25
0
def fetch_multiple_releases(mbids, includes=None):
    """Get info related to multiple releases using their MusicBrainz IDs.
    Args:
        mbids (list): List of MBIDs of releases.
        includes (list): List of information to be included.
    Returns:
        A dictionary containing info of multiple releases keyed by their MBID.
        If an MBID doesn't exist in the database, it isn't returned.
        If an MBID is a redirect, the dictionary key will be the MBID given as an argument,
         but the returned object will contain the new MBID in the 'mbid' key.
    """
    if includes is None:
        includes = []
    includes_data = defaultdict(dict)
    check_includes('release', includes)
    with mb_session() as db:
        query = db.query(models.Release)
        if 'release-groups' in includes:
            query = query.options(joinedload('release_group'))
        if 'artists' in includes:
            query = query.\
                options(joinedload("artist_credit")).\
                options(joinedload("artist_credit.artists")).\
                options(joinedload("artist_credit.artists.artist"))
        if 'media' in includes:
            # Fetch media with tracks
            query = query.options(joinedload('mediums')).\
                    options(joinedload('mediums.tracks')).\
                    options(joinedload('mediums.format')).\
                    options(joinedload('mediums.tracks.recording')).\
                    options(joinedload('mediums.tracks.recording.artist_credit')).\
                    options(joinedload('mediums.tracks.recording.artist_credit.artists')).\
                    options(joinedload('mediums.tracks.recording.artist_credit.artists.artist'))
        releases = get_entities_by_gids(
            query=query,
            entity_type='release',
            mbids=mbids,
        )
        release_ids = [release.id for release in releases.values()]

        if 'release-groups' in includes:
            for release in releases.values():
                includes_data[
                    release.id]['release-groups'] = release.release_group

        if 'artists' in includes:
            for release in releases.values():
                artist_credit_names = release.artist_credit.artists
                includes_data[
                    release.id]['artist-credit-names'] = artist_credit_names
                includes_data[release.id][
                    'artist-credit-phrase'] = release.artist_credit.name

        if 'media' in includes:
            for release in releases.values():
                includes_data[release.id]['media'] = release.mediums

        if 'url-rels' in includes:
            get_relationship_info(
                db=db,
                target_type='url',
                source_type='release',
                source_entity_ids=release_ids,
                includes_data=includes_data,
            )

        releases = {
            str(mbid): serialize_releases(release, includes_data[release.id])
            for mbid, release in releases.items()
        }
    return releases
コード例 #26
0
def fetch_multiple_events(mbids,
                          includes=None,
                          unknown_entities_for_missing=False):
    """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,
            unknown_entities_for_missing=unknown_entities_for_missing,
        )
        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,
            )

    if 'rating' in includes:
        for event in events.values():
            includes_data[event.id]['rating'] = event.rating

    return {
        str(mbid): serialize_events(events[mbid],
                                    includes_data[events[mbid].id])
        for mbid in mbids
    }
コード例 #27
0
def session(engine):
    return mb_session()