mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-10-11 01:10:45 +00:00
1 parent
5928981e3b
commit
f54830e272
35 files changed
+324
-248
No files matched your search
@@ -4,6 +4,7 @@ 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']
|
||||
@@ -12,6 +13,7 @@ def get_artist(name):
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def show_artist_albums(artist):
|
||||
albums = []
|
||||
results = sp.artist_albums(artist['id'], album_type='album')
|
||||
@@ -19,7 +21,7 @@ def show_artist_albums(artist):
|
||||
while results['next']:
|
||||
results = sp.next(results)
|
||||
albums.extend(results['items'])
|
||||
seen = set() # to avoid dups
|
||||
seen = set() # to avoid dups
|
||||
albums.sort(key=lambda album: album['name'].lower())
|
||||
for album in albums:
|
||||
name = album['name']
|
||||
@@ -27,6 +29,7 @@ def show_artist_albums(artist):
|
||||
print((' ' + name))
|
||||
seen.add(name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sp = spotipy.Spotify()
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ 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']
|
||||
@@ -12,6 +13,7 @@ def get_artist(name):
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def show_album_tracks(album):
|
||||
tracks = []
|
||||
results = sp.album_tracks(album['id'])
|
||||
@@ -24,6 +26,7 @@ def show_album_tracks(album):
|
||||
print()
|
||||
print(track)
|
||||
|
||||
|
||||
def show_artist_albums(id):
|
||||
albums = []
|
||||
results = sp.artist_albums(artist['id'], album_type='album')
|
||||
@@ -35,17 +38,19 @@ def show_artist_albums(id):
|
||||
unique = set() # skip duplicate albums
|
||||
for album in albums:
|
||||
name = album['name'].lower()
|
||||
if not name in unique:
|
||||
if name not in unique:
|
||||
print(name)
|
||||
unique.add(name)
|
||||
show_album_tracks(album)
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -7,7 +7,8 @@ import spotipy
|
||||
from spotipy.oauth2 import SpotifyClientCredentials
|
||||
client_credentials_manager = SpotifyClientCredentials()
|
||||
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||
sp.trace=False
|
||||
sp.trace = False
|
||||
|
||||
|
||||
def get_artist(name):
|
||||
results = sp.search(q='artist:' + name, type='artist')
|
||||
@@ -17,11 +18,12 @@ def get_artist(name):
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def show_recommendations_for_artist(artist):
|
||||
albums = []
|
||||
results = sp.recommendations(seed_artists=[artist['id']])
|
||||
for track in results['tracks']:
|
||||
print track['name'], '-', track['artists'][0]['name']
|
||||
print(track['name'], '-', track['artists'][0]['name'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
@@ -32,4 +34,4 @@ if __name__ == '__main__':
|
||||
if artist:
|
||||
show_recommendations_for_artist(artist)
|
||||
else:
|
||||
print "Can't find that artist", name
|
||||
print("Can't find that artist", name)
|
||||
@@ -11,7 +11,7 @@ import sys
|
||||
|
||||
client_credentials_manager = SpotifyClientCredentials()
|
||||
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||
sp.trace=False
|
||||
sp.trace = False
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
artist_name = ' '.join(sys.argv[1:])
|
||||
|
||||
@@ -12,7 +12,7 @@ import sys
|
||||
|
||||
client_credentials_manager = SpotifyClientCredentials()
|
||||
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||
sp.trace=True
|
||||
sp.trace = True
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
tids = sys.argv[1:]
|
||||
|
||||
@@ -25,7 +25,7 @@ if len(sys.argv) > 3:
|
||||
|
||||
else:
|
||||
print("Usage: %s username playlist_id name [public collaborative "
|
||||
"description]" % (sys.argv[0]))
|
||||
"description]" % (sys.argv[0]))
|
||||
sys.exit()
|
||||
|
||||
scope = 'playlist-modify-public playlist-modify-private'
|
||||
@@ -37,6 +37,6 @@ if token:
|
||||
results = sp.user_playlist_change_details(
|
||||
username, playlist_id, name=name, public=public,
|
||||
collaborative=collaborative, description=description)
|
||||
print results
|
||||
print(results)
|
||||
else:
|
||||
print "Can't get token for", username
|
||||
print("Can't get token for"), username
|
||||
@@ -12,7 +12,9 @@ if len(sys.argv) > 2:
|
||||
playlist_name = sys.argv[2]
|
||||
playlist_description = sys.argv[3]
|
||||
else:
|
||||
print("Usage: %s username playlist-name playlist-description" % (sys.argv[0],))
|
||||
print(
|
||||
"Usage: %s username playlist-name playlist-description" %
|
||||
(sys.argv[0],))
|
||||
sys.exit()
|
||||
|
||||
scope = "playlist-modify-public"
|
||||
|
||||
+1
-2
@@ -7,7 +7,7 @@ import spotipy
|
||||
|
||||
client_credentials_manager = SpotifyClientCredentials()
|
||||
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||
sp.trace=True
|
||||
sp.trace = True
|
||||
try:
|
||||
print('bad call 0')
|
||||
bad_artist_call = sp.artist('spotify:artist:12341234')
|
||||
@@ -17,4 +17,3 @@ except spotipy.client.SpotifyException:
|
||||
print('bad call 1')
|
||||
bad_artist_call = sp.artist('spotify:artist:12341234')
|
||||
print('bad artist', bad_artist_call)
|
||||
|
||||
@@ -19,6 +19,6 @@ if token:
|
||||
sp.trace = False
|
||||
results = sp.current_user_playlists(limit=50)
|
||||
for i, item in enumerate(results['items']):
|
||||
print("%d %s" %(i, item['name']))
|
||||
print("%d %s" % (i, item['name']))
|
||||
else:
|
||||
print("Can't get token for", username)
|
||||
@@ -1,11 +1,9 @@
|
||||
# Shows the top artists for a user
|
||||
|
||||
import pprint
|
||||
import sys
|
||||
|
||||
import spotipy
|
||||
import spotipy.util as util
|
||||
import simplejson as json
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
username = sys.argv[1]
|
||||
@@ -21,10 +19,10 @@ if token:
|
||||
sp.trace = False
|
||||
ranges = ['short_term', 'medium_term', 'long_term']
|
||||
for range in ranges:
|
||||
print ("range:", range)
|
||||
print("range:", range)
|
||||
results = sp.current_user_top_artists(time_range=range, limit=50)
|
||||
for i, item in enumerate(results['items']):
|
||||
print (i, item['name'])
|
||||
print ()
|
||||
print(i, item['name'])
|
||||
print()
|
||||
else:
|
||||
print("Can't get token for", username)
|
||||
@@ -1,11 +1,9 @@
|
||||
# Shows the top tracks for a user
|
||||
|
||||
import pprint
|
||||
import sys
|
||||
|
||||
import spotipy
|
||||
import spotipy.util as util
|
||||
import simplejson as json
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
username = sys.argv[1]
|
||||
@@ -21,11 +19,11 @@ if token:
|
||||
sp.trace = False
|
||||
ranges = ['short_term', 'medium_term', 'long_term']
|
||||
for range in ranges:
|
||||
print ("range:", range)
|
||||
print("range:", range)
|
||||
results = sp.current_user_top_tracks(time_range=range, limit=50)
|
||||
for i, item in enumerate(results['items']):
|
||||
print (i, item['name'], '//', item['artists'][0]['name'])
|
||||
print ()
|
||||
print(i, item['name'], '//', item['artists'][0]['name'])
|
||||
print()
|
||||
|
||||
else:
|
||||
print("Can't get token for", username)
|
||||
@@ -10,4 +10,4 @@ username = uri.split(':')[2]
|
||||
playlist_id = uri.split(':')[4]
|
||||
|
||||
results = sp.user_playlist(username, playlist_id)
|
||||
print (json.dumps(results, indent=4))
|
||||
print(json.dumps(results, indent=4))
|
||||
@@ -15,7 +15,9 @@ if len(sys.argv) > 3:
|
||||
tid, pos = t_pos.split(',')
|
||||
track_ids.append({"uri": tid, "positions": [int(pos)]})
|
||||
else:
|
||||
print("Usage: %s username playlist_id track_id,pos track_id,pos ..." % (sys.argv[0],))
|
||||
print(
|
||||
"Usage: %s username playlist_id track_id,pos track_id,pos ..." %
|
||||
(sys.argv[0],))
|
||||
sys.exit()
|
||||
|
||||
scope = 'playlist-modify-public'
|
||||
@@ -24,7 +26,8 @@ token = util.prompt_for_user_token(username, scope)
|
||||
if token:
|
||||
sp = spotipy.Spotify(auth=token)
|
||||
sp.trace = False
|
||||
results = sp.user_playlist_remove_specific_occurrences_of_tracks(username, playlist_id, track_ids)
|
||||
results = sp.user_playlist_remove_specific_occurrences_of_tracks(
|
||||
username, playlist_id, track_ids)
|
||||
pprint.pprint(results)
|
||||
else:
|
||||
print("Can't get token for", username)
|
||||
@@ -20,7 +20,8 @@ token = util.prompt_for_user_token(username, scope)
|
||||
if token:
|
||||
sp = spotipy.Spotify(auth=token)
|
||||
sp.trace = False
|
||||
results = sp.user_playlist_remove_all_occurrences_of_tracks(username, playlist_id, track_ids)
|
||||
results = sp.user_playlist_remove_all_occurrences_of_tracks(
|
||||
username, playlist_id, track_ids)
|
||||
pprint.pprint(results)
|
||||
else:
|
||||
print("Can't get token for", username)
|
||||
@@ -22,6 +22,5 @@ try:
|
||||
print('Related artists for', name)
|
||||
for artist in related['artists']:
|
||||
print(' ', artist['name'])
|
||||
except:
|
||||
except BaseException:
|
||||
print("usage show_related.py [artist-name]")
|
||||
|
||||
@@ -19,6 +19,3 @@ if __name__ == '__main__':
|
||||
results = sp.tracks(tids[start: start + max_tracks_per_call])
|
||||
for track in results['tracks']:
|
||||
print(track['name'] + ' - ' + track['artists'][0]['name'])
|
||||
|
||||
|
||||
|
||||
@@ -14,4 +14,3 @@ sp = spotipy.Spotify()
|
||||
sp.trace = True
|
||||
user = sp.user(username)
|
||||
pprint.pprint(user)
|
||||
|
||||
@@ -14,4 +14,3 @@ while results['next']:
|
||||
|
||||
for album in albums:
|
||||
print((album['name']))
|
||||
|
||||
@@ -15,4 +15,3 @@ items = results['artists']['items']
|
||||
if len(items) > 0:
|
||||
artist = items[0]
|
||||
print(artist['name'], artist['images'][0]['url'])
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import spotipy
|
||||
import random
|
||||
import simplejson as json
|
||||
|
||||
'''
|
||||
generates a list of songs where the first word in each subsequent song
|
||||
@@ -14,13 +13,14 @@ skiplist = set(['dm', 'remix'])
|
||||
max_offset = 500
|
||||
seen = set()
|
||||
|
||||
|
||||
def find_songs_that_start_with_word(word):
|
||||
max_titles = 20
|
||||
max_offset = 200
|
||||
offset = 0
|
||||
|
||||
out = []
|
||||
while offset < max_offset and len(out) < max_titles:
|
||||
while offset < max_offset and len(out) < max_titles:
|
||||
results = sp.search(q=word, type='track', limit=50, offset=offset)
|
||||
if len(results['tracks']['items']) == 0:
|
||||
break
|
||||
@@ -37,27 +37,29 @@ def find_songs_that_start_with_word(word):
|
||||
if '/' in name:
|
||||
continue
|
||||
words = name.split()
|
||||
if len(words) > 1 and words[0] == word and words[-1] not in skiplist:
|
||||
#print " ", name, len(out)
|
||||
if len(words) > 1 and words[0] == word \
|
||||
and words[-1] not in skiplist:
|
||||
# print " ", name, len(out)
|
||||
out.append(item)
|
||||
offset += 50
|
||||
#print "found", len(out), "matches"
|
||||
# print "found", len(out), "matches"
|
||||
return out
|
||||
|
||||
|
||||
def make_chain(word):
|
||||
which = 1
|
||||
while True:
|
||||
songs = find_songs_that_start_with_word(word)
|
||||
if len(songs) > 0:
|
||||
song = random.choice(songs)
|
||||
print which, song['name'] + " by " + song['artists'][0]['name']
|
||||
print(which, song['name'] + " by " + song['artists'][0]['name'])
|
||||
which += 1
|
||||
word = song['name'].lower().split()[-1]
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
title = ' '.join(sys.argv[1:])
|
||||
make_chain(sys.argv[1].lower())
|
||||
|
||||
@@ -4,10 +4,13 @@ import sys
|
||||
import spotipy
|
||||
import spotipy.util as util
|
||||
|
||||
|
||||
def show_tracks(results):
|
||||
for i, item in enumerate(results['items']):
|
||||
track = item['track']
|
||||
print(" %d %32.32s %s" % (i, track['artists'][0]['name'], track['name']))
|
||||
print(
|
||||
" %d %32.32s %s" %
|
||||
(i, track['artists'][0]['name'], track['name']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -28,7 +31,8 @@ if __name__ == '__main__':
|
||||
print()
|
||||
print(playlist['name'])
|
||||
print(' total tracks', playlist['tracks']['total'])
|
||||
results = sp.user_playlist(username, playlist['id'], fields="tracks,next")
|
||||
results = sp.user_playlist(
|
||||
username, playlist['id'], fields="tracks,next")
|
||||
tracks = results['tracks']
|
||||
show_tracks(tracks)
|
||||
while tracks['next']:
|
||||
@@ -36,4 +40,3 @@ if __name__ == '__main__':
|
||||
show_tracks(tracks)
|
||||
else:
|
||||
print("Can't get token for", username)
|
||||
|
||||
@@ -18,7 +18,13 @@ playlists = sp.user_playlists(user)
|
||||
|
||||
while playlists:
|
||||
for i, playlist in enumerate(playlists['items']):
|
||||
print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name']))
|
||||
print(
|
||||
"%4d %s %s" %
|
||||
(i +
|
||||
1 +
|
||||
playlists['offset'],
|
||||
playlist['uri'],
|
||||
playlist['name']))
|
||||
if playlists['next']:
|
||||
playlists = sp.next(playlists)
|
||||
else:
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
|
||||
"""
|
||||
|
||||
import pprint
|
||||
import sys
|
||||
import json
|
||||
import spotipy
|
||||
import spotipy.util as util
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import spotipy
|
||||
import spotipy.util as util
|
||||
|
||||
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
username = sys.argv[1]
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user