Skip to content

Commit

Permalink
Add linter, solves #348 (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanebruckert authored Jan 12, 2020
1 parent 5928981 commit f54830e
Show file tree
Hide file tree
Showing 35 changed files with 322 additions and 246 deletions.
5 changes: 4 additions & 1 deletion examples/artist_albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
''' 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']
Expand All @@ -12,21 +13,23 @@ def get_artist(name):
else:
return None


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'])
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']
if name not in seen:
print((' ' + name))
seen.add(name)


if __name__ == '__main__':
sp = spotipy.Spotify()

Expand Down
7 changes: 6 additions & 1 deletion examples/artist_discography.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
''' 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']
Expand All @@ -12,6 +13,7 @@ def get_artist(name):
else:
return None


def show_album_tracks(album):
tracks = []
results = sp.album_tracks(album['id'])
Expand All @@ -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')
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions examples/artist_recommendations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
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')
Expand All @@ -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:
Expand All @@ -32,4 +34,4 @@ def show_recommendations_for_artist(artist):
if artist:
show_recommendations_for_artist(artist)
else:
print "Can't find that artist", name
print("Can't find that artist", name)
2 changes: 1 addition & 1 deletion examples/audio_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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:])
Expand Down
2 changes: 1 addition & 1 deletion examples/audio_features_for_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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:]
Expand Down
6 changes: 3 additions & 3 deletions examples/change_playlist_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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'
Expand All @@ -37,6 +37,6 @@
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
4 changes: 3 additions & 1 deletion examples/create_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
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"
Expand Down
3 changes: 1 addition & 2 deletions examples/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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')
Expand All @@ -17,4 +17,3 @@
print('bad call 1')
bad_artist_call = sp.artist('spotify:artist:12341234')
print('bad artist', bad_artist_call)

2 changes: 1 addition & 1 deletion examples/my_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
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)
8 changes: 3 additions & 5 deletions examples/my_top_artists.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -21,10 +19,10 @@
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)
8 changes: 3 additions & 5 deletions examples/my_top_tracks.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -21,11 +19,11 @@
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)
2 changes: 1 addition & 1 deletion examples/read_a_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
playlist_id = uri.split(':')[4]

results = sp.user_playlist(username, playlist_id)
print (json.dumps(results, indent=4))
print(json.dumps(results, indent=4))
7 changes: 5 additions & 2 deletions examples/remove_specific_tracks_from_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
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'
Expand All @@ -24,7 +26,8 @@
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 changes: 2 additions & 1 deletion examples/remove_tracks_from_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
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)
3 changes: 1 addition & 2 deletions examples/show_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@
print('Related artists for', name)
for artist in related['artists']:
print(' ', artist['name'])
except:
except BaseException:
print("usage show_related.py [artist-name]")

3 changes: 0 additions & 3 deletions examples/show_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@
results = sp.tracks(tids[start: start + max_tracks_per_call])
for track in results['tracks']:
print(track['name'] + ' - ' + track['artists'][0]['name'])



1 change: 0 additions & 1 deletion examples/show_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@
sp.trace = True
user = sp.user(username)
pprint.pprint(user)

1 change: 0 additions & 1 deletion examples/simple1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@

for album in albums:
print((album['name']))

1 change: 0 additions & 1 deletion examples/simple3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
if len(items) > 0:
artist = items[0]
print(artist['name'], artist['images'][0]['url'])

16 changes: 9 additions & 7 deletions examples/title_chain.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,13 +13,14 @@
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
Expand All @@ -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())

9 changes: 6 additions & 3 deletions examples/user_playlists_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
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__':
Expand All @@ -28,12 +31,12 @@ def show_tracks(results):
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']:
tracks = sp.next(tracks)
show_tracks(tracks)
else:
print("Can't get token for", username)

Loading

0 comments on commit f54830e

Please sign in to comment.