Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active March 20, 2025 16:16
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)
@gillesvm
Copy link

gillesvm commented Feb 6, 2025

To add the tracks to the playlist I used another spotipy method, I can't find the one in the solution in the documentation. According to PyCharm that method also doesn't exist. Here is my code I used:

``

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

load_dotenv()

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

# entered a fixed date for testing purposes
# date = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD:")
date = "2022-12-12"
response = requests.get(url=f"{BILLBOARD_ENDPOINT}{date}",headers=BILLBOARD_HEADER)

soup = BeautifulSoup(response.text, "html.parser")
songs = soup.find_all(name="h3", class_="c-title a-no-trucate a-font-primary-bold-s u-letter-spacing-0021 lrv-u-font-size-18@tablet lrv-u-font-size-16 u-line-height-125 u-line-height-normal@mobile-max a-truncate-ellipsis u-max-width-330 u-max-width-230@tablet-only")
top_100_songs = []
for song in songs:
    title = song.getText().strip()
    top_100_songs.append(title)

sp = spotipy.Spotify(
    auth_manager=SpotifyOAuth(
        scope="playlist-modify-private",
        redirect_uri="http://example.com",
        client_id=os.environ["SPOTIPY_CLIENT_ID"],
        client_secret=os.environ["SPOTIPY_CLIENT_SECRET"],
        show_dialog=True,
        cache_path="token.txt",
        username="***********"
    )
)
user_id = sp.current_user()["id"]

track_uri_list = []
for song in top_100_songs:
    search = sp.search(q=song,type="track")
    try:
        track_uri_list.append(search["tracks"]["items"][0]["uri"])
    except IndexError:
        print("Track not found")

playlist_name = f"{date} Billboard 100"
playlist = sp.user_playlist_create(user_id, playlist_name, public=False, description=f"Billboard top 10 from {date}")
playlist_id = playlist["id"]
print(f"playlist created with id {playlist_id}")
sp.user_playlist_add_tracks(user=user_id, playlist_id=playlist_id, tracks=track_uri_list)
print("tracks added")

``

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