riverscuomo / spotkin_server

A python package that updates one or more of your Spotify playlists every day with a random selection of songs from any playlists you specify. Here's mine: https://open.spotify.com/playlist/1HaQfSGjNzIsiC5qOsCUcW?si=ddc16d3e9524410c . This repo also contains the backend server for Spotkin webapp.
GNU General Public License v3.0
83 stars 8 forks source link

Cleanup + Fixes #9

Closed TimStewartJ closed 1 year ago

TimStewartJ commented 1 year ago
riverscuomo commented 1 year ago

I'm really excited about this new auth flow. I'm hoping it will lead the way to non-programmers being able to use the app, via a webapp or something.

TimStewartJ commented 1 year ago

Yeah, the new auth flow doesn't require a user ID anymore, making things a little easier for first time setup. Users won't have to dig around their spotify settings to find their ID, which is harder than it should be.

Not much else has changed in terms of interaction for this PR. I've tried to preserve as much of the original functionality as possible, just focusing on readability and clean up. A webapp interface for spotnik sounds really interesting. Maybe we could make a discussion for it to figure out how we want to approach it? It seems like a longer-term overhaul that would benefit from focused development.

There are still some chunks of code and comments that seem to be left over from earlier states of development, but I've left most of them intact in case any useful information could be garnered from them. Other than that, I'd say this is good to merge, barring any comments from anyone else.

riverscuomo commented 1 year ago

@TimStewartJ hmm...without passing my Spotify username as a parameter, I get this error in get_spotify(). Do I have to get a new set of Spotify developer creds maybe?

Getting tracks from the playlists earmarked for the Spotnik Example Playlist 1 playlist...
- sampling a maximum of 8 Spotify tracks from the playlist 'albumsImCheckingOut'... 
Traceback (most recent call last):
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\oauth2.py", line 588, in refresh_access_token
    response.raise_for_status()
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\aethe\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\aethe\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotnik\spotnik\__main__.py", line 117, in <module>
    main()
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotnik\spotnik\__main__.py", line 53, in main
    tracks = get_all_tracks(data, job, spotify)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotnik\spotnik\get_all_tracks.py", line 37, in get_all_tracks
    new_tracks = sample_playlist_tracks(
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotnik\spotnik\api.py", line 64, in sample_playlist_tracks
    all_tracks = get_playlist_tracks(spotify, playlist_id)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotnik\spotnik\api.py", line 74, in get_playlist_tracks
    results = spotify.playlist_tracks(playlist_id)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\client.py", line 649, in playlist_tracks
    return self.playlist_items(playlist_id, fields, limit, offset,
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\client.py", line 673, in playlist_items
    return self._get(
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\client.py", line 221, in _internal_call
    headers = self._auth_headers()
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\oauth2.py", line 525, in get_access_token
    token_info = self.validate_token(self.cache_handler.get_cached_token())
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\oauth2.py", line 380, in validate_token
    token_info = self.refresh_access_token(
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\oauth2.py", line 596, in refresh_access_token
    self._handle_oauth_error(http_error)
  File "C:\RC Dropbox\Rivers Cuomo\Apps\.G\lib\site-packages\spotipy\oauth2.py", line 146, in _handle_oauth_error
    raise SpotifyOauthError(
spotipy.oauth2.SpotifyOauthError: error: invalid_client, error_description: Invalid client
TimStewartJ commented 1 year ago

@riverscuomo That's really odd... I'm trying to reproduce it but I can't. In the meantime I would recommend the following steps:

riverscuomo commented 1 year ago

@TimStewartJ Thanks. I deleted .cache and .cache-rcuomo and then it ran successfully. I saw no sign of the program reaching oauthStepTwo(), so I'm not sure how it worked. But work, it did.

TimStewartJ commented 1 year ago

Great, glad to hear it. Yeah I don't think oauthStepTwo() is necessary any more, spotipy should handle login in most situations that require it, like when the cached token expires, or if access to the app is revoked.