From a37ac22e6c74c41a6b88e26ef55b0654432debb5 Mon Sep 17 00:00:00 2001 From: ndm13 Date: Mon, 2 Jan 2023 15:09:16 -0500 Subject: [PATCH 1/3] Add code from pylast #407 to readme Describe authentication with OAuth token --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index baa3cb3..8676805 100644 --- a/README.md +++ b/README.md @@ -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() From 8647cbdd4844dd80ab9762fc8e18c0cf225b2cc9 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 5 Jan 2023 18:36:38 +0200 Subject: [PATCH 2/3] Make alternative clearer via own code blocks --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8676805..4c624ba 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,12 @@ network = pylast.LastFMNetwork( username=username, password_hash=password_hash, ) +``` -# You can also authenticate with a session key +Alternatively, instead of creating `network` with a username and password, +you can authenticate with a session key: + +```python 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): @@ -110,7 +114,11 @@ 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 track = network.get_track("Iron Maiden", "The Nomad") track.love() @@ -120,6 +128,7 @@ track.add_tags(("awesome", "favorite")) # to get more help about anything and see examples of how it works ``` + More examples in hugovk/lastfm-tools and [tests/](https://github.com/pylast/pylast/tree/main/tests). From f5ea06c6c9488b0f6ffeee52374f197f7a5d7224 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 6 Jan 2023 09:49:52 +0200 Subject: [PATCH 3/3] Include "import pylast" in both blocks --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4c624ba..0739ee7 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,8 @@ 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):