Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active October 31, 2025 02:33
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)
@a-k-14
Copy link

a-k-14 commented Jun 29, 2025

I have added few more features:

  • get the list of playlists in a user profile
  • check if there is an empty playlist and if yes, get its 'id'
  • if no playlists exist or if no empty playlists exist, create a new playlist and get its id
  • time taken to execute the program completely

This way we can have new playlist on every run.

Note: Spotify takes some time to report the proper track count in a playlist. So, if you create a new empty playlist, add some songs to it, and immediately run the code again, the previously created playlist will be reported as empty. I think it takes roughly 15sec to get the updated count.

`

GOAL - to create a spotify playlist of top 100 songs on billboard.com on a selected day

import requests
from bs4 import BeautifulSoup
import time
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pprint

spotify_client_id = "..."
spotify_client_secret = "..."
spotify_redirect_uri = "https://example.com/"

def get_top_100_titles(selected_date) -> list:
"""
Fetches the top 100 titles for a selected date from billboard.com and returns
:return: list of top 100 titles
"""
print(f"Getting top 100 titles on {selected_date} from billboard...")

header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0"}
billboard_url = "https://www.billboard.com/charts/hot-100/" + selected_date.strip()

response = requests.get(url=billboard_url, headers=header)
response.raise_for_status()
website = response.text

soup = BeautifulSoup(website, "html.parser")

# list_items = soup.find_all(name="li", class_="o-chart-results-list__item")
list_items = soup.select("li ul li h3")

titles = [item.string.strip() for item in list_items]

# for item in list_items:
# title_h3 = item.find(name="h3", attrs={"class":"c-title"})
# if title_h3:
#     titles.append(title_h3.string.strip())
return titles

def write_data(selected_date, data: list) -> None:
"""
Writes the data to a text file
:param selected_date: user selected date
:param data: list
:return: None
"""
print("Adding the top 100 songs to the file...")

with open("top_100.txt", "w") as file:
    file.write(f"{selected_date}\n")
    for i in range(len(data)):
        file.write(f"{i + 1}. {data[i]}\n")

def humanize_time(seconds) -> None:
intervals = [("d", 86400), ("h", 3600), ("m", 60)]
parts = []
if seconds:
for label, count in intervals:
value, seconds = divmod(seconds, count)

        if value:
            parts.append(f"{value:.0f}{label}")

parts.append(f"{seconds:.02f}s")
print(" ".join(parts))

def add_to_playlist(date, titles_to_add):
print("Authenticating with Spotify...")
# steps to add the title to the user's spotify playlist
# 1. get client_id, client_secret from spotify

# 2. get authorization token from spotify using spotipy
# spotify authorization
# playlist-modify-private
# scope = "user-library-read"
scope = "playlist-modify-public"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, client_id=spotify_client_id,
                                               client_secret=spotify_client_secret, redirect_uri=spotify_redirect_uri, show_dialog=True, cache_path="token.txt", username="vgst cof"))
user_id = sp.current_user()["id"]

# 3. get an empty playlist id from spotify

# get the list of playlists in a user profile
print("Fetching list of playlists...")
playlist_search_results = sp.current_user_playlists()["items"]

# check if there are any empty playlists and get its id
empty_playlist_id = None
if playlist_search_results:
    print(f"{len(playlist_search_results)} playlists exist")
    # there are playlists in the user profile
    for playlist in playlist_search_results:
        no_of_tracks = int(playlist["tracks"]["total"])
        if not no_of_tracks:
            print(no_of_tracks)
            empty_playlist_id =  playlist["id"]
            empty_playlist_name = playlist["name"]
            print(f"Empty playlist name: {empty_playlist_name}")
            break

if not playlist_search_results or not empty_playlist_id:
    print("No playlists or no empty playlists exist, creating a new playlist...")
    # if there are no playlists or if there are no empty playlists, create a new playlist
    new_empty_playlist = sp.user_playlist_create(user=user_id, name=f"Top 100 {date}")
    empty_playlist_id = new_empty_playlist["id"]
    empty_playlist_name = new_empty_playlist["name"]
    print(f"New empty playlist name: {empty_playlist_name}")


# 4. get the id of the title
print("Adding tracks to the the playlist", end="")
titles_with_ids = []
for title in titles_to_add:
    print(".", end="")
    search_results = sp.search(q="track:" + title, type="track")["tracks"]["items"]
    if search_results:
        # if the title exists in spotify db
        title_id = search_results[0]["id"]
        titles_with_ids.append(title_id)
    else:
        print(title, "does not exist in Spotify. Skipped.")

# 5. add the title to the user's playlist
sp.playlist_add_items(playlist_id=empty_playlist_id, items=titles_with_ids)

#--------------------#--------------------#

get the date from the user

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

to track the time for program execution

start_t = time.time()

get the top 100 songs/titles for the selected date

top_100_titles = get_top_100_titles(user_chosen_date)

if top_100_titles:
# write the titles to a text file
write_data(user_chosen_date, top_100_titles)
# add tracks to the user's playlist
add_to_playlist(user_chosen_date, top_100_titles)

end_t = time.time()

humanize_time(end_t - start_t)

`

@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.

@rajuashokk-git
Copy link

Hello Developers,

import requests
from bs4 import BeautifulSoup
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json

Note: Set Up a Spotify Developer Account
Go to the Spotify Developer Dashboard.
Log in and create a new app.
Note down the Client ID and Client Secret.
Set the Redirect URI (e.g., http://localhost:8888/callback or https://example.com).

Constants

CLIENT_ID = "YOUR_CLIENT_ID"
CLIENT_SECRET = "YOUR_CLIENT_SECRET"
REDIRECT_URI = "https://example.com" # I used this.
SPOTIFY_SCOPE = "playlist-modify-private"

date = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD: ")
URL = f"https://billboard.com/charts/hot-100/{date}/" # Dynamic URL

URL = "https://www.billboard.com/charts/hot-100/2000-08-12" # Constant URL for the testing

header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0"}

response = requests.get(url = URL, headers=header)
website_data = response.text
soup = BeautifulSoup(website_data, "html.parser")

Get the Song name of top 100 list

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

print(song_names)

Authenticate

sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
scope=SPOTIFY_SCOPE))

user-library-read

user_id = sp.current_user()["id"]
print(user_id)

Create Playlist

playlist = sp.user_playlist_create(user=user_id, name=f"{date} Billboard 100", public=False, description=f"{date} Billboard 100")
print(playlist)

Get the song uri to add into the playlist tracks

song_uris = []
for song in song_names:
result = sp.search(q=song) # type = dictionary to get data.
try:
# print(json.dumps(result, sort_keys=4, indent=4)) # this is to identify the song url
uri = result["tracks"]["items"][0]["uri"]
song_uris.append(uri)
except IndexError:
print(f"{song} doesn't exist in Spotify. Skipped.")

Add songs into playlist

sp.playlist_add_items(playlist_id=playlist['id'], items=song_uris)
print("Tracks added to the playlist!")

@busan-bae
Copy link

It feels too difficult for me right now. :(
I find it hard to fully understand the Spotify library documentation.
However, I will try to follow the example code you provided, and later I will give it another attempt.

@kingkupo1985
Copy link

kingkupo1985 commented Oct 9, 2025 via email

@rajuashokk-git
Copy link

Yes, the documentation is vast, and it's not readily available to understand the concepts.
Please post your question on the code I posted. I can try to clarify the same to you.
Thanks!

@ChaVarilla
Copy link

ChaVarilla commented Oct 17, 2025

I'm so happy that I was able to finish this project. There might be some problems on the solution due to some system update but glad that I was able to solve it through the help of the people here. Thank you so much.

The solutions I did are:

  1. The redirect URI should be "https://open.spotify.com/" not "'https://example.com/", and should also match the redirect URI on your Dashboard App URI
  2. I changed the scope from "playlist-modify-private" to "playlist-modify-private playlist-modify-public". There should be two scopes added.
  3. When the "Enter the URL you are directed:" pop up on your console, copy the whole URL to which you are directed. It won't appear the second time you run your code again.

Here's my code. Hope it helps:

import export
import requests
from bs4 import BeautifulSoup
import spotipy
from spotipy.oauth2 import SpotifyOAuth

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

url = f'https://www.billboard.com/charts/hot-100/{date}/'
headers = {'user-agent': 'your own user agent'}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text,'html.parser')
song_names_spans = soup.select("li ul li h3")
song_names = [song.getText().strip() for song in song_names_spans]
print(song_names)

REDIRECT_URI = "https://open.spotify.com/"

sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
cache_path="token.txt",
scope="playlist-modify-private playlist-modify-public"))

year_input = date.split("-")[0]
song_uris = []

for song in song_names:

search_requests = sp.search(q=f"track:{song} year:{year_input}")

try:
    uri = search_requests["tracks"]["items"][0]["uri"]
    song_uris.append(uri)

except IndexError:
    print(f"\n{song} does not exist in Spotify. Skipped...\n")

user_id = sp.current_user()["id"]

playlist = sp.user_playlist_create(user=user_id,
name=f"{date} Billboard 100",
public=False,
description=f"Top 100 songs on {date}")

sp.playlist_add_items(playlist_id=playlist["id"], items=song_uris)

@rajuashokk-git
Copy link

Good to hear, ChaVarilla!
Happy Learning!

@GreggRodgers02
Copy link

GreggRodgers02 commented Oct 31, 2025

As of October 2025, this code is working as attended for everyone reference

import requests
from bs4 import BeautifulSoup
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os
from dotenv import load_dotenv

load_dotenv()

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

headers = {'User-Agent': 'MY USER AGENT'}

site_html = requests.get(f'https://www.billboard.com/charts/hot-100/{user_input}/', headers=headers).text
soup = BeautifulSoup(site_html, features='html.parser')

top_100 = soup.select('li ul li h3')
top_100_list = [songs.text.strip().replace('\n', '').replace('\t', '') for songs in top_100]

#print(top_100_list)
sp = spotipy.Spotify(
    auth_manager=SpotifyOAuth(
        client_id=os.getenv('SPOTIFY_CLIENT_ID'),
        client_secret=os.getenv('SPOTIFY_CLIENT_SECRET'),
        scope='playlist-modify-private',
        cache_path='token.txt',
        redirect_uri='http://127.0.0.1:9090',
        show_dialog=True,
        username='g35k',
))
user_id = sp.current_user()["id"]

song_uris = []
year = user_input.split('-')[0]
print(year)
for song in top_100_list:
    result = sp.search(q=f'track:{song} year:{year}', type='track')
    print(result)
    try:
        uri=result['tracks']['items'][0]['uri']
        song_uris.append(uri)
    except IndexError:
        print(f'{song} not found')


playlist = sp.user_playlist_create(user=user_id, name=f'{user_input} Billboard 100', public=False)


sp.playlist_add_items(playlist_id=playlist['id'], `items=song_uris)```

I believe struggling to understand the API document is a benefit, Learning happen best when struggling lol.

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