Add code from pylast #407 to readme

Describe authentication with OAuth token
This commit is contained in:
ndm13 2023-01-02 15:09:16 -05:00 committed by GitHub
parent 219be9f61a
commit a37ac22e6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -85,6 +85,32 @@ network = pylast.LastFMNetwork(
password_hash=password_hash,
)
# You can also authenticate with a session key
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
# Now you can use that object everywhere
track = network.get_track("Iron Maiden", "The Nomad")
track.love()