Merge branch 'master' into master

This commit is contained in:
Stéphane Bruckert authored and GitHub committed 2020-01-15 19:16:38 +00:00
commit 9b8c90dbe1
61 files changed
+1113 -770

No files matched your search

-1
View File
@@ -1,7 +1,6 @@
# Adds tracks to a playlist
import pprint
import sys
import spotipy
+5 -2
View File
@@ -5,6 +5,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']
@@ -13,6 +14,7 @@ def get_artist(name):
else:
return None
def show_artist_albums(artist):
albums = []
results = sp.artist_albums(artist['id'], album_type='album')
@@ -20,14 +22,15 @@ def show_artist_albums(artist):
while results['next']:
results = sp.next(results)
albums.extend(results['items'])
seen = set() # to avoid dups
albums.sort(key=lambda album:album['name'].lower())
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__':
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
+6 -1
View File
@@ -5,6 +5,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']
@@ -13,6 +14,7 @@ def get_artist(name):
else:
return None
def show_album_tracks(album):
tracks = []
results = sp.album_tracks(album['id'])
@@ -25,6 +27,7 @@ def show_album_tracks(album):
print()
print(track)
def show_artist_albums(id):
albums = []
results = sp.artist_albums(artist['id'], album_type='album')
@@ -36,17 +39,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__':
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
+7 -5
View File
@@ -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']])
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)
+1 -1
View File
@@ -20,4 +20,4 @@ start = time.time()
analysis = sp.audio_analysis(tid)
delta = time.time() - start
print(json.dumps(analysis, indent=4))
print ("analysis retrieved in %.2f seconds" % (delta,))
print("analysis retrieved in %.2f seconds" % (delta,))
+2 -2
View File
@@ -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:])
@@ -33,4 +33,4 @@ for feature in features:
analysis = sp._get(feature['analysis_url'])
print(json.dumps(analysis, indent=4))
print()
print ("features retrieved in %.2f seconds" % (delta,))
print("features retrieved in %.2f seconds" % (delta,))
+2 -2
View File
@@ -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:]
@@ -22,4 +22,4 @@ if len(sys.argv) > 1:
features = sp.audio_features(tids)
delta = time.time() - start
print(json.dumps(features, indent=4))
print ("features retrieved in %.2f seconds" % (delta,))
print("features retrieved in %.2f seconds" % (delta,))
+4 -4
View File
@@ -24,8 +24,8 @@ if len(sys.argv) > 3:
description = sys.argv[6]
else:
print ("Usage: %s username playlist_id name [public collaborative "
"description]" % (sys.argv[0]))
print("Usage: %s username playlist_id name [public collaborative "
"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
+1 -1
View File
@@ -10,7 +10,7 @@ if len(sys.argv) > 2:
username = sys.argv[1]
tids = sys.argv[2:]
else:
print("Usage: %s username track-id ..." % (sys.argv[0],))
print("Usage: %s username track-id ..." % (sys.argv[0],))
sys.exit()
token = util.prompt_for_user_token(username, scope)
+6 -5
View File
@@ -2,8 +2,6 @@
import pprint
import sys
import os
import subprocess
import spotipy
import spotipy.util as util
@@ -14,16 +12,19 @@ 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()
token = util.prompt_for_user_token(username)
scope = "playlist-modify-public"
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
playlists = sp.user_playlist_create(username, playlist_name,
playlist_description)
description=playlist_description)
pprint.pprint(playlists)
else:
print("Can't get token for", username)
+5 -9
View File
@@ -2,22 +2,18 @@
from __future__ import print_function # (at top of module)
from spotipy.oauth2 import SpotifyClientCredentials
import json
import spotipy
import time
import sys
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp.trace=True
sp.trace = True
try:
print ('bad call 0')
print('bad call 0')
bad_artist_call = sp.artist('spotify:artist:12341234')
except spotipy.client.SpotifyException:
print ('bad call 0 exception' )
print('bad call 0 exception')
print ('bad call 1')
print('bad call 1')
bad_artist_call = sp.artist('spotify:artist:12341234')
print ('bad artist', bad_artist_call)
print('bad artist', bad_artist_call)
+2 -4
View File
@@ -1,11 +1,9 @@
# Shows the top artists for a user
# Shows a user's playlists
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,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)
+3 -5
View File
@@ -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)
+5 -7
View File
@@ -1,11 +1,9 @@
# Adds tracks to a playlist
# 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)
+1 -1
View File
@@ -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))
@@ -1,5 +1,4 @@
# Adds tracks to a playlist
# removes tracks from a playlist
import pprint
import sys
@@ -11,12 +10,14 @@ if len(sys.argv) > 3:
username = sys.argv[1]
playlist_id = sys.argv[2]
track_ids_and_positions = sys.argv[3:]
track_ids = [ ]
track_ids = []
for t_pos in sys.argv[3:]:
tid, pos = t_pos.split(',')
track_ids.append( { "uri" : tid, "positions": [ int(pos)] } )
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'
@@ -25,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)
+3 -3
View File
@@ -1,5 +1,4 @@
# Adds tracks to a playlist
# removes tracks to a playlist
import pprint
import sys
@@ -21,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)
-1
View File
@@ -1,4 +1,3 @@
# Replaces all tracks in a playlist
import pprint
+1
View File
@@ -3,6 +3,7 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import sys
import pprint
+1
View File
@@ -2,6 +2,7 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import sys
import pprint
+1 -1
View File
@@ -2,8 +2,8 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import sys
import pprint
if len(sys.argv) > 1:
urn = sys.argv[1]
-1
View File
@@ -2,7 +2,6 @@
import spotipy
import sys
import pprint
import spotipy.util as util
if len(sys.argv) > 1:
+1 -2
View File
@@ -2,7 +2,6 @@
import spotipy
import sys
import pprint
import spotipy.util as util
if len(sys.argv) > 1:
@@ -22,7 +21,7 @@ if token:
while response:
albums = response['albums']
for i, item in enumerate(albums['items']):
print(albums['offset'] + i,item['name'])
print(albums['offset'] + i, item['name'])
if albums['next']:
response = sp.next(albums)
+1 -3
View File
@@ -4,7 +4,6 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import sys
import pprint
if len(sys.argv) > 1:
artist_name = sys.argv[1]
@@ -22,6 +21,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]")
+1
View File
@@ -2,6 +2,7 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import sys
import pprint
-3
View File
@@ -21,6 +21,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'])
-1
View File
@@ -16,4 +16,3 @@ sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp.trace = True
user = sp.user(username)
pprint.pprint(user)
+3 -5
View File
@@ -1,18 +1,16 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
client_credentials_manager = SpotifyClientCredentials()
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
results = spotify.artist_albums(birdy_uri, album_type='album')
results = sp.artist_albums(birdy_uri, album_type='album')
albums = results['items']
while results['next']:
results = spotify.next(results)
results = sp.next(results)
albums.extend(results['items'])
for album in albums:
print((album['name']))
+2 -3
View File
@@ -2,13 +2,12 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
client_credentials_manager = SpotifyClientCredentials()
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
results = spotify.artist_top_tracks(lz_uri)
results = sp.artist_top_tracks(lz_uri)
for track in results['tracks'][:10]:
print('track : ' + track['name'])
+2 -4
View File
@@ -1,18 +1,16 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import sys
client_credentials_manager = SpotifyClientCredentials()
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
if len(sys.argv) > 1:
name = ' '.join(sys.argv[1:])
else:
name = 'Radiohead'
results = spotify.search(q='artist:' + name, type='artist')
results = sp.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
artist = items[0]
print(artist['name'], artist['images'][0]['url'])
+10 -8
View File
@@ -1,7 +1,6 @@
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import random
import simplejson as json
'''
generates a list of songs where the first word in each subsequent song
@@ -18,14 +17,15 @@ 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:
results = sp.search(q=word, type = 'track', limit=50, offset = offset)
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
@@ -41,27 +41,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())
-3
View File
@@ -1,9 +1,6 @@
# shows a user's playlists (need to be authenticated via oauth)
import pprint
import sys
import os
import subprocess
import spotipy
+6 -3
View File
@@ -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)
+8 -2
View File
@@ -1,6 +1,6 @@
# Gets all the public playlists for the given
# user. Uses Client Credentials flow
#
#
import sys
import spotipy
@@ -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:
+28
View File
@@ -0,0 +1,28 @@
"""
Deletes user saved album
"""
import sys
import spotipy
import spotipy.util as util
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print("Usage: %s username" % (sys.argv[0],))
sys.exit()
scope = 'user-library-modify'
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
uris = input("input a list of album URIs, URLs or IDs: ")
uris = list(map(str, uris.split()))
deleted = sp.current_user_saved_albums_delete(uris)
print("Deletion successful.")
else:
print("Can't get token for", username)
+1 -2
View File
@@ -5,7 +5,6 @@ import spotipy
import spotipy.util as util
if len(sys.argv) > 1:
username = sys.argv[1]
else:
@@ -23,7 +22,7 @@ if token:
while tracks:
for item in tracks['items']:
track = item['track']
print(which, track['name' ], ' --', track['artists'][0]['name'])
print(which, track['name'], ' --', track['artists'][0]['name'])
which += 1
tracks = sp.next(tracks)