Skip to content

Instantly share code, notes, and snippets.

@yiufung
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yiufung/57d1744b9d4544fd2beb to your computer and use it in GitHub Desktop.
Save yiufung/57d1744b9d4544fd2beb to your computer and use it in GitHub Desktop.
Get songs longer than a specified value by an artist. Require Spotipy: https://github.com/plamere/spotipy
import spotipy, sys
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 get_album_tracks(album):
tracks = []
results = sp.album_tracks(album['id'])
tracks.extend(results['items'])
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
return tracks
def show_artist_albums(artist):
albums = []
results = sp.artist_albums(artist['id'])
albums.extend(results['items'])
while results['next']:
results = sp.next(results)
albums.extend(results['items'])
print('Total albums:', len(albums))
unique = set() # skip duplicate albums
tracks = []
for album in albums:
name = album['name']
if not name in unique:
# print(name)
unique.add(name)
tracks.extend(get_album_tracks(album))
return tracks
def show_artist(artist):
print('====', artist['name'], '====')
print('Popularity: ', artist['popularity'])
if len(artist['genres']) > 0:
print('Genres: ', ','.join(artist['genres']))
if __name__ == '__main__':
sp = spotipy.Spotify()
sp.trace = False
name = "Yo La Tengo"
artist = get_artist(name)
tracks = show_artist_albums(artist)
seconds = 360 # 6 minute
print "Printing all tracks longer than %d:%02d" % (seconds/60, seconds%60)
for track in tracks:
if int(track['duration_ms']) > seconds * 1000:
import spotipy, sys
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 get_album_tracks(album):
tracks = []
results = sp.album_tracks(album['id'])
tracks.extend(results['items'])
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
return tracks
def show_artist_albums(artist):
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'])
print('Total albums:', len(albums))
unique = set() # skip duplicate albums
tracks = []
for album in albums:
name = album['name']
if not name in unique:
# print(name)
unique.add(name)
tracks.extend(get_album_tracks(album))
return tracks
def show_artist(artist):
print('====', artist['name'], '====')
print('Popularity: ', artist['popularity'])
if len(artist['genres']) > 0:
print('Genres: ', ','.join(artist['genres']))
if __name__ == '__main__':
sp = spotipy.Spotify()
sp.trace = False
name = "Yo La Tengo"
artist = get_artist(name)
tracks = show_artist_albums(artist)
seconds = 360 # 6 minute
print "Printing all tracks longer than %d:%02d" % (seconds/60, seconds%60)
for track in tracks:
if int(track['duration_ms']) > seconds * 1000:
print "%-*s%*s" % (45, track['name'], 5, "%d:%02d" % (track['duration_ms'] / 1000 / 60, track['duration_ms'] / 1000 % 60))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment