Last active
August 29, 2015 14:14
-
-
Save kingosticks/6c31f111fe116a76a85b to your computer and use it in GitHub Desktop.
pyspotify vs spotipy - artist albums
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function, unicode_literals | |
import time | |
import spotify | |
import threading | |
import spotipy | |
sp = spotipy.Spotify() | |
VARIOUS_ARTISTS_URIS = [ | |
'spotify:artist:0LyfQWJT6nXafLPZqxe9Of', | |
] | |
logged_in_event = threading.Event() | |
def connection_state_listener(session): | |
if session.connection.state is spotify.ConnectionState.LOGGED_IN: | |
logged_in_event.set() | |
config = spotify.Config() | |
config.cache_location = None | |
session = spotify.Session(config) | |
session.on( | |
spotify.SessionEvent.CONNECTION_STATE_UPDATED, | |
connection_state_listener) | |
username = raw_input('Username: ') | |
password = raw_input('Password: ') | |
session.login(username, password) | |
while not logged_in_event.wait(0.1): | |
session.process_events() | |
artistname = 'Avicii' #raw_input('Artist: ') | |
search = session.search('artist:"{0}"'.format(artistname)) | |
artist = search.load().artists[0] | |
print('Gathering albums for artist "%s"' % artist.link) | |
tracks = [] | |
albums_full = [] | |
albums = [] | |
num_requests = 1 | |
start = time.time() | |
results = sp.artist_albums(str(artist.link), | |
album_type='album,single', | |
country='GB', | |
limit=50, | |
offset=0) | |
albums = results['items'] | |
while results['next']: | |
results = spotify.next(results) | |
albums.extend(results['items']) | |
num_requests += 1 | |
middle = time.time() | |
ids = [] | |
for album in albums: | |
ids.append(album['id']) | |
if len(ids) == 20: | |
albums_full.extend(sp.albums(ids)['albums']) # API limits this to batches of 20 | |
num_requests += 1 | |
ids = [] | |
if len(ids) > 0: | |
albums_full.extend(sp.albums(ids)['albums']) | |
num_requests += 1 | |
for album in albums_full: | |
tracks.extend(album['tracks']['items']) | |
end = time.time() | |
print('Found %d albums and %d tracks in %.3fs (%.3fs) using %d requests' % | |
(len(albums_full), len(tracks), end - start, end - middle, num_requests)) | |
for album in sorted(albums_full, key=lambda a: a['name']): | |
print("\t%s (%s) - %s (%s)" % (album['name'], | |
album['release_date'], | |
album['uri'], | |
','.join([a['id'] for a in album['artists']]) )) | |
print("Found %d tracks" % len(tracks)) | |
###### | |
albums = [] | |
albums_full = [] | |
tracks = [] | |
start = time.time() | |
artistbrowse = artist.browse(type=spotify.ArtistBrowserType.NO_TRACKS) | |
artistbrowse.load() | |
for album in artistbrowse.albums: | |
if album.is_available: | |
#if album.artist.link.uri != artist.link.uri: | |
#continue | |
if album.type in [spotify.AlbumType.COMPILATION, spotify.AlbumType.UNKNOWN]: | |
continue | |
if album.artist.link.uri in VARIOUS_ARTISTS_URIS: | |
continue | |
albums.append(album.browse()) | |
middle = time.time() | |
for album in albums: | |
album.load() | |
albums_full.append(album.album) | |
tracks.extend(album.tracks) | |
end = time.time() | |
print('Found %d albums and %d tracks in %.3fs (%.3fs)' % | |
(len(albums_full), len(tracks), end - start, end - middle)) | |
for album in sorted(albums_full, key=lambda a: a.name): | |
print("\t%s (%s) - %s (%s)" % (album.name, | |
album.year, | |
album.link, | |
album.artist.link.uri)) |
Author
kingosticks
commented
Jan 27, 2015
Uncommenting line 91 gives:
Found 29 albums and 113 tracks in 0.901s (0.603s)
Addicted To You (Remixes) (2014) - spotify:album:6Mf5BaiqtQaLT1MaL6hAXA (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Bom (2010) - spotify:album:7aR6fF9YKw16KjRB8kqGKc (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Hey Brother / Wake Me Up / You Make Me (Remixes) (2013) - spotify:album:1ZuXqOXxbpMiJwmfZMtGUc (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
I Could Be The One [Avicii vs Nicky Romero] (Noonie Bao Acoustic Mix) (2013) - spotify:album:3s5esxv8ThJSUlQ1J6ezpE (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
I Could Be the One (Avicii vs. Nicky Romero) (2012) - spotify:album:4Xn3jf19ktN3c6pDEGvSIo (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
I Could Be the One (Avicii vs. Nicky Romero) [Remixes] (2013) - spotify:album:1QgsH2GJ3S9yDSeIbpD7nC (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Jailbait (2011) - spotify:album:2CiCqULAKUeMWQcFdA53w3 (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Levels (2011) - spotify:album:4mkvtXQd6rD7zuAHhexEvb (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Levels (Remixes) (2011) - spotify:album:42z0CzDpHpFYXLmKIk6NuC (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
My Feelings For You (2010) - spotify:album:7jcz4cTKVZOPuZ6qpxaQGZ (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Silhouettes (2012) - spotify:album:6VG7t4lyDUUfrg6s9Ei6iB (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Silhouettes (Remixes) (2012) - spotify:album:7qpD4yvq4XlxCso6C4fXWh (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
So Excited (2009) - spotify:album:5EJhgtt9MFjFBbnvP0dUOC (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Sound Of Now (2008) - spotify:album:2J6UoVABt9KXVTr02uQCew (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Speed (Burn & Lotus Team F1 Mix) (2013) - spotify:album:0x9EFaSNBVVETzDAiTE9QW (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Street Dancer (2011) - spotify:album:5UDyLKPA81YKTMVyWiYpjW (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Street Dancer (2011) - spotify:album:5QcAKG0qHKdAkK5n0afUYm (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
The Days / Nights (2014) - spotify:album:6bV4Ee8NU7YubrdPec3ab5 (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
The Nights (2015) - spotify:album:6NUwvJNh9sUYpauXI61KOL (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
The Nights (2014) - spotify:album:5Xhp3wYT5izs1kIRPJQqCl (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
The Nights (Mike Mago Remix) (2015) - spotify:album:7uIGRS1a1X4w4URdHPUdUx (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
True (2013) - spotify:album:02h9kO2oLKnLtycgbElKsw (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
True: Avicii By Avicii (2014) - spotify:album:2zcx8cMjC0zHU94PrIZZd0 (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
Wake Me Up (Remixes) (2013) - spotify:album:025ROb3r19luTGtBRBCJhq (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
We Write The Story (2013) - spotify:album:4m6BXAnopdrg80jkhm4B7Z (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
X You (2013) - spotify:album:4UzCCSrGPBRZfjTEfQqO7X (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
X You (2013) - spotify:album:4TIO4EhBRDHFpLaxpE9Twp (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
X You (Vocal Radio Edit) (2013) - spotify:album:4flxpA1Ykxs5FgEEtr8Ysl (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
You Make Me (Diplo & Ookay Remix) (2014) - spotify:album:7l3zJXSPhF6qCZUU7lBTEF (spotify:artist:1vCWHaC5f2uS3yhpwWbIA6)
The times are all over the place.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment