Beispiel #1
0
def search(config, session, web_client,
           query=None, uris=None, exact=False, types=_SEARCH_TYPES):
    # TODO Respect `uris` argument
    # TODO Support `exact` search

    if query is None:
        logger.debug('Ignored search without query')
        return models.SearchResult(uri='spotify:search')

    if 'uri' in query:
        return _search_by_uri(config, session, query)

    sp_query = translator.sp_search_query(query)
    if not sp_query:
        logger.debug('Ignored search with empty query')
        return models.SearchResult(uri='spotify:search')

    uri = 'spotify:search:%s' % urllib.quote(sp_query.encode('utf-8'))
    logger.info('Searching Spotify for: %s', sp_query)

    if session.connection.state is not spotify.ConnectionState.LOGGED_IN:
        logger.info('Spotify search aborted: Spotify is offline')
        return models.SearchResult(uri=uri)

    search_count = max(
        config['search_album_count'],
        config['search_artist_count'],
        config['search_track_count'])

    if search_count > 50:
        logger.warn(
            'Spotify currently allows maximum 50 search results of each type. '
            'Please set the config values spotify/search_album_count, '
            'spotify/search_artist_count and spotify/search_track_count '
            'to at most 50.')
        search_count = 50

    result = web_client.get(_API_BASE_URI, params={
        'q': sp_query,
        'limit': search_count,
        'type': ','.join(types)})

    albums = [
        translator.web_to_album(web_album) for web_album in
        result['albums']['items'][:config['search_album_count']]
    ] if 'albums' in result else []

    artists = [
        translator.web_to_artist(web_artist) for web_artist in
        result['artists']['items'][:config['search_artist_count']]
    ] if 'artists' in result else []

    tracks = [
        translator.web_to_track(web_track) for web_track in
        result['tracks']['items'][:config['search_track_count']]
    ] if 'tracks' in result else []

    return models.SearchResult(
        uri=uri, albums=albums, artists=artists, tracks=tracks)
Beispiel #2
0
    def test_calls_web_to_artist_ref(self, web_artist_mock):
        ref_mock = mock.Mock(spec=models.Ref.artist)
        ref_mock.uri = str(sentinel.uri)
        ref_mock.name = str(sentinel.name)

        with patch.object(
            translator, "web_to_artist_ref", return_value=ref_mock
        ) as ref_func_mock:
            artist = translator.web_to_artist(web_artist_mock)
            ref_func_mock.assert_called_once_with(web_artist_mock)

        assert artist.uri == str(sentinel.uri)
        assert artist.name == str(sentinel.name)
Beispiel #3
0
def search(config,
           session,
           web_client,
           query=None,
           uris=None,
           exact=False,
           types=_SEARCH_TYPES):
    # TODO Respect `uris` argument
    # TODO Support `exact` search

    if query is None:
        logger.debug('Ignored search without query')
        return models.SearchResult(uri='spotify:search')

    if 'uri' in query:
        return _search_by_uri(config, session, query, web_client)

    sp_query = translator.sp_search_query(query)
    if not sp_query:
        logger.debug('Ignored search with empty query')
        return models.SearchResult(uri='spotify:search')

    uri = 'spotify:search:%s' % urllib.quote(sp_query.encode('utf-8'))
    logger.info('Searching Spotify for: %s', sp_query)

    if session.connection.state is not spotify.ConnectionState.LOGGED_IN:
        logger.info('Spotify search aborted: Spotify is offline')
        return models.SearchResult(uri=uri)

    search_count = max(config['search_album_count'],
                       config['search_artist_count'],
                       config['search_track_count'])

    if search_count > 50:
        logger.warn(
            'Spotify currently allows maximum 50 search results of each type. '
            'Please set the config values spotify/search_album_count, '
            'spotify/search_artist_count and spotify/search_track_count '
            'to at most 50.')
        search_count = 50

    result = web_client.get('search',
                            params={
                                'q': sp_query,
                                'limit': search_count,
                                'market': session.user_country,
                                'type': ','.join(types)
                            })

    albums = [
        translator.web_to_album(web_album) for web_album in result['albums']
        ['items'][:config['search_album_count']]
    ] if 'albums' in result else []

    artists = [
        translator.web_to_artist(web_artist) for web_artist in
        result['artists']['items'][:config['search_artist_count']]
    ] if 'artists' in result else []

    tracks = [
        translator.web_to_track(web_track) for web_track in result['tracks']
        ['items'][:config['search_track_count']]
    ] if 'tracks' in result else []

    return models.SearchResult(uri=uri,
                               albums=albums,
                               artists=artists,
                               tracks=tracks)
    def test_successful_translation(self, web_artist_mock):
        artist = translator.web_to_artist(web_artist_mock)

        assert artist.uri == 'spotify:artist:abba'
        assert artist.name == 'ABBA'
Beispiel #5
0
    def test_successful_translation(self, web_artist_mock):
        artist = translator.web_to_artist(web_artist_mock)

        assert artist.uri == 'spotify:artist:abba'
        assert artist.name == 'ABBA'
Beispiel #6
0
def search(
    config,
    session,
    web_client,
    query=None,
    uris=None,
    exact=False,
    types=_SEARCH_TYPES,
):
    # TODO Respect `uris` argument
    # TODO Support `exact` search

    if query is None:
        logger.debug("Ignored search without query")
        return models.SearchResult(uri="spotify:search")

    if "uri" in query:
        return _search_by_uri(config, session, web_client, query)

    sp_query = translator.sp_search_query(query)
    if not sp_query:
        logger.debug("Ignored search with empty query")
        return models.SearchResult(uri="spotify:search")

    uri = f"spotify:search:{urllib.parse.quote(sp_query)}"
    logger.info(f"Searching Spotify for: {sp_query}")

    if session.connection.state is not spotify.ConnectionState.LOGGED_IN:
        logger.info("Spotify search aborted: Spotify is offline")
        return models.SearchResult(uri=uri)

    search_count = max(
        config["search_album_count"],
        config["search_artist_count"],
        config["search_track_count"],
    )

    if search_count > 50:
        logger.warn(
            "Spotify currently allows maximum 50 search results of each type. "
            "Please set the config values spotify/search_album_count, "
            "spotify/search_artist_count and spotify/search_track_count "
            "to at most 50.")
        search_count = 50

    result = web_client.get(
        "search",
        params={
            "q": sp_query,
            "limit": search_count,
            "market": "from_token",
            "type": ",".join(types),
        },
    )

    albums = ([
        translator.web_to_album(web_album) for web_album in result["albums"]
        ["items"][:config["search_album_count"]]
    ] if "albums" in result else [])

    artists = ([
        translator.web_to_artist(web_artist) for web_artist in
        result["artists"]["items"][:config["search_artist_count"]]
    ] if "artists" in result else [])

    tracks = ([
        translator.web_to_track(web_track) for web_track in result["tracks"]
        ["items"][:config["search_track_count"]]
    ] if "tracks" in result else [])

    return models.SearchResult(uri=uri,
                               albums=albums,
                               artists=artists,
                               tracks=tracks)