Close

Writing getSessionKey.py

A project log for SpinLast

Automatically scrobble your favorite radio tracks from Spinitron.com to last.fm

mx-jack-nelsonMx. Jack Nelson 04/26/2023 at 16:141 Comment

The app getSessionKey.py is a Python script written by my lovely wife, Steph. She has kindly donated this script to my project, as getting the Last.fm Session Key is the most complicated step of the entire process - now simplified. The script's purpose is to generate a session key for the Last.fm API, authorize the app to access the user's Last.fm profile, and then use the session key to scrobble a specific song to the user's Last.fm account to check that it is working.

Note: The script contains placeholders for the user's API key (`YOUR_API_KEY`) and API secret (`YOUR_API_SECRET`), which need to be replaced with the user's actual values before running the script. Sign up for a Last.fm API account to acquire your API key and API shared secret.

Here's a summary of the script's main steps:

1. The script defines various constants and variables, including the Last.fm API endpoints, the user's API key and secret, and other configuration options.

2. The script generates an API signature (`API_TK_SIG`) using the user's API key and secret, and then sends an HTTP GET request to the Last.fm API to obtain a temporary token (`TOKEN`) using the `auth.getToken` method.

3. The user is prompted to visit a URL to authorize the app to access their Last.fm profile. After the user grants access, they are instructed to press any key to continue.

4. The script generates another API signature (`API_SES_SIG`) using the temporary token, and then sends another HTTP GET request to the Last.fm API to obtain a permanent session key (`SESSION_KEY`) using the `auth.getSession` method.

5. The script prints the session key to the console.

6. The script prepares to scrobble the song "Never Gonna Give You Up" by Rick Astley. It generates a timestamp (`TS`) and an API signature (`API_SIG`) using the session key, artist name, track name, and other parameters.

7. The script sends an HTTP POST request to the Last.fm API to scrobble the song using the `track.scrobble` method. The request includes the necessary data, such as the API key, API signature, artist name, track name, session key, and timestamp.

8. The script prints the XML response from the Last.fm API to the console.

Discussions

opera2390 wrote 05/25/2024 at 11:49 point

Thanks!

  Are you sure? yes | no