Ejemplo n.º 1
0
def test_add_spotify_external_url_already_existing(mocker):
    mocker.patch("requests.get", new=mock_get_request_on_spotify_api)
    input_track = Track(
        title="logical song",
        album="Breakfeast in America",
        artist="supertramp",
        external_urls={"spotify": "A random link already there"},
    )

    output_track = input_track.copy(deep=True)
    output_track.external_urls = simple_queries_responses[
        "logical song supertramp"]["external_urls"]

    assert add_spotify_external_url(input_track) == output_track
Ejemplo n.º 2
0
def test_add_spotify_external_url(mocker):
    mocker.patch("requests.get", new=mock_get_request_on_spotify_api)
    input_track_dict = {
        "title": "logical song",
        "album": "Breakfeast in America",
        "artist": "supertramp",
    }
    input_track = Track(**input_track_dict)

    output_track = Track(**input_track_dict,
                         external_urls=simple_queries_responses[
                             "logical song supertramp"]["external_urls"])

    assert add_spotify_external_url(input_track) == output_track
Ejemplo n.º 3
0
def test_get_spotify_track(mocker):
    mocker.patch("requests.get", new=mock_get_request_on_spotify_api)

    output_track = Track(**simple_queries_responses["logical song supertramp"])

    # Exact match title + supertramp
    input_track = Track(title="logical song",
                        album="Breakfeast in America",
                        artist="supertramp")
    assert get_spotify_track(input_track) == output_track

    # Match when limiting to 2 words
    input_track = Track(
        title="logical song remastered",
        album="Breakfeast in America",
        artist="supertramp",
    )
    assert get_spotify_track(input_track) == output_track
Ejemplo n.º 4
0
def test_get_spotify_track_unknown(mocker):
    mocker.patch("requests.get", new=mock_get_request_on_spotify_api)
    input_track = Track(title="This",
                        album="song",
                        artist="does",
                        musical_kind="not",
                        label="exist")
    with pytest.raises(SpotifyTrackNotFound):
        get_spotify_track(input_track)
Ejemplo n.º 5
0
def test_add_spotify_external_url_unknow(mocker):
    mocker.patch("requests.get", new=mock_get_request_on_spotify_api)
    input_track = Track(title="This",
                        album="song",
                        artist="does",
                        musical_kind="not",
                        label="exist")

    assert add_spotify_external_url(input_track) == input_track
Ejemplo n.º 6
0
def search_on_spotify(query: str) -> Track:
    logger.info(f"search for '{query}' on Spotify API")
    service_address = f"http://{SPOTIFY_API_HOST}:{SPOTIFY_API_PORT}/search"
    payload = {"q": query, "simple": True}  # Get a flat simple response
    r = requests.get(service_address, params=payload)
    if r.status_code == requests.codes.not_found:
        logger.info(f"no track found on Spotify with query '{query}'")
        raise SpotifyTrackNotFound("no track found on Spotify with query '{query}'")
    r.raise_for_status()
    return Track(**r.json())
Ejemplo n.º 7
0
def convert_to_track(track_dict: Dict[str, Any]) -> Track:
    doc = track_dict["track"]
    artist = ""
    try:
        artist = doc["mainArtists"][0]
    except IndexError:
        pass
    doc["artist"] = artist
    doc["year"] = doc["productionDate"]
    doc["album"] = album = doc["albumTitle"]

    return Track(**doc)
Ejemplo n.º 8
0
def add_spotify_external_url(input_track: Track) -> Track:
    external_urls = input_track.external_urls
    try:
        spotifyTrack = get_spotify_track(input_track)
        if "spotify" in spotifyTrack.external_urls:
            logger.info("Adding a spotify url to track")
            external_urls["spotify"] = spotifyTrack.external_urls["spotify"]
    except SpotifyTrackNotFound:
        # already logged previously
        pass

    output_track = input_track.copy(deep=True)
    output_track.external_urls = external_urls
    return output_track
Ejemplo n.º 9
0
def get_current_song() -> Track:
    r = requests.get(url=RADIO_MEUH_API_URL)
    song = r.json()[0]
    logger.debug(song)

    song["title"] = song["titre"]
    # song["album"] = song["album"]
    # song["artist"] = song["artist"]

    song["external_urls"] = {}
    if song["url"] != "":
        song["external_urls"]["spotify"] = song["url"]

    song["cover_url"] = song["imgSrc"]

    return Track(**song)
Ejemplo n.º 10
0
def get_now_unofficial() -> Track:
    r = requests.get(url=UNOFFICIAL_API_URL + UNOFFICIAL_API_OPERATION_NOW)
    song = r.json()["data"]["now"]["song"]
    logger.debug(song)

    song["artist"] = song["interpreters"][0] if len(
        song["interpreters"]) > 0 else ""

    song["cover_url"] = song["cover"]

    song["external_urls"] = {}
    for key, value in song["external_links"].items():
        if not (key.startswith("__") or value is None):
            song["external_urls"][key] = value["link"]

    # Special case uncountered once : album is none
    song["album"] = "" if song["album"] == None else song["album"]

    return Track(**song)
Ejemplo n.º 11
0
def test_get_live():
    response = client.get("/live")
    assert response.status_code in (200, 219)
    if response.status_code == 200:
        assert Track(**response.json())
Ejemplo n.º 12
0
async def test_get_now_unofficial_with_cover():
    """
    Test the assumption that when the unofficial API works, there is always a cover
    """
    assert Track(**get_now_unofficial().dict()).external_urls is not None
Ejemplo n.º 13
0
async def test_get_now_unofficial():
    assert Track(**get_now_unofficial().dict())
Ejemplo n.º 14
0
def test_search_on_spotify(mocker):
    mocker.patch("requests.get", new=mock_get_request_on_spotify_api)
    for query in simple_queries_responses.keys():
        assert search_on_spotify(query) == Track(
            **simple_queries_responses[query])
Ejemplo n.º 15
0
async def test_get_current_song():
    assert Track(**get_current_song().dict())
Ejemplo n.º 16
0
def test_execute_live_query():
    try:
        response = execute_live_query("FIP")
    except LiveUnavailableException as e:
        return
    assert Track(**response.dict())
Ejemplo n.º 17
0
def test_get_live_meuh():
    response = client.get("/meuh")
    assert response.status_code == 200
    assert Track(**response.json())