Documentation updates

This commit is contained in:
Paul Lamere
2014-08-22 09:28:19 -04:00
parent 080786654a
commit 58e5206076
7 changed files with 439 additions and 46 deletions

41
examples/artist_albums.py Normal file
View File

@@ -0,0 +1,41 @@
import sys
import spotipy
''' shows the albums and tracks for a given artist.
'''
def get_artist(name):
results = sp.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
def show_artist_albums(id):
albums = []
results = sp.artist_albums(artist['id'], album_type='album')
albums.extend(results['items'])
while results['next']:
results = sp.next(results)
albums.extend(results['items'])
seen = set() # to avoid dups
albums.sort(key=lambda album:album['name'].lower())
for album in albums:
name = album['name']
if name not in seen:
print(' ' + name)
seen.add(name)
if __name__ == '__main__':
sp = spotipy.Spotify()
if len(sys.argv) < 2:
print('Usage: {0} artist name'.format(sys.argv[0]))
else:
name = ' '.join(sys.argv[1:])
artist = get_artist(name)
if artist:
show_artist_albums(artist)
else:
print("Can't find that artist")

17
examples/simple1.py Normal file
View File

@@ -0,0 +1,17 @@
import spotipy
birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
spotify = spotipy.Spotify()
results = spotify.artist_albums(birdy_uri, album_type='album')
albums = results['items']
while results['next']:
results = spotify.next(results)
albums.extend(results['items'])
for album in albums:
print(album['name'])
print album

15
examples/simple2.py Normal file
View File

@@ -0,0 +1,15 @@
import spotipy
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
spotify = spotipy.Spotify()
results = spotify.artist_top_tracks(lz_uri)
for track in results['tracks'][:10]:
print 'track : ' + track['name']
print 'audio : ' + track['preview_url']
print 'cover art: ' + track['album']['images'][0]['url']
print

16
examples/simple3.py Normal file
View File

@@ -0,0 +1,16 @@
import spotipy
import sys
spotify = spotipy.Spotify()
if len(sys.argv) > 1:
name = ' '.join(sys.argv[1:])
else:
name = 'Radiohead'
results = spotify.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
artist = items[0]
print artist['name'], artist['images'][0]['url']