Use f-strings + code simplification (#961)

Resolves #960
This commit is contained in:
Atharva Shah authored and GitHub committed 2025-01-16 00:51:26 +00:00
1 parent 3c75886229
commit 5d0b8edff4
28 files changed
+334 -329

No files matched your search

+1 -3
View File
@@ -88,9 +88,7 @@ def currently_playing():
return redirect('/')
spotify = spotipy.Spotify(auth_manager=auth_manager)
track = spotify.current_user_playing_track()
if not track is None:
return track
return "No track currently playing."
return track if track is not None else "No track currently playing."
@app.route('/current_user')
+2 -5
View File
@@ -18,12 +18,9 @@ def get_args():
def get_artist(name):
results = sp.search(q='artist:' + name, type='artist')
results = sp.search(q=f'artist:{name}', type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
return items[0] if len(items) > 0 else None
def show_artist_albums(artist):
+2 -5
View File
@@ -18,12 +18,9 @@ def get_args():
def get_artist(name):
results = sp.search(q='artist:' + name, type='artist')
results = sp.search(q=f'artist:{name}', type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
return items[0] if len(items) > 0 else None
def show_album_tracks(album):
+2 -5
View File
@@ -20,12 +20,9 @@ def get_args():
def get_artist(name):
results = sp.search(q='artist:' + name, type='artist')
results = sp.search(q=f'artist:{name}', type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
return items[0] if len(items) > 0 else None
def show_recommendations_for_artist(artist):
+12
View File
@@ -0,0 +1,12 @@
# Add a list of items (URI) to a playlist (URI)
import spotipy
from spotipy.oauth2 import SpotifyOAuth
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR_APP_CLIENT_ID",
client_secret="YOUR_APP_CLIENT_SECRET",
redirect_uri="YOUR_APP_REDIRECT_URI",
scope="playlist-modify-private"
))
sp.playlist_add_items('playlist_id', ['list_of_items'])
+1 -2
View File
@@ -25,6 +25,5 @@ for item in tracks:
tracks.remove(item)
i += 1
# print result
print("Playlist length: " + str(len(tracks)) + "\nExcluding: " + str(i))
print(f"Playlist length: {len(tracks)}\nExcluding: {i}")
@@ -14,8 +14,7 @@ if len(sys.argv) > 2:
tid, pos = t_pos.split(',')
track_ids.append({"uri": tid, "positions": [int(pos)]})
else:
print(
f"Usage: {sys.argv[0]} playlist_id track_id,pos track_id,pos ...")
print(f"Usage: {sys.argv[0]} playlist_id track_id,pos track_id,pos ...")
sys.exit()
scope = 'playlist-modify-public'
+1 -5
View File
@@ -5,11 +5,7 @@ import spotipy
import sys
import pprint
if len(sys.argv) > 1:
search_str = sys.argv[1]
else:
search_str = 'Radiohead'
search_str = sys.argv[1] if len(sys.argv) > 1 else 'Radiohead'
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
result = sp.search(search_str)
pprint.pprint(result)
+1 -4
View File
@@ -13,7 +13,4 @@ while response:
for i, item in enumerate(playlists['items']):
print(playlists['offset'] + i, item['name'])
if playlists['next']:
response = sp.next(playlists)
else:
response = None
response = sp.next(playlists) if playlists['next'] else None
+1 -4
View File
@@ -12,7 +12,4 @@ while response:
for i, item in enumerate(albums['items']):
print(albums['offset'] + i, item['name'])
if albums['next']:
response = sp.next(albums)
else:
response = None
response = sp.next(albums) if albums['next'] else None
+2 -6
View File
@@ -4,14 +4,10 @@ from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import sys
if len(sys.argv) > 1:
artist_name = sys.argv[1]
else:
artist_name = 'weezer'
artist_name = sys.argv[1] if len(sys.argv) > 1 else 'weezer'
auth_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(auth_manager=auth_manager)
result = sp.search(q='artist:' + artist_name, type='artist')
result = sp.search(q=f'artist:{artist_name}', type='artist')
try:
name = result['artists']['items'][0]['name']
uri = result['artists']['items'][0]['uri']
+1 -5
View File
@@ -5,11 +5,7 @@ import spotipy
import sys
import pprint
if len(sys.argv) > 1:
username = sys.argv[1]
else:
username = 'plamere'
username = sys.argv[1] if len(sys.argv) > 1 else 'plamere'
auth_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(auth_manager=auth_manager)
sp.trace = True
+2 -6
View File
@@ -6,12 +6,8 @@ import spotipy
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
if len(sys.argv) > 1:
name = ' '.join(sys.argv[1:])
else:
name = 'Radiohead'
results = sp.search(q='artist:' + name, type='artist')
name = ' '.join(sys.argv[1:]) if len(sys.argv) > 1 else 'Radiohead'
results = sp.search(q=f'artist:{name}', type='artist')
items = results['artists']['items']
if len(items) > 0:
artist = items[0]
+1 -3
View File
@@ -39,7 +39,5 @@ recommendations = sp.recommendations(
# Display the recommendations
for i, track in enumerate(recommendations['tracks']):
print(
"{}. {} by {}"
.format(i+1, track['name'], ', '
.join([artist['name'] for artist in track['artists']]))
f"{i+1}. {track['name']} by {', '.join([artist['name'] for artist in track['artists']])}"
)
+2 -9
View File
@@ -9,11 +9,7 @@ from spotipy.oauth2 import SpotifyClientCredentials
auth_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(auth_manager=auth_manager)
user = 'spotify'
if len(sys.argv) > 1:
user = sys.argv[1]
user = sys.argv[1] if len(sys.argv) > 1 else 'spotify'
playlists = sp.user_playlists(user)
while playlists:
@@ -25,7 +21,4 @@ while playlists:
playlists['offset'],
playlist['uri'],
playlist['name']))
if playlists['next']:
playlists = sp.next(playlists)
else:
playlists = None
playlists = sp.next(playlists) if playlists['next'] else None