Auto-close auth browser window

This commit is contained in:
Stephane Bruckert 2020-04-01 21:25:48 +01:00
parent acdcfcecf8
commit 73aac90bbe
5 changed files with 44 additions and 17 deletions

View File

@ -6,13 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- Added CONTRIBUTING.md
### Changed ### Changed
- Client retry logic has changed as it now uses urllib3's `Retry` in conjunction with requests `Session` - Client retry logic has changed as it now uses urllib3's `Retry` in conjunction with requests `Session`
- The session is customizable as it allows for: - The session is customizable as it allows for:
- status_forcelist - status_forcelist
- retries - retries
- status_retries - status_retries
- backoff_factor - backoff_factor
- Spin up a local webserver to auto-fill authentication URL
## [2.10.0] - 2020-03-18 ## [2.10.0] - 2020-03-18

View File

@ -1,7 +1,9 @@
## Contributing ## Contributing
If you would like to contribute to spotipy follow these steps: If you would like to contribute to spotipy follow these steps:
### Export the needed environment variables ### Export the needed environment variables
```bash ```bash
export SPOTIPY_CLIENT_ID=client_id_here export SPOTIPY_CLIENT_ID=client_id_here
export SPOTIPY_CLIENT_SECRET=client_secret_here export SPOTIPY_CLIENT_SECRET=client_secret_here
@ -9,10 +11,26 @@ export SPOTIPY_CLIENT_USERNAME=client_username_here # This is actually an id not
export SPOTIPY_REDIRECT_URI=http://localhost/ # Make url is set in app you created to get your ID and SECRET export SPOTIPY_REDIRECT_URI=http://localhost/ # Make url is set in app you created to get your ID and SECRET
``` ```
### Create virtual enevironment, install dependencies, run tests: ### Create virtual environment, install dependencies, run tests:
```bash ```bash
$ virtualenv --python=python3.7 env $ virtualenv --python=python3.7 env
(env) $ pip install requirements.txt (env) $ pip install requirements.txt
(env) $ python -m unittest discover -v tests (env) $ python -m unittest discover -v tests
``` ```
### Lint
To automatically fix the code style:
pip install autopep8
autopep8 --in-place --aggressive --recursive .
To verify the code style:
pip install flake8
flake8 .
### README
Don't forget to add a short description of your change in the [CHANGELOG](CHANGELOG.md)

View File

@ -320,8 +320,7 @@ class SpotifyOAuth(SpotifyAuthBase):
return self.parse_response_code(response) return self.parse_response_code(response)
def _get_auth_response_local_server(self, redirect_port): def _get_auth_response_local_server(self, redirect_port):
server = start_local_http_server(redirect_port)
with start_local_http_server(redirect_port) as server:
self._open_auth_url() self._open_auth_url()
server.handle_request() server.handle_request()
@ -488,9 +487,12 @@ class RequestHandler(BaseHTTPRequestHandler):
return return
self._write("""<html> self._write("""<html>
<script>
window.close()
</script>
<body> <body>
<h1>Authentication status: {}</h1> <h1>Authentication status: {}</h1>
Now you can close this window or tab. This window can be closed.
</body> </body>
</html>""".format(status)) </html>""".format(status))

View File

@ -192,8 +192,9 @@ class AuthTestSpotipy(unittest.TestCase):
sp = spotipy.Spotify(requests_timeout=0.01, sp = spotipy.Spotify(requests_timeout=0.01,
client_credentials_manager=client_credentials_manager) client_credentials_manager=client_credentials_manager)
with self.assertRaises(requests.exceptions.Timeout): # depending on the timing or bandwidth, this raises a timeout or connection error"
sp.search(q='my*', type='track') self.assertRaises((requests.exceptions.Timeout, requests.exceptions.ConnectionError),
lambda: sp.search(q='my*', type='track'))
def test_album_search(self): def test_album_search(self):
results = self.spotify.search(q='weezer pinkerton', type='album') results = self.spotify.search(q='weezer pinkerton', type='album')

View File

@ -59,7 +59,7 @@ class SpotipyPlaylistApiTest(unittest.TestCase):
user = playlist['owner']['id'] user = playlist['owner']['id']
pid = playlist['id'] pid = playlist['id']
results = self.spotify.user_playlist_tracks(user, pid) results = self.spotify.user_playlist_tracks(user, pid)
self.assertEquals(len(results['items']), 0) self.assertEqual(len(results['items']), 0)
def test_current_user_playlists(self): def test_current_user_playlists(self):
playlists = self.spotify.current_user_playlists(limit=10) playlists = self.spotify.current_user_playlists(limit=10)
@ -134,7 +134,7 @@ class SpotipyPlaylistApiTest(unittest.TestCase):
self.spotify.playlist_upload_cover_image(self.new_playlist_uri, dog_base64) self.spotify.playlist_upload_cover_image(self.new_playlist_uri, dog_base64)
res = self.spotify.playlist_cover_image(self.new_playlist_uri) res = self.spotify.playlist_cover_image(self.new_playlist_uri)
self.assertEquals(len(res), 1) self.assertEqual(len(res), 1)
first_image = res[0] first_image = res[0]
self.assertIn('width', first_image) self.assertIn('width', first_image)
self.assertIn('height', first_image) self.assertIn('height', first_image)
@ -196,13 +196,13 @@ class SpotipyLibraryApiTests(unittest.TestCase):
tracks = self.spotify.current_user_saved_tracks() tracks = self.spotify.current_user_saved_tracks()
new_total = tracks['total'] new_total = tracks['total']
self.assertEquals(new_total - total, len(self.four_tracks)) self.assertEqual(new_total - total, len(self.four_tracks))
tracks = self.spotify.current_user_saved_tracks_delete( tracks = self.spotify.current_user_saved_tracks_delete(
self.four_tracks) self.four_tracks)
tracks = self.spotify.current_user_saved_tracks() tracks = self.spotify.current_user_saved_tracks()
new_total = tracks['total'] new_total = tracks['total']
self.assertEquals(new_total, total) self.assertEqual(new_total, total)
def test_current_user_saved_albums(self): def test_current_user_saved_albums(self):
# Add # Add
@ -212,12 +212,12 @@ class SpotipyLibraryApiTests(unittest.TestCase):
# Contains # Contains
resp = self.spotify.current_user_saved_albums_contains(self.album_ids) resp = self.spotify.current_user_saved_albums_contains(self.album_ids)
self.assertEquals(resp, [True, True]) self.assertEqual(resp, [True, True])
# Remove # Remove
self.spotify.current_user_saved_albums_delete(self.album_ids) self.spotify.current_user_saved_albums_delete(self.album_ids)
resp = self.spotify.current_user_saved_albums_contains(self.album_ids) resp = self.spotify.current_user_saved_albums_contains(self.album_ids)
self.assertEquals(resp, [False, False]) self.assertEqual(resp, [False, False])
class SpotipyUserApiTests(unittest.TestCase): class SpotipyUserApiTests(unittest.TestCase):
@ -244,11 +244,11 @@ class SpotipyUserApiTests(unittest.TestCase):
def test_basic_user_profile(self): def test_basic_user_profile(self):
user = self.spotify.user(self.username) user = self.spotify.user(self.username)
self.assertEquals(user['id'], self.username.lower()) self.assertEqual(user['id'], self.username.lower())
def test_current_user(self): def test_current_user(self):
user = self.spotify.current_user() user = self.spotify.current_user()
self.assertEquals(user['id'], self.username.lower()) self.assertEqual(user['id'], self.username.lower())
def test_me(self): def test_me(self):
user = self.spotify.me() user = self.spotify.me()
@ -373,7 +373,7 @@ class SpotipyPlayerApiTests(unittest.TestCase):
def test_devices(self): def test_devices(self):
# No devices playing by default # No devices playing by default
res = self.spotify.devices() res = self.spotify.devices()
self.assertEquals(len(res["devices"]), 0) self.assertEqual(len(res["devices"]), 0)
def test_current_user_recently_played(self): def test_current_user_recently_played(self):
# No cursor # No cursor