mirror of
https://github.com/c0de-archive/spotipy.git
synced 2025-01-04 22:32:40 +00:00
Clean up of examples
This commit is contained in:
parent
6cf9f67c94
commit
34f476d17f
1
.gitignore
vendored
1
.gitignore
vendored
@ -54,3 +54,4 @@ docs/_build/
|
|||||||
|
|
||||||
|
|
||||||
.*
|
.*
|
||||||
|
archive
|
||||||
|
@ -22,7 +22,7 @@ token = util.prompt_for_user_token(username, scope)
|
|||||||
if token:
|
if token:
|
||||||
sp = spotipy.Spotify(auth=token)
|
sp = spotipy.Spotify(auth=token)
|
||||||
sp.trace = False
|
sp.trace = False
|
||||||
results = sp.current_user_saved_tracks_add(ids=tids)
|
results = sp.current_user_saved_tracks_add(tracks=tids)
|
||||||
pprint.pprint(results)
|
pprint.pprint(results)
|
||||||
else:
|
else:
|
||||||
print "Can't get token for", username
|
print "Can't get token for", username
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
import sys
|
|
||||||
import spotipy
|
|
||||||
import pprint
|
|
||||||
|
|
||||||
sp = None
|
|
||||||
max_artists = 100
|
|
||||||
artist_queue = []
|
|
||||||
queued = set()
|
|
||||||
|
|
||||||
'''
|
|
||||||
This example generates a collaboration network suitable for plotting with graphviz
|
|
||||||
Typical usage:
|
|
||||||
|
|
||||||
% python collaborations.py 32 deadmau5 > deadmau5.gv
|
|
||||||
% graphviz deadmau5.gv
|
|
||||||
'''
|
|
||||||
|
|
||||||
def add_artists_from_albums(name, spid):
|
|
||||||
offset = 0
|
|
||||||
limit = 50
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
response = sp.artist_albums(spid, limit=limit, offset=offset)
|
|
||||||
except spotipy.SpotifyException:
|
|
||||||
# a known issue with the API occasionally yields an error when retrieving albums
|
|
||||||
sys.stderr.write('trouble getting albums for %s' % (name,))
|
|
||||||
return
|
|
||||||
for album in response['albums']:
|
|
||||||
for artist in album['artists']:
|
|
||||||
add_artist(name, album['name'], artist)
|
|
||||||
offset += limit
|
|
||||||
|
|
||||||
if not response['paging']['next']:
|
|
||||||
break
|
|
||||||
|
|
||||||
def fn(name):
|
|
||||||
return name.replace('"', '').encode('utf-8')
|
|
||||||
|
|
||||||
def add_artist(name, album_name, artist):
|
|
||||||
if not artist['spotify_uri'] in queued:
|
|
||||||
if name:
|
|
||||||
print(' "%s" -> "%s" [label="%s"];' % (fn(name), fn(artist['name']), fn(album_name)))
|
|
||||||
queued.add(artist['spotify_uri'])
|
|
||||||
artist_queue.append( ( artist['name'], artist['spotify_uri']) )
|
|
||||||
|
|
||||||
def process_artists():
|
|
||||||
done = set()
|
|
||||||
while len(artist_queue) > 0:
|
|
||||||
name, spid = artist_queue.pop(0)
|
|
||||||
if spid not in done:
|
|
||||||
done.add(spid)
|
|
||||||
if len(queued) > max_artists:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
add_artists_from_albums(name, spid)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
if len(sys.argv) < 3:
|
|
||||||
print('Usage: %s max_artists artist name' % (sys.argv[0]))
|
|
||||||
print('Example: %s 100 Rihanna' % (sys.argv[0]))
|
|
||||||
else:
|
|
||||||
max_artists = int(sys.argv[1])
|
|
||||||
artist = ' '.join(sys.argv[2:])
|
|
||||||
sp = spotipy.Spotify()
|
|
||||||
|
|
||||||
results = sp.search(artist, type='artist')
|
|
||||||
artists = results['artists']
|
|
||||||
if len(artists) > 0:
|
|
||||||
add_artist(None, None, artists[0])
|
|
||||||
print('digraph G {')
|
|
||||||
process_artists()
|
|
||||||
print('}')
|
|
||||||
|
|
@ -19,7 +19,7 @@ token = util.prompt_for_user_token(username, scope)
|
|||||||
if token:
|
if token:
|
||||||
sp = spotipy.Spotify(auth=token)
|
sp = spotipy.Spotify(auth=token)
|
||||||
sp.trace = False
|
sp.trace = False
|
||||||
results = sp.current_user_saved_tracks_contains(ids=tids)
|
results = sp.current_user_saved_tracks_contains(tracks=tids)
|
||||||
pprint.pprint(results)
|
pprint.pprint(results)
|
||||||
else:
|
else:
|
||||||
print "Can't get token for", username
|
print "Can't get token for", username
|
||||||
|
@ -21,7 +21,7 @@ token = util.prompt_for_user_token(username, scope)
|
|||||||
if token:
|
if token:
|
||||||
sp = spotipy.Spotify(auth=token)
|
sp = spotipy.Spotify(auth=token)
|
||||||
sp.trace = False
|
sp.trace = False
|
||||||
results = sp.current_user_saved_tracks_delete(ids=tids)
|
results = sp.current_user_saved_tracks_delete(tracks=tids)
|
||||||
pprint.pprint(results)
|
pprint.pprint(results)
|
||||||
else:
|
else:
|
||||||
print "Can't get token for", username
|
print "Can't get token for", username
|
||||||
|
@ -13,5 +13,4 @@ while results['next']:
|
|||||||
|
|
||||||
for album in albums:
|
for album in albums:
|
||||||
print(album['name'])
|
print(album['name'])
|
||||||
print album
|
|
||||||
|
|
||||||
|
@ -7,6 +7,6 @@ sp = spotipy.Spotify()
|
|||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
artist_name = ' '.join(sys.argv[1:])
|
artist_name = ' '.join(sys.argv[1:])
|
||||||
tracks = sp.search(q=artist_name, limit=20)
|
results = sp.search(q=artist_name, limit=20)
|
||||||
for i, t in enumerate(tracks['tracks']):
|
for i, t in enumerate(results['tracks']['items']):
|
||||||
print(' ', i, t['name'])
|
print(' ', i, t['name'])
|
||||||
|
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='SpotipyWebApi',
|
name='SpotipyWebApi',
|
||||||
version='1.320',
|
version='2.0.0',
|
||||||
description='simple client for the Spotify Web API',
|
description='simple client for the Spotify Web API',
|
||||||
author="@plamere",
|
author="@plamere",
|
||||||
author_email="paul@echonest.com",
|
author_email="paul@echonest.com",
|
||||||
|
@ -1 +1,2 @@
|
|||||||
|
VERSION='2.0.0'
|
||||||
from client import Spotify, SpotifyException
|
from client import Spotify, SpotifyException
|
||||||
|
@ -400,6 +400,17 @@ class Spotify(object):
|
|||||||
tlist = [self._get_id('track', t) for t in tracks]
|
tlist = [self._get_id('track', t) for t in tracks]
|
||||||
return self._delete('me/tracks/?ids=' + ','.join(tlist))
|
return self._delete('me/tracks/?ids=' + ','.join(tlist))
|
||||||
|
|
||||||
|
def current_user_saved_tracks_contains(self, tracks=[]):
|
||||||
|
''' Check if one or more tracks is already saved in
|
||||||
|
the current Spotify user’s “Your Music” library.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- tracks - a list of track URIs, URLs or IDs
|
||||||
|
'''
|
||||||
|
tlist = [self._get_id('track', t) for t in tracks]
|
||||||
|
return self._get('me/tracks/contains?ids=' + ','.join(tlist))
|
||||||
|
|
||||||
|
|
||||||
def current_user_saved_tracks_add(self, tracks=[]):
|
def current_user_saved_tracks_add(self, tracks=[]):
|
||||||
''' Add one or more tracks to the current user's
|
''' Add one or more tracks to the current user's
|
||||||
"Your Music" library.
|
"Your Music" library.
|
||||||
|
Loading…
Reference in New Issue
Block a user