Example #1
0
def spotify():
    release_group_id = request.args.get('release_group_id')
    if not release_group_id:
        return redirect(url_for('frontend.index'))

    release_group = musicbrainz.get_release_group_by_id(release_group_id)
    if not release_group:
        flash(gettext("Only existing release groups can be mapped to Spotify!"), 'error')
        return redirect(url_for('search.index'))

    page = int(request.args.get('page', default=1))
    if page < 1:
        return redirect(url_for('.spotify'))
    limit = 16
    offset = (page - 1) * limit

    # Removing punctuation from the string
    punctuation_map = dict((ord(char), None) for char in string.punctuation)
    query = unicode(release_group['title']).translate(punctuation_map)
    # Searching...
    response = spotify_api.search(query, 'album', limit, offset).get('albums')

    albums_ids = [x['id'] for x in response['items']]
    full_response = spotify_api.get_multiple_albums(albums_ids)

    return render_template(
            'mapping/spotify.html', release_group=release_group,
            search_results=[full_response[id] for id in albums_ids if id in full_response],
            page=page, limit=limit, count=response.get('total'))
Example #2
0
def spotify():
    release_group_id = request.args.get('release_group_id')
    if not release_group_id:
        return redirect(url_for('frontend.index'))

    release_group = musicbrainz.get_release_group_by_id(release_group_id)
    if not release_group:
        flash.error(
            gettext("Only existing release groups can be mapped to Spotify!"))
        return redirect(url_for('search.index'))

    page = int(request.args.get('page', default=1))
    if page < 1:
        return redirect(url_for('.spotify'))
    limit = 16
    offset = (page - 1) * limit

    # Removing punctuation from the string
    punctuation_map = dict((ord(char), None) for char in string.punctuation)
    query = release_group['title'].translate(punctuation_map)
    # Searching...
    response = spotify_api.search(query, 'album', limit, offset).get('albums')

    albums_ids = [x['id'] for x in response['items']]
    full_response = spotify_api.get_multiple_albums(albums_ids)

    return render_template('mapping/spotify.html',
                           release_group=release_group,
                           search_results=[
                               full_response[id] for id in albums_ids
                               if id in full_response
                           ],
                           page=page,
                           limit=limit,
                           count=response.get('total'))
    def test_get_multiple_albums(self):
        # if no spotify ids are sent
        self.assertDictEqual(spotify.get_multiple_albums([]), {})

        # all ids are in cache
        cache.get_many = MagicMock(return_value=self.all_albums)
        self.assertDictEqual(spotify.get_multiple_albums(self.all_spotify_ids), self.all_albums)

        # some ids are not in cache
        cache.get_many = MagicMock(return_value=self.some_albums)
        # _get sends an unexpected response
        spotify._get = MagicMock(return_value={})
        with self.assertRaises(spotify.SpotifyUnexpectedResponseException):
            spotify.get_multiple_albums(self.all_spotify_ids)
        # _get sends expected response
        spotify._get = MagicMock(return_value=self.some_albums_response)
        self.assertDictEqual(spotify.get_multiple_albums(self.all_spotify_ids), self.all_albums)
    def test_get_multiple_albums(self):
        # if no spotify ids are sent
        self.assertDictEqual(spotify.get_multiple_albums([]), {})

        # all ids are in cache
        cache.get_many = MagicMock(return_value=self.all_albums)
        self.assertDictEqual(spotify.get_multiple_albums(self.all_spotify_ids),
                             self.all_albums)

        # some ids are not in cache
        cache.get_many = MagicMock(return_value=self.some_albums)
        # _get sends an unexpected response
        spotify._get = MagicMock(return_value={})
        with self.assertRaises(spotify.SpotifyUnexpectedResponseException):
            spotify.get_multiple_albums(self.all_spotify_ids)
        # _get sends expected response
        spotify._get = MagicMock(return_value=self.some_albums_response)
        self.assertDictEqual(spotify.get_multiple_albums(self.all_spotify_ids),
                             self.all_albums)
Example #5
0
    def test_get_multiple_albums(self, cache_get_many, spotify_get):
        # if no spotify ids are sent
        self.assertDictEqual(spotify.get_multiple_albums([]), {})

        # all ids are in cache
        cache_get_many.return_value = self.all_albums
        self.assertDictEqual(spotify.get_multiple_albums(self.all_spotify_ids),
                             self.all_albums)

        # some ids are not in cache
        cache_get_many.reset_mock()
        cache_get_many.return_value = self.some_albums
        # _get sends an unexpected response
        spotify_get.return_value = {}
        with self.assertRaises(spotify.SpotifyUnexpectedResponseException):
            spotify.get_multiple_albums(self.all_spotify_ids)
        # _get sends expected response
        spotify_get.reset_mock()
        spotify_get.return_value = self.some_albums_response
        self.assertDictEqual(spotify.get_multiple_albums(self.all_spotify_ids),
                             self.all_albums)
Example #6
0
def spotify_add():
    release_group_id = request.args.get('release_group_id')
    if not release_group_id:
        return redirect(url_for('frontend.index'))
    try:
        release_group = mb_release_group.get_release_group_by_id(
            release_group_id)
    except mb_exceptions.NoDataFoundException:
        flash.error(
            gettext("Only existing release groups can be mapped to Spotify!"))
        return redirect(url_for('search.index'))

    page = int(request.args.get('page', default=1))
    if page < 1:
        return redirect(url_for('.spotify_add'))
    limit = 16
    offset = (page - 1) * limit

    # Removing punctuation from the string
    punctuation_map = dict((ord(char), None) for char in string.punctuation)
    query = release_group['title'].translate(punctuation_map)
    # Searching...
    try:
        response = spotify_api.search(query,
                                      item_types='album',
                                      limit=limit,
                                      offset=offset).get('albums')
    except ExternalServiceException as e:
        current_app.logger.error("Error while searching Spotify API: %s",
                                 str(e),
                                 exc_info=True)
        raise ServiceUnavailable(e)

    albums_ids = [x['id'] for x in response['items']]
    try:
        full_response = spotify_api.get_multiple_albums(albums_ids)
    except ExternalServiceException as e:
        current_app.logger.error("Error while getting albums from Spotify: %s",
                                 str(e),
                                 exc_info=True)
        raise ServiceUnavailable(e)

    search_results = [
        full_response[id] for id in albums_ids if id in full_response
    ]

    return render_template('mapping/spotify.html',
                           release_group=release_group,
                           search_results=search_results,
                           page=page,
                           limit=limit,
                           count=response.get('total'))
Example #7
0
    def test_get_multiple_albums(self):
        spotify_ids = ['0Y7qkJVZ06tS2GUCDptzyW']
        if cache.get_many(spotify_ids, 'spotify_albums'):
            cache.delete_many(spotify_ids, 'spotify_albums')

        spotify._get = lambda query: FakeSpotifyResponse.fromSpotifyIds(
            spotify_ids)
        albums = spotify.get_multiple_albums(spotify_ids)
        self.assertDictEqual(albums[spotify_ids[0]], {
            'id': spotify_ids[0],
            'data': spotify._BASE_URL + spotify_ids[0],
        })

        albums = cache.get_many(spotify_ids, 'spotify_albums')
        self.assertListEqual(list(albums.keys()), spotify_ids)

        # test if cached result is returned properly
        albums = spotify.get_multiple_albums(spotify_ids)
        self.assertDictEqual(albums[spotify_ids[0]], {
            'id': spotify_ids[0],
            'data': spotify._BASE_URL + spotify_ids[0],
        })
Example #8
0
def spotify_list(release_group_id):
    """This view lists all Spotify albums mapped to a specified release group."""
    spotify_mappings = mbspotify.mappings(str(release_group_id))

    # Converting Spotify URIs to IDs
    spotify_ids = []
    for mapping in spotify_mappings:
        spotify_ids.append(mapping[14:])

    if len(spotify_ids) > 0:
        spotify_albums = spotify_api.get_multiple_albums(spotify_ids)
    else:
        spotify_albums = []
    release_group = musicbrainz.get_release_group_by_id(release_group_id)
    if not release_group:
        raise NotFound("Can't find release group with a specified ID.")
    return render_template('mapping/list.html', spotify_albums=spotify_albums,
                           release_group=release_group)
Example #9
0
def spotify_list(release_group_id):
    """This view lists all Spotify albums mapped to a specified release group."""
    spotify_mappings = mbspotify.mappings(str(release_group_id))

    # Converting Spotify URIs to IDs
    spotify_ids = []
    for mapping in spotify_mappings:
        spotify_ids.append(mapping[14:])

    if len(spotify_ids) > 0:
        spotify_albums = spotify_api.get_multiple_albums(spotify_ids)
    else:
        spotify_albums = []
    release_group = musicbrainz.get_release_group_by_id(release_group_id)
    if not release_group:
        raise NotFound("Can't find release group with a specified ID.")
    return render_template('mapping/list.html',
                           spotify_albums=spotify_albums,
                           release_group=release_group)
Example #10
0
def spotify_list(release_group_id):
    """This view lists all Spotify albums mapped to a specified release group."""
    spotify_mappings = mbspotify.mappings(str(release_group_id))
    # Converting Spotify URIs to IDs
    spotify_ids = []
    for mapping in spotify_mappings:
        spotify_ids.append(mapping[14:])

    if spotify_ids:
        try:
            spotify_albums = spotify_api.get_multiple_albums(spotify_ids)
        except ExternalServiceException as e:
            raise ServiceUnavailable(e)
    else:
        spotify_albums = {}
    try:
        release_group = mb_release_group.get_release_group_by_id(str(release_group_id))
    except mb_exceptions.NoDataFoundException:
        raise NotFound("Can't find release group with a specified ID.")
    return render_template('mapping/list.html', spotify_albums=spotify_albums,
                           release_group=release_group)
Example #11
0
def spotify_add():
    release_group_id = request.args.get('release_group_id')
    if not release_group_id:
        return redirect(url_for('frontend.index'))
    try:
        release_group = mb_release_group.get_release_group_by_id(release_group_id)
    except mb_exceptions.NoDataFoundException:
        flash.error(gettext("Only existing release groups can be mapped to Spotify!"))
        return redirect(url_for('search.index'))

    page = int(request.args.get('page', default=1))
    if page < 1:
        return redirect(url_for('.spotify_add'))
    limit = 16
    offset = (page - 1) * limit

    # Removing punctuation from the string
    punctuation_map = dict((ord(char), None) for char in string.punctuation)
    query = release_group['title'].translate(punctuation_map)
    # Searching...
    try:
        response = spotify_api.search(query, item_types='album', limit=limit, offset=offset).get('albums')
    except ExternalServiceException as e:
        current_app.logger.error("Error while searching Spotify API: %s", str(e), exc_info=True)
        raise ServiceUnavailable(e)

    albums_ids = [x['id'] for x in response['items']]
    try:
        full_response = spotify_api.get_multiple_albums(albums_ids)
    except ExternalServiceException as e:
        current_app.logger.error("Error while getting albums from Spotify: %s", str(e), exc_info=True)
        raise ServiceUnavailable(e)

    search_results = [full_response[id] for id in albums_ids if id in full_response]

    return render_template('mapping/spotify.html', release_group=release_group,
                           search_results=search_results, page=page, limit=limit,
                           count=response.get('total'))
Example #12
0
def spotify_list(release_group_id):
    """This view lists all Spotify albums mapped to a specified release group."""
    spotify_mappings = mbspotify.mappings(str(release_group_id))
    # Converting Spotify URIs to IDs
    spotify_ids = []
    for mapping in spotify_mappings:
        spotify_ids.append(mapping[14:])

    if spotify_ids:
        try:
            spotify_albums = spotify_api.get_multiple_albums(spotify_ids)
        except ExternalServiceException as e:
            raise ServiceUnavailable(e)
    else:
        spotify_albums = {}
    try:
        release_group = mb_release_group.get_release_group_by_id(
            str(release_group_id))
    except mb_exceptions.NoDataFoundException:
        raise NotFound("Can't find release group with a specified ID.")
    return render_template('mapping/list.html',
                           spotify_albums=spotify_albums,
                           release_group=release_group)