Merge pull request #421 from ndm13/patch-1
Document how to authenticate with a session key
This commit is contained in:
commit
7f07babdf4
37
README.md
37
README.md
|
@ -84,7 +84,43 @@ network = pylast.LastFMNetwork(
|
||||||
username=username,
|
username=username,
|
||||||
password_hash=password_hash,
|
password_hash=password_hash,
|
||||||
)
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, instead of creating `network` with a username and password,
|
||||||
|
you can authenticate with a session key:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import pylast
|
||||||
|
|
||||||
|
SESSION_KEY_FILE = os.path.join(os.path.expanduser("~"), ".session_key")
|
||||||
|
network = pylast.LastFMNetwork(API_KEY, API_SECRET)
|
||||||
|
if not os.path.exists(SESSION_KEY_FILE):
|
||||||
|
skg = pylast.SessionKeyGenerator(network)
|
||||||
|
url = skg.get_web_auth_url()
|
||||||
|
|
||||||
|
print(f"Please authorize this script to access your account: {url}\n")
|
||||||
|
import time
|
||||||
|
import webbrowser
|
||||||
|
|
||||||
|
webbrowser.open(url)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
session_key = skg.get_web_auth_session_key(url)
|
||||||
|
with open(SESSION_KEY_FILE, "w") as f:
|
||||||
|
f.write(session_key)
|
||||||
|
break
|
||||||
|
except pylast.WSError:
|
||||||
|
time.sleep(1)
|
||||||
|
else:
|
||||||
|
session_key = open(SESSION_KEY_FILE).read()
|
||||||
|
|
||||||
|
network.session_key = session_key
|
||||||
|
```
|
||||||
|
|
||||||
|
And away we go:
|
||||||
|
|
||||||
|
```python
|
||||||
# Now you can use that object everywhere
|
# Now you can use that object everywhere
|
||||||
track = network.get_track("Iron Maiden", "The Nomad")
|
track = network.get_track("Iron Maiden", "The Nomad")
|
||||||
track.love()
|
track.love()
|
||||||
|
@ -94,6 +130,7 @@ track.add_tags(("awesome", "favorite"))
|
||||||
# to get more help about anything and see examples of how it works
|
# to get more help about anything and see examples of how it works
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
More examples in
|
More examples in
|
||||||
<a href="https://github.com/hugovk/lastfm-tools">hugovk/lastfm-tools</a> and
|
<a href="https://github.com/hugovk/lastfm-tools">hugovk/lastfm-tools</a> and
|
||||||
[tests/](https://github.com/pylast/pylast/tree/main/tests).
|
[tests/](https://github.com/pylast/pylast/tree/main/tests).
|
||||||
|
|
Loading…
Reference in a new issue