Songs from a DJ set into Last.fm

Scrape 1001tracklists to extracts a track list from a DJ set. Then, send it over to Last.fm using their API. #python

Prerequisites

pip3 install pylast requests fake-useragent beautifulsoup4

First generate a Last.fm API key and fill in lines 7-10 with your API and account details. Afterwards, call the script with:

python scrape.py https://www.1001tracklists.com/tracklist/td181gt/rl-grime-jawns-rossy-sable-valley-stream001-united-states-2020-05-30.html

Script

#!/usr/bin/python3
import pylast, requests, sys, time
from fake_useragent import UserAgent
from bs4 import BeautifulSoup

LASTFM_API_KEY = "<your Last.fm API key goes here>"
LASTFM_API_SECRET = "<your Last.fm API secret goes here>"
LASTFM_USERNAME = "<your Last.fm username goes here>"
LASTFM_PASSWORD = pylast.md5("<your Last.fm password goes here>")

url = str(sys.argv[1])
print("Scrobbling from " + url )
network = pylast.LastFMNetwork(api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET, username=LASTFM_USERNAME, password_hash=LASTFM_PASSWORD)

r = requests.get(url, headers={'User-Agent': UserAgent().firefox })
soup = BeautifulSoup(r.text, features="lxml")

for track in soup.find_all('span', class_="trackFormat"):
  full_text = track.text.split(" - ")
  artist = full_text[0][1:]
  track_name = full_text[1][:-1]
  if artist != "ID" and track_name != "ID" and "ID Remix" not in track_name:
    last_api_call = network.scrobble(artist, track_name, int(time.time()))
    print ("Scrobbled: " + artist + " - " + track_name)

Unless specified otherwise, this work is licensed under a Creative Commons BY-NC-SA 4.0.