Created
September 7, 2019 08:50
-
-
Save JosephRedfern/a514b23de9fa9dd8783d4d0ed96e2d1a to your computer and use it in GitHub Desktop.
Scrape BBC Sounds and update a spotify playlist with the tracks found. Requires spotipy library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_USERNAME = "" | |
SPOTIFY_CLIENT_ID = '' | |
SPOTIFY_CLIENT_SECRET = '' | |
SPOTIFY_REDIRECT_URL = 'https://localhost/' | |
SPOTIFY_PLAYLIST_ID = '20yFxgsxEx8x7C3zMwddR3' | |
PLAYLIST_URL = 'https://www.bbc.co.uk/sounds/play/brand:p071zbkq/p07912km' | |
""" | |
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 = sp.search(f"{track_name} - {artist}", type='track', limit=1) | |
if len(result['tracks']['items']) > 0: | |
track_id = result['tracks']['items'][0]['id'] | |
track_ids.append(track_id) | |
print(f"[+] Found track: {track_name} - {artist} ({track_id})") | |
else: | |
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