Non commercial radio stations use Spinitron to publish their live playlists, and I wanted to be able to scrobble those songs to my Last.fm account. Now I can, thanks to an afternoon with ChatGPT, who helped me build this little web app that provides a convenient way for anyone to automatically log their favorite radio station's tracks to their Last.fm account.
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.
This CSS code styles a webpage with a white background, a container with a maximum width of 600px, a box-shadow effect, and padding. It also centers headings and has styles for messages and links. The code also includes styles for input fields and buttons, including a hover effect for buttons. The container also has a "Go back" button styled, which gets bigger on hovering. The button-container is designed to live within the container and is displayed as a flex element.
Upon opening the index.html page, users are greeted with a centered heading that reads "SpinLast" and a subheading reading "The Spinitron Scrobbler.": Below the heading, there is a form that prompts users to enter the URL of their desired Spinitron radio station. The form consists of a label, an input text field, and a submit button.
The label instructs users to "Enter Spinitron Radio Station URL," and the input text field is where users can type or paste the URL. The input field is styled to be 100% wide, with padding and rounded corners for a pleasant visual appearance. The submit button is styled with a blue background and white text, and it changes to a darker shade of blue when hovered over.
Once users enter the URL and click the "Submit" button, the form sends a POST request to the "/submit" endpoint, triggering the scrobbling process.
The overall design of the page is clean and straightforward, with a light background and a centered container that holds the form. The container has a white background and a subtle box shadow for a modern look. The font used is Arial, and the page is responsive, thanks to the "viewport" meta tag.
This Python script is designed to automatically scrobble the tracks played on a Spinitron radio station to a user's Last.fm account. It uses the Flask web framework to create a simple web application that allows users to input the URL of a Spinitron radio station. The script then periodically checks the Spinitron page for the currently playing track and scrobbles it to Last.fm if it's a new track.
Here's how the script works:
The script starts by importing the necessary libraries, including threading, time, schedule, pylast, requests, BeautifulSoup, and Flask.
The user's Last.fm API credentials (API key, API secret, and session key) are defined, and a Last.fm API client (network) is set up using the pylast library.
The Flask web application is created with the app variable.
Two global variables, last_scrobbled_track and spinitron_url, are defined to keep track of the most recently scrobbled track and the URL of the Spinitron radio station, respectively.
An event object (stop_event) is created to control the scheduler thread.
The run_scheduler function is defined to run the scheduled tasks in a separate thread. It uses the stop_event to stop the scheduler thread when needed.
The scrobble_new_track function is defined to check the Spinitron page for the currently playing track. It fetches the HTML content of the Spinitron page, extracts the track information (artist, song, and release), and compares it to the last scrobbled track. If the current track is different, it scrobbles the track to Last.fm using the network.scrobble method.
The index route renders the HTML page with a form for users to input the Spinitron URL.
The submit route is triggered when the user submits the form. It extracts the Spinitron URL from the form data, performs an initial scrobble, and returns a message indicating the result.
The latest_scrobble route returns the most recently scrobbled track in JSON format.
The scrobble_new_track function is scheduled to run every minute using the schedule library.
The scheduler thread is started, and the Flask web application is launched.
If the Flask application is terminated (e.g., by pressing Ctrl+C), the scheduler thread is signaled to stop, and the script gracefully shuts down.