This application listens for webhook events from your Plex Media Server and updates your Last.fm "Now Playing" status in real-time. The built in Plex scrobbler only scrobbles after the track is over, and some other attempts seem to time out halfway through a song. This should fix both of those problems.
The application is configured using environment variables. You will need to gather the following information:
LASTFM_API_KEY
: Your Last.fm API Key. You can get one from Last.fm API Accounts.LASTFM_API_SECRET
: Your Last.fm API Shared Secret (you'll get it with the API_KEY).
You can run this application using Docker (amd64 or arm64) or as a systemd service on a Linux system.
There is a guided set up upon first start. You can access it by going to http://your-instance:8000/setup.
-
It will use your API_KEY and API_SECRET to get an auth url.
-
You need to click that link and accept the application in last.fm.
-
Once you've done that, go back to this site and it will ask you for your last.fm username; submit.
-
(This part is invisible to you - it happens behind the scenes) Once you complete that, it grabs a session key and stores it for you in a .env file in the volume specified in your docker compose file or in the path
$currentWorkingDirectory/lastfm-data/.env
NOTE: This file must be writeable on the host from Docker or your user running via systemd. -
From now on (even after restarts), it will use your session key to authenticate you in a way that allows you to scrobble (the API_KEY and API_SECRET alone will not give you write permissions)
Prerequisites:
- Docker installed.
- Docker Compose installed.
Steps:
-
Create a
docker-compose.yml
file: Create a file nameddocker-compose.yml
with the following content: Note: The application stores Last.fm credentials and session data in a persistent volume. By default, data is stored in/opt/homelab/traefik/lastfm-data
on the host.services: plex-lastfm-now-playing: image: ghcr.io/soehlert/plex_lastfm_now_playing:latest container_name: plex_lastfm_now_playing restart: unless-stopped ports: - "8000:8000" volumes: - ${LASTFM_DATA_PATH:-/opt/homelab/traefik/lastfm-data}:/app/lastfm-data # Set up to read env vars from portainer environment: - LASTFM_API_KEY=${LASTFM_API_KEY} - LASTFM_API_SECRET=${LASTFM_API_SECRET} # Optionally use an env file instead of vars in portainer # env_file: # - .env
Optional env_file:
LASTFM_DATA_PATH=path on the host of your .env file LASTFM_API_KEY=your_lastfm_api_key_here LASTFM_API_SECRET=your_lastfm_api_secret_here # Optional as we will gather this during set up if it does not exist (you likely don't want to set these on your own) LASTFM_USERNAME=your_lastfm_username_here LASTFM_SESSION_KEY=your_session_key # Optional to override defaults UPDATE_INTERVAL_SECONDS=some_sort_of_integer_seconds PAUSE_TIMEOUT_SECONDS=some_sort_of_integer_seconds APP_PORT=8000 LOG_LEVEL="INFO"
-
Start the application: Open a terminal in the directory where you created the docker-compose.yml file and run:
docker-compose up -d
This method involves installing the application directly on your Linux system and running it under systemd.
Prerequisites:
- Python 3.12+
- uv - installer.
- git
- A dedicated user to run the application (recommended for security, e.g.,
scrobbler
).
Steps:
-
Clone the repository:
git clone https://github.com/soehlert/plex_lastfm_now_playing /usr/local/plex_lastfm_now_playing chown scrobbler:scrobbler /usr/local/plex_lastfm_now_playing cd /usr/local/plex_lastfm_now_playing
-
Create a virtual environment
uv venv
-
Activate the virtual environment
source .venv/bin/activate
-
Install the application and its dependencies using uv
uv pip install -e .
-
Configure Environment Variables for systemd: Create an environment file for the service. For example, at /etc/plex_lastfm_now_playing/config.env:
sudo mkdir -p /etc/plex_lastfm_now_playing sudo nano /etc/plex_lastfm_now_playing/config.env
-
Add your configuration from the previous envfile example to this file:
-
Secure the file:
sudo chown scrobbler:scrobbler /etc/plex_lastfm_now_playing/config.env sudo chmod 600 /etc/plex_lastfm_now_playing/config.env
-
Create the systemd Service File: Create a file named plex-lastfm-now-playing.service in /etc/systemd/system/
[Unit] Description=Plex Last.fm Now Playing Scrobbler After=network.target [Service] User=scrobbler Group=scrobbler WorkingDirectory=/usr/local/plex_lastfm_now_playing EnvironmentFile=/etc/plex_lastfm_now_playing/config.env ExecStart=/usr/local/plex_lastfm_now_playing/.venv/bin/fastapi run src/plex_lastfm_now_playing/main.py --proxy-headers --port 8000 Restart=always RestartSec=3 [Install] WantedBy=multi-user.target
-
Pick up the new service and start it:
sudo systemctl daemon-reload sudo systemctl enable plex-lastfm-now-playing.service sudo systemctl start plex-lastfm-now-playing.service
Once the application is running (either via Docker or systemd), you need to configure Plex to send webhooks to it.
- In Plex, go to Settings > Webhooks.
- Click Add Webhook.
- Enter the URL where your plex-lastfm-now-playing application is listening. Eg:
http://10.10.10.10:8000/webhook
- Save the webhook.
Plex will now send notifications to your application when media playback starts, resumes, stops, or pauses (among others - you can ignore them).