Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Scrape BBC Sounds and update a spotify playlist with the tracks found. Requires spotipy library.
import requests
from bs4 import BeautifulSoup
import json
import spotipy
import spotipy.util
Various constants -- the secrets should be moved to a .env file. SPOTIFY_SCOPES too broad, should be reduced.
SPOTIFY_SCOPES = 'user-read-recently-played user-top-read user-library-modify user-library-read playlist-read-private playlist-modify-public playlist-modify-private playlist-read-collaborative user-read-email user-read-birthdate user-read-private user-read-playback-state user-modify-playback-state user-read-currently-playing app-remote-control streaming user-follow-read user-follow-modify'
SPOTIFY_REDIRECT_URL = 'https://localhost/'
SPOTIFY_PLAYLIST_ID = '20yFxgsxEx8x7C3zMwddR3'
Get a token if needed
token = spotipy.util.prompt_for_user_token(SPOTIFY_USERNAME, scope=SPOTIFY_SCOPES, client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET, redirect_uri=SPOTIFY_REDIRECT_URL)
sp = spotipy.Spotify(auth=token)
html = requests.get(PLAYLIST_URL).text
print("[+] Got playlist page HTML")
soup = BeautifulSoup(html, 'html.parser')
#tracklist_html = soup.findAll('li', attrs={'class': 'sc-c-tracklist__track'})
tracklist_html = soup.findAll('div', attrs={'class': 'sc-c-basic-tile__text'})
print("[+] Parsed HTML")
track_ids = []
for n, track_html in enumerate(tracklist_html):
artist = track_html.find('p', attrs={'class': 'sc-c-basic-tile__artist'}).text
track_name = track_html.find('p', attrs={'class': 'sc-c-basic-tile__title'}).text
result ="{track_name} - {artist}", type='track', limit=1)
if len(result['tracks']['items']) > 0:
track_id = result['tracks']['items'][0]['id']
print(f"[+] Found track: {track_name} - {artist} ({track_id})")
print(f"[-] Couldn't find track: {track_name} - {artist}")
print("[i] Adding Tracks!")
sp.user_playlist_replace_tracks(SPOTIFY_USERNAME, SPOTIFY_PLAYLIST_ID, track_ids)
print(f"[+] Added {len(track_ids)} tracks!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.