Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active August 12, 2025 20:31
Show Gist options
  • Save TheMuellenator/c84616c21f0f9ce68c12c357d3e1c794 to your computer and use it in GitHub Desktop.
Save TheMuellenator/c84616c21f0f9ce68c12c357d3e1c794 to your computer and use it in GitHub Desktop.
sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
scope="playlist-modify-private",
redirect_uri="http://example.com",
client_id=YOUR UNIQUE CLIENT ID,
client_secret= YOUR UNIQUE CLIENT SECRET,
show_dialog=True,
cache_path="token.txt"
)
)
user_id = sp.current_user()["id"]
date = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD: ")
song_uris = ["The list of", "song URIs", "you got by", "searching Spotify"]
playlist = sp.user_playlist_create(user=user_id, name=f"{date} Billboard 100", public=False)
# print(playlist)
sp.playlist_add_items(playlist_id=playlist["id"], items=song_uris)
@entfy
Copy link

entfy commented Aug 12, 2025

import requests, spotipy, os
from dotenv import load_dotenv
from spotipy.oauth2 import SpotifyOAuth


load_dotenv()

class SpotifyManager:

    def __init__(self):
        self._user=os.environ["SPOTIFY_CLIENT_ID"]
        self._password=os.environ["SPOTIFY_CLIENT_SECRET"]
        self._sp = self._create_object()
        self._user_id = self._get_user_id()
        self.song_names = []
        self.song_artists = []
        self.track_uris = []
        self.results = []
        
        
    def _create_object(self):
        return spotipy.Spotify(
            auth_manager=SpotifyOAuth(
                scope="playlist-modify-private",
                redirect_uri="https://example.com",
                client_id=self._user,
                client_secret=self._password,
                cache_path="token.txt",
                username=""
            )
        )
        
    
    def _get_user_id(self):
        
        return self._sp.current_user()['id']
    
    
    def create_top100_playlist(self, date):
        
        self.get_track_uris(self.song_names)
        self.add_tracks_playlist(self.create_playlist(date))
        
    
    def create_playlist(self, date):
        
        new_playlist = self._sp.user_playlist_create(
            user=self._user_id,
            name=f"Top 100 on {date}",
            public=False,
            description=f"Billboard Top 100 Playlist from {date}"
        )
        
        return new_playlist['id']
        
    def get_track_uris(self, songs):
        
        for song in songs:
            result = self._sp.search(q=song, type='track', limit=10)
            self.results.append(result)
            
            try:
                uri = result['tracks']['items'][0]['uri']
                self.track_uris.append(uri)
            except IndexError:
                print(f"{song} doesn't exist in Spotify. Skipped")
                
                
    def add_tracks_playlist(self, playlist_id):
        
        self._sp.user_playlist_add_tracks(user=self._user_id, playlist_id=playlist_id, tracks=self.track_uris)
import requests, spotipy, os
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from data_manager import SpotifyManager

load_dotenv()

spotifymanager = SpotifyManager()

date = input("Which year do you want to travel to? Type the year in this format YYYY-MM-DD: ")

URL="https://www.billboard.com/charts/hot-100/"
header = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15"
}

response = requests.get(url=f"{URL}{date}", headers=header)
soup = BeautifulSoup(response.text, "html.parser")

song_names_spans = soup.select("li ul li h3")
spotifymanager.song_names = [song.getText().strip() for song in song_names_spans]

song_artist_spans = soup.select("li ul li h3+span")
spotifymanager.song_artists = [artist.getText().strip() for artist in song_artist_spans]

spotifymanager.create_top100_playlist(date)

It took a bit because the documentation isn't the greatest for Spotipy, but after doing everything based on Spotify API, I used the Spotipy package and got it solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment