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
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
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
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)
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
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())
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)
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
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)
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)
def test_get_live(): response = client.get("/live") assert response.status_code in (200, 219) if response.status_code == 200: assert Track(**response.json())
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
async def test_get_now_unofficial(): assert Track(**get_now_unofficial().dict())
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])
async def test_get_current_song(): assert Track(**get_current_song().dict())
def test_execute_live_query(): try: response = execute_live_query("FIP") except LiveUnavailableException as e: return assert Track(**response.dict())
def test_get_live_meuh(): response = client.get("/meuh") assert response.status_code == 200 assert Track(**response.json())