def test_spotify_error_on_release(self, dummy_auth): api = object.__new__(SpotifyApi) SpotifyApi.__init__(api, dummy_auth) mock_response = Mock() mock_response.json.return_value = { 'error': { 'message' : 'Not available' } } with patch('requests.get', return_value=mock_response): with pytest.raises(SpotifyErrorMessage): result = api.get_new_release()
def test_get_new_release(self, dummy_auth, news_release_from_spotify): api = object.__new__(SpotifyApi) SpotifyApi.__init__(api, dummy_auth) mock_response = Mock() mock_response.json.return_value = { 'albums': { 'items': news_release_from_spotify } } with patch('requests.get', return_value=mock_response): result = api.get_new_release() assert result == news_release_from_spotify
def test_get_artist(self, dummy_auth, artist_from_spotify): api = object.__new__(SpotifyApi) SpotifyApi.__init__(api, dummy_auth) mock_response = Mock() mock_response.json.return_value = artist_from_spotify with patch('requests.get', return_value=mock_response): result = api.get_artist('id') assert result == artist_from_spotify
def test_get_artist_with_token_changes(self, artist_from_spotify): mock_auth = Mock() mock_auth.token.return_value = 'something' mock_auth._handle_token_expired.return_value = mock_auth api = object.__new__(SpotifyApi) SpotifyApi.__init__(api, mock_auth) mock_response = Mock() mock_response.json.side_effect = [ { 'error': { 'message' : 'The access token expired' } }, artist_from_spotify ] with patch('requests.get', return_value=mock_response): result = api.get_artist('id') assert result == artist_from_spotify
def update_artists(): auth = SpotifyAuth.objects.all().first() if auth is None: print('No Auth saved') return spotify = SpotifyApi(auth=auth) try: # Fetch all new releases from spotify and iterate for release in spotify.get_new_release(): # A release can have several artists colaborating, so iterate on artist per release for artist in release['artists']: # If an artist with current release exist we can stop fetching if Artist.objects.filter( name=artist['name'], last_release_name=release['name']).exists(): raise Reached # Check if this artist exists already in base in_base_artist = Artist.objects.filter( name=artist['name']).first() # If yes : update the last release values if in_base_artist is not None: in_base_artist.last_release_name = release['name'] in_base_artist.last_release_date = release['release_date'] in_base_artist.save() # If not : create it else: artist = spotify.get_artist(artist['id']) # Get all genres of the artist genres_artist = [] for genre in artist['genres']: in_base_genre, c = Genre.objects.get_or_create( name=genre) genres_artist.append(in_base_genre) a = Artist(name=artist['name'], last_release_name=release['name'], last_release_date=release['release_date'], spotify_link=artist['external_urls']['spotify']) a.save() a.genres.set(genres_artist) a.save() except Reached: print("reached" ) # Stop everything if we reached an already ingested release except ErrorWhileGettingToken: print('Something went wrong with spotify') except SpotifyErrorMessage: print('Something went wrong whith spotify')