Spotipy environment variables
Hi guys im new in the pythonista world, Im not a very good english writer so I’ll try to be short
I have a small idea of a script in python but I need to work with Spotipy, but when I try to start the script says the client_id is not set, I don’t understand where I have to set it, I tried to modify the oauth2.py in the variables, but doesn’t work, where I set the environment variable?
This is an example code i need to make work (sesrch.py on spotipy github page)
from spotipy.oauth2 import SpotifyClientCredentials import spotipy if len(sys.argv) > 1: search_str = sys.argv else: search_str = 'Radiohead' sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) result = sp.search(search_str) pprint.pprint(result)
@MrKingDice, OAuth2 is always very confusing the first time around.
You need to register your app with Spotify to get a
Instructions for getting those are here.
There you also need to whitelist a redirect uri, docs say to use http://localhost for that.
Then set SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET environment variables with these values, e.g.:
import os os.environ['SPOTIPY_CLIENT_ID'] = 'your value'
Expect to get asked for your Spotify credentials when you run your script.
Thank you I’ve resolved partially, I’ve moved the problem in client.py in the spotipy api,
Pythonista3/Documents/site-packages-3/spotipy/client.py", line 151, in _internal_call method, url, headers=headers, proxies=self.proxies, **args AttributeError: __enter__
@MrKingDice, I encountered the same issue and tried to use this method instead:
import spotipy.util as util util.prompt_for_user_token(username', 'user-library-read', #client_id='your-spotify-client-id', #client_secret='your-spotify-client-secret', redirect_uri='http://localhost')
Id and secret commented out as they come via the environment variables.
Unfortunately this method does not work very well on Pythonista, opening another panel for entering the credentials, then asking for urls, which are difficult to find while the script is running.
If I had a need to access Spotify from Pythonista, I would be sorely tempted to skip Spotipy, use raw OAuth2 and write directly against the Spotify Web API.
@mikael, I'll try that code but I think the second option is more viable, I'll just need more time to get the things done
@MrKingDice, I had something laying around and repurposed it.
See here, get the
from spoter import Spoter spot = Spoter(client_id=client_id, client_secret=client_secret) # Or set values as environment variables: # - SPOTIFY_CLIENT_ID # - SPOTIFY_CLIENT_SECRET print('Search for playlists for Medieval ambient music') print('-----------------------------------------------') result = spot.search('Medieval ambient', 'playlist') for item in result['playlists']['items']: print(item['name'])
First time around, it will open a web tab in Pythonista or a browser on PC/Mac for you to enter your Spotify credentials. After that, it will store a refresh token (by default in Documents) and you will not need to authenticate again.
searchis the only method implemented right now. Tell me what you need and I can probably quickly add it, unless you want to go for it, in which case I appreciate the pull requests.
@mikael That is perfect for my use case, so I can create a converter to apple music song to spotify link for sharing purpuose
Take a look if u want to integrate
and the script code:
from spoter import Spoter import clipboard import webbrowser query=clipboard.get().splitlines() q='track:'+query+' artist:'+query.replace('&','')+' album:'+query.replace('- Single','') #apple music put & when a song is made from 2 artists and add - Single in the album name q.replace(" ","%20") print(q) spot = Spoter(client_id='client_id', client_secret='client_secret') result = spot.search(q, 'track') clipboard.set(result['tracks']['items']['external_urls']['spotify']) url = "music://x-callback-url/run--shortcut?name=[Test]&input=clipboard" #I Assume you are using the shortcut in share option in music app webbrowser.open(url)
Is not perfect at 100% but I'm bad at finding "hard" testing cases
@MrKingDice, nice use case. I still do not want to move to Apple Music, but if I did, this would be my way of getting 50% there to get the playlists transferred.
As a side note, maybe not include your client ID and secret in the post here?
@mikael sorry my error, I have modified it now lol
I'll test and modify if needed, if I come with something useful I'll make a pull request