Closed KamranAbbaszadeIbrahim closed 2 years ago
Is there any review for current issue?
@KamranAbbaszadeIbrahim it works for me:
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from pprint import pprint
sp = spotipy.Spotify(auth_manager=SpotifyOAuth())
pprint(
sp.search(q="Folk punk", limit=50, type='playlist').get('playlists')["items"]
)
[{'collaborative': False,
'description': '',
'external_urls': {'spotify': 'https://open.spotify.com/playlist/6jvQiScdEWO3oZNrcISu6X'},
'href': 'https://api.spotify.com/v1/playlists/6jvQiScdEWO3oZNrcISu6X',
'id': '6jvQiScdEWO3oZNrcISu6X',
'images': [{'height': 640,
'url': 'https://mosaic.scdn.co/640/ab67616d0000b2732a4aeff3ddbbc30af9131e12ab67616d0000b27354096a7f898de0233d76f626ab67616d0000b273c8fee8b836a2cad100d5fbc9ab67616d0000b273c94be489babeb56b78d47bb5',
'width': 640},
{'height': 300,
'url': 'https://mosaic.scdn.co/300/ab67616d0000b2732a4aeff3ddbbc30af9131e12ab67616d0000b27354096a7f898de0233d76f626ab67616d0000b273c8fee8b836a2cad100d5fbc9ab67616d0000b273c94be489babeb56b78d47bb5',
'width': 300},
{'height': 60,
'url': 'https://mosaic.scdn.co/60/ab67616d0000b2732a4aeff3ddbbc30af9131e12ab67616d0000b27354096a7f898de0233d76f626ab67616d0000b273c8fee8b836a2cad100d5fbc9ab67616d0000b273c94be489babeb56b78d47bb5',
'width': 60}],
'name': 'Folk-Punk',
'owner': {'display_name': 'Tom Sherman',
'external_urls': {'spotify': 'https://open.spotify.com/user/tom-sherman'},
'href': 'https://api.spotify.com/v1/users/tom-sherman',
'id': 'tom-sherman',
'type': 'user',
'uri': 'spotify:user:tom-sherman'},
'primary_color': None,
'public': None,
'snapshot_id': 'NjI0LGE0MWJmMTNkNGE0NTZjYzRiOTY5NzE5ZDg2OTk2M2I4NWVmMzU0N2Q=',
'tracks': {'href': 'https://api.spotify.com/v1/playlists/6jvQiScdEWO3oZNrcISu6X/tracks',
'total': 492},
'type': 'playlist',
'uri': 'spotify:playlist:6jvQiScdEWO3oZNrcISu6X'},
{'collaborative': False,
'description': '',
'external_urls': {'spotify': 'https://open.spotify.com/playlist/5HwtKsl7l7Y8qhb3XN2EDc'},
...
@stephanebruckert If you will check the same search query in Browser on spotify web pge, you will see that there are Playlists by Spotify. But when you query it from google colab, it did not retrieve me any playlists by Spotify, only by independent users. In your example, it is Tom Sherman.
This bug is observed not in every query. In Some queries I can see Playlists created by Spotify, in others - No.
Sorry I misread your initial post, however I can still see playlists from spotify:user:spotify
in the results:
{'collaborative': False,
'description': 'Folk punk rock music picked just for you',
'external_urls': {'spotify': 'https://open.spotify.com/playlist/37i9dQZF1EIhqpYvooO9sF'},
'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1EIhqpYvooO9sF',
'id': '37i9dQZF1EIhqpYvooO9sF',
'images': [{'height': None,
'url': 'https://seed-mix-image.spotifycdn.com/v6/img/desc/Folk%20Punk%20Rock/en/large',
'width': None}],
'name': 'Folk Punk Rock Mix',
'owner': {'display_name': 'Spotify',
'external_urls': {'spotify': 'https://open.spotify.com/user/spotify'},
'href': 'https://api.spotify.com/v1/users/spotify',
'id': 'spotify',
'type': 'user',
'uri': 'spotify:user:spotify'},
'primary_color': None,
'public': None,
'snapshot_id': 'MTkyNDIsMDAwMDAwMDA1YzhlM2MzY2QzNDY4YmJlMDdiYzBiMTJhODA4MTgwNQ==',
'tracks': {'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1EIhqpYvooO9sF/tracks',
'total': 50},
'type': 'playlist',
'uri': 'spotify:playlist:37i9dQZF1EIhqpYvooO9sF'},
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from pprint import pprint
sp = spotipy.Spotify(auth_manager=SpotifyOAuth())
for pl in sp.search(q="Folk punk", limit=50, type='playlist').get('playlists')["items"]:
if pl['owner']['uri'] == 'spotify:user:spotify':
pprint(pl)
Could you explain, how did you get this result? is it because you use SpotifyOAuth rather than SpotifyClientCredentials? The only difference in my and your code is the Auth method. I use client_id and client_secret that I generated.
And I did not get any result.
from spotipy.oauth2 import SpotifyClientCredentials
from pprint import pprint
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))
for pl in sp.search(q="Folk punk", limit=50, type='playlist').get('playlists')["items"]:
if pl['owner']['uri'] == 'spotify:user:spotify':
pprint(pl)
Same with SpotifyClientCredentials
:
➜ cat main.py
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from pprint import pprint
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
for pl in sp.search(q="Folk punk", limit=50, type='playlist').get('playlists')["items"]:
if pl['owner']['uri'] == 'spotify:user:spotify':
pprint(pl)
➜ python3 main.py
{'collaborative': False,
'description': 'Folk punk rock music picked just for you',
'external_urls': {'spotify': 'https://open.spotify.com/playlist/37i9dQZF1EIhqpYvooO9sF'},
'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1EIhqpYvooO9sF',
'id': '37i9dQZF1EIhqpYvooO9sF',
'images': [{'height': None,
'url': 'https://seed-mix-image.spotifycdn.com/v6/img/desc/Folk%20Punk%20Rock/en/large',
'width': None}],
'name': 'Folk Punk Rock Mix',
'owner': {'display_name': 'Spotify',
'external_urls': {'spotify': 'https://open.spotify.com/user/spotify'},
'href': 'https://api.spotify.com/v1/users/spotify',
'id': 'spotify',
'type': 'user',
'uri': 'spotify:user:spotify'},
'primary_color': None,
'public': None,
'snapshot_id': 'MTkyNDIsMDAwMDAwMDA1YzhlM2MzY2QzNDY4YmJlMDdiYzBiMTJhODA4MTgwNQ==',
'tracks': {'href': 'https://api.spotify.com/v1/playlists/37i9dQZF1EIhqpYvooO9sF/tracks',
'total': 50},
'type': 'playlist',
'uri': 'spotify:playlist:37i9dQZF1EIhqpYvooO9sF'}
@stephanebruckert I do really apologize, but I still do not understand why I can not get the same result as you?
@stephanebruckert Additionally, I tried to register new account and create new client_id and client_secret. It did not help.
The only thing that comes to my mind is the current market being searched.
There can be differences between what you see between the web and api results https://github.com/plamere/spotipy/blob/master/FAQ.md#search-doesnt-find-some-tracks
According to https://developer.spotify.com/documentation/web-api/reference/#/operations/search it looks like Spotify is not searching the us
market code by default anymore.
Therefore can you try to add market='US'
to the search method such as:
sp.search(q="Folk punk", limit=50, type='playlist', market='US')
or try to use different market codes from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 and see if it changes anything.
@stephanebruckert
I tried with us
market code - Did not work.
I tried with next code
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from pprint import pprint
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))
code_temp = 'AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH BI BJ BK BL BM BN BO BP BQ BR BS BT BU BV BW BX BY BZ CA CB CC CD CE CF CG CH CI CJ CK CL CM CN CO CP CQ CR CS CT CU CV CW CX CY CZ DA DB DC DD DE DF DG DH DI DJ DK DL DM DN DO DP DQ DR DS DT DU DV DW DX DY DZ EA EB EC ED EE EF EG EH EI EJ EK EL EM EN EO EP EQ ER ES ET EU EV EW EX EY EZ FA FB FC FD FE FF FG FH FI FJ FK FL FM FN FO FP FQ FR FS FT FU FV FW FX FY FZ GA GB GC GD GE GF GG GH GI GJ GK GL GM GN GO GP GQ GR GS GT GU GV GW GX GY GZ HA HB HC HD HE HF HG HH HI HJ HK HL HM HN HO HP HQ HR HS HT HU HV HW HX HY HZ IA IB IC ID IE IF IG IH II IJ IK IL IM IN IO IP IQ IR IS IT IU IV IW IX IY IZ JA JB JC JD JE JF JG JH JI JJ JK JL JM JN JO JP JQ JR JS JT JU JV JW JX JY JZ KA KB KC KD KE KF KG KH KI KJ KK KL KM KN KO KP KQ KR KS KT KU KV KW KX KY KZ LA LB LC LD LE LF LG LH LI LJ LK LL LM LN LO LP LQ LR LS LT LU LV LW LX LY LZ MA MB MC MD ME MF MG MH MI MJ MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NB NC ND NE NF NG NH NI NJ NK NL NM NN NO NP NQ NR NS NT NU NV NW NX NY NZ OA OB OC OD OE OF OG OH OI OJ OK OL OM ON OO OP OQ OR OS OT OU OV OW OX OY OZ PA PB PC PD PE PF PG PH PI PJ PK PL PM PN PO PP PQ PR PS PT PU PV PW PX PY PZ QA QB QC QD QE QF QG QH QI QJ QK QL QM QN QO QP QQ QR QS QT QU QV QW QX QY QZ RA RB RC RD RE RF RG RH RI RJ RK RL RM RN RO RP RQ RR RS RT RU RV RW RX RY RZ SA SB SC SD SE SF SG SH SI SJ SK SL SM SN SO SP SQ SR SS ST SU SV SW SX SY SZ TA TB TC TD TE TF TG TH TI TJ TK TL TM TN TO TP TQ TR TS TT TU TV TW TX TY TZ UA UB UC UD UE UF UG UH UI UJ UK UL UM UN UO UP UQ UR US UT UU UV UW UX UY UZ VA VB VC VD VE VF VG VH VI VJ VK VL VM VN VO VP VQ VR VS VT VU VV VW VX VY VZ WA WB WC WD WE WF WG WH WI WJ WK WL WM WN WO WP WQ WR WS WT WU WV WW WX WY WZ XA XB XC XD XE XF XG XH XI XJ XK XL XM XN XO XP XQ XR XS XT XU XV XW XX XY XZ YA YB YC YD YE YF YG YH YI YJ YK YL YM YN YO YP YQ YR YS YT YU YV YW YX YY YZ ZA ZB ZC ZD ZE ZF ZG ZH ZI ZJ ZK ZL ZM ZN ZO ZP ZQ ZR ZS ZT ZU ZV ZW ZX ZY ZZ'
code_temp = code_temp.replace('\t', ' ')
code = code_temp.split(" ")
for c in code:
try:
for pl in sp.search(q="Folk punk", limit=50, type='playlist', market=c).get('playlists')["items"]:
if pl['owner']['uri'] == 'spotify:user:spotify':
pprint(pl)
except Exception as e:
print(c)
pass
Again, no success. This is the output:
Yeah not all markets are valid, that's fine.
Still your problem isn't solved. I run out of ideas and it might indeed be a bug on the Spotify API side. In any case it's not python related so I suggest you open a bug on https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer (feel free to post the link there for reference)
@stephanebruckert
I have one assumption, but in order to test it, I need your help.
Can you explain how to properly set the SpotifyOAuth especially redirect_url?
I want to check whether this issue occurs because of missing some scopes or not.
I can even share the client_id and client_secret of one of the accounts that I use, so that you can check whether it is issue with account or not.
I'm happy to try with your client ID + secret if you send them to my email (can you see it on my profile?).
Can you explain how to properly set the SpotifyOAuth especially redirect_url?
Your spotify app must have a redirect URI set (eg. http://localhost:8080), see https://spotipy.readthedocs.io/en/master/#redirect-uri. Then I use the code I pasted above and run it with:
SPOTIPY_CLIENT_ID=my-app-id SPOTIPY_CLIENT_SECRET=my-app-secret SPOTIPY_REDIRECT_URI=http://localhost:8080 python3 main.py
You can also set the secrets like shown here https://github.com/plamere/spotipy#with-user-authentication
In the case of Google Colab it's a bit different, it's likely going to give you a URL that you must visit, and then you have to provide the result of that URL back to the terminal.
@stephanebruckert I have sent you the client_id and client_secret via email. I am trying to execute the code shared by you using redirect_url parameter. As soon as i get updates, I will share them.
I tried one of your credentials, didn't work (no spotify playlist). Tried again with mine and didn't work either! I'm super confused. Give me some time, I'm trying to get my own credentials to work again
@stephanebruckert At least, I am not alone with this issue, I appreciate your help)
Ok I found the problem. After I switch from SpotifyOAuth
to SpotifyClientCredentials
, I didn't delete the cached token (.cache
) on my local environment. So it was still using SpotifyOAuth
.
Now I'm cleaning up the token after each run and I can confirm it works with SpotifyOAuth
and not with SpotifyClientCredentials
!
@stephanebruckert Okay, so I will describe my current issues with SpotifyOAuth.
I used:
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from pprint import pprint
redirect = "http://localhost:4444/"
scope="playlist-read-private, playlist-read-collaborative"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, scope=scope, redirect_uri=redirect))
#sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
for pl in sp.search(q="Folk punk", limit=50, type='playlist').get('playlists')["items"]:
if pl['owner']['uri'] == 'spotify:user:spotify':
pprint(pl)
In the Developer Dashboard, I went to Application Settings and set Redirect URIs the same value => http://localhost:4444/
I confirm that current port is free.
When I run the code in google colab, my code does not complete.
Any idea what I am doing wrong? It is already running 13 minutes. I tried also to open google colab notebook in incognito , in Chrome.
@stephanebruckert I could not apply successfully the steps described in the link((
What is your code/error?
It always fails in Redirect_URI part. I tried to generate token in Developer Console manually and it worked, I could run your code and retrieve results which I expected. But, please, could you help me to be able to use next code?
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from spotipy import util
from pprint import pprint
redirect = "http://localhost:4444/"
scope="playlist-read-private, playlist-read-collaborative"
username = '0p1zntuvzzmqct6yn0lvlv'
token = util.prompt_for_user_token(username,
scope,
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect)
sp = spotipy.Spotify(auth=token)
Don't use util.prompt_for_user_token
as it's deprecated, but use what's on the README https://github.com/plamere/spotipy#with-user-authentication with open_browser=False
.
I could make it work, but in this case, I had to go to specific url and then copy the redirected url into input box
Go to the following URL: https://accounts.spotify.com/authorize?client_id=947fdb7706664305a5bb23c0105a9de7&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A4444%2F&scope=+playlist-read-collaborative+playlist-read-private
Enter the URL you were redirected to: http://localhost:4444/?code=AQDlqR6zrg_fKwx1A4Zwt-9l6rLvcR0eSgKDAQRlay6CEXG_4jVKabZW9gIznehq7Ckb4hNC70vUe4jim8EpPG7lBI2bR5M2It9EIZApV7YNHHw120deN7Jb0mbO5anJ6_sh8OTQs3cx25F-YO93DT5z9lPDhgi6kP2sKQ-5AhB4WS-bjM8bfSWwDlnUVjQsuP_strl72UglI5n3IuIEsLSqSXAOQylTzn3vnxz5
And it worked. Is there any way, to not be in a need to do like this?
This is the only way on Google Colab, because obviously your browser cannot open another browser/tab.
Locally you wouldn't have to do this but it would still open your browser and require you to sign in.
However, locally the token would be cached and refreshed automatically so you would only need to sign in once. On Google Colab I'm not sure you can cache the token.
How many request (approximatelly) I will be able to execute using this Auth method? Because with SpotifyClientCredentials i was able to execute around 25K-27K requests per day. THen I was just changing manually the client_id and client_secret, cleaning cache and running again.
It's the same AFAIK
Okay. @stephanebruckert Thank you very very much for your help. The solution for this Issue is to use SpotifyOAuth instead of SpotifyClientCredentials.
Describe the bug When 'Search' function is used to query playlists matching certan key words, the results do not contain playlists, which are available on official web page of spotify. The search query is the same. While in results of Official Webpage of Spotify I can see playlists, created by Spotify (where owner of playlist is user with id=spotify), in results of Spotipy Library with the same search query, I sometimes get playlists created by Spotify user, sometimes I do not get any.
Your code
Expected behavior I expect to retrieve from Spotipy the same results that I can get from Spotify Web Page.
Output (https://open.spotify.com/search/Folk%20punk/playlists) - here is the output that I expect to see.
Environment:
Additional context Additionally, I checked function from Spotipy - user_playlists. While on https://open.spotify.com/user/spotify page I can see that Spotify user has 1520 public playlists, with almost the same code:
I am getting less number of playlists (1177) Is there something that I missed?