Skip to content

Commit

Permalink
Fix local files error'ing (#196)
Browse files Browse the repository at this point in the history
Co-authored-by: Sathyajith Bhat <sathya@sathyasays.com>
  • Loading branch information
Tony and SathyaBhat committed Oct 25, 2021
1 parent 8346f98 commit 5a26ba0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ def fetch_tracks(sp, item_type, url):
cover = item['track']['album']['images'][0]['url']
else:
cover = None

if len(sp.artist(artist_id=item['track']['artists'][0]['uri'])['genres']) > 0:
genre = sp.artist(artist_id=item['track']['artists'][0]['uri'])['genres'][0]

artists = track_info.get('artists')
main_artist_id = artists[0].get('uri', None) if len(artists) > 0 else None
genres = sp.artist(artist_id=main_artist_id).get('genres', []) if main_artist_id else []
if len(genres) > 0:
genre = genres[0]
else:
genre = ""
songs_list.append({"name": track_name, "artist": track_artist, "album": track_album, "year": track_year,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_spotify_fetch_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,19 @@ def test_spotify_album_fetch_more():
'playlist_num': 16,
'spotify_id': '4G4Sf18XkFvNTV5vAxiQyd'}] == songs
assert (len(songs)) == 16

def test_spotify_playlist_fetch_local_file():
sp = spotify_auth()
url = "https://open.spotify.com/playlist/1TWZ36xJ8qkvSeAQQUvU5b?si=ad56b6bb085b4ab9"
item_type = "playlist"
songs = fetch_tracks(sp, item_type, url)
assert [{'album': "Yoshi's Island",
'artist': 'Koji Kondo',
'cover': None,
'genre': '',
'name': 'Flower Garden',
'num': 0,
'num_tracks': None,
'year': '',
'playlist_num': 1,
'spotify_id': None}] == songs

0 comments on commit 5a26ba0

Please sign in to comment.