-
-
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) |
#Here Is The Complete Project In Simplest Way
from bs4 import BeautifulSoup
import requests
import spotipy
from spotipy.oauth2 import SpotifyOAuth
#date=input("Please Enter date in YYYY-MM- Format")
response=requests.get("https://www.billboard.com/charts/hot-100/2023-08-05/")
soup=BeautifulSoup(response.text,"html.parser")
all_titles= soup.select(selector=".o-chart-results-list__item #title-of-a-story")
client_id="05d82555b91a4325b5e1e5563e4b0faa"
client_secret="5c3d0bc4a04d47b38935ee977742c379"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id,
client_secret=client_secret,
redirect_uri='https://example.com',
scope='playlist-modify-private'))
user_profile = sp.current_user()
Extract user ID from the profile
user_id = user_profile['id']
print(user_id)
playlist_name = 'Top 100 Songs'
playlist_description = 'Your Playlist Description'
playlist = sp.user_playlist_create(user=user_id, name=playlist_name, public=False, description=playlist_description)
for titles in all_titles:
print(titles.getText().strip())
# Search for the song
song_title = titles.getText().strip()
search_results = sp.search(q=song_title, type='track')
# Extract the track URI
track_uri = search_results['tracks']['items'][0]['uri'] # Assuming the first search result is the desired song
playlist_id = playlist['id']
sp.playlist_add_items(playlist_id=playlist_id, items=[track_uri])
Here is my code, this one is definitely challenging because the documentation is not explained very well (at least for me) so it requires trials and lots of errors. Worked it all out in the end. Hope this helps
from bs4 import BeautifulSoup
import requests
import spotipy
from spotipy.oauth2 import SpotifyOAuth
time_input = input("Enter the time you want to jump to in this format YYYY-MM-DD: ")
URL = "https://www.billboard.com/charts/hot-100/"+time_input
TEMP1 = "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"
TEMP2 = "c-title a-no-trucate a-font-primary-bold-s u-letter-spacing-0021 u-font-size-23@tablet lrv-u-font-size-16 u-line-height-125 u-line-height-normal@mobile-max a-truncate-ellipsis u-max-width-245 u-max-width-230@tablet-only u-letter-spacing-0028@tablet"
TEMP3 = "c-label a-no-trucate a-font-primary-s lrv-u-font-size-14@mobile-max u-line-height-normal@mobile-max u-letter-spacing-0021 lrv-u-display-block a-truncate-ellipsis-2line u-max-width-330 u-max-width-230@tablet-only"
TEMP4 = "c-label a-no-trucate a-font-primary-s lrv-u-font-size-14@mobile-max u-line-height-normal@mobile-max u-letter-spacing-0021 lrv-u-display-block a-truncate-ellipsis-2line u-max-width-330 u-max-width-230@tablet-only u-font-size-20@tablet"
# These are classes scrapped from the billboard website
CLIENT_ID = (your client_id here)
CLIENT_SECRET = (your client_secret here)
REDIRECT_URI = "http://example.com"
SPOTIFY_URL = "https://api.spotify.com/v1"
USERNAME = (your username here)
# ------------- SCRAPE BILLBOARD AND GET 2 LISTS OF SONGS AND CORRESPONDING ARTISTS -------------
response = requests.get(URL)
data = response.text
soup = BeautifulSoup(data, "html.parser")
songs = [item.getText().strip() for item in soup.find_all(name="h3", id="title-of-a-story", class_=TEMP1)]
first_song = soup.find(name="h3", id="title-of-a-story", class_=TEMP2).getText().strip()
songs.insert(0, first_song)
artists = [item.getText().strip() for item in soup.find_all(name="span", class_=TEMP3)]
first_artist = soup.find(name="span", class_=TEMP4).getText().strip()
artists.insert(0, first_artist)
# ------------- ACCESS SPOTIFY AND MAKE AUTH -------------
scope = "playlist-modify-private"
sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
cache_path="token.txt",
scope=scope,
show_dialog=True,
username=USERNAME,
)
)
user_id = sp.current_user()["id"]
# ------------- CREATE PLAYLIST -------------
playlist = sp.user_playlist_create(
user=user_id,
name="Top 100 Billboard songs",
public=False,
collaborative=False,
description=f"Top 100 Billboard songs on {time_input}",
)
playlist_id = playlist["id"]
# ------------- SEARCH FOR SONGS -------------
class NoSongFound(Exception):
pass
uris = []
for index in range(0, len(songs)):
try:
search = sp.search(
q=f"track:{songs[index]} artist:{artists[index]}",
limit=1,
offset=0,
market=None,
type="track",
)
if search["tracks"]["total"] == 0:
raise NoSongFound
except NoSongFound:
pass
else:
uri = str(search["tracks"]["items"][0]["id"])
uris.append(uri)
# ------------- ADD SONGS -------------
sp.playlist_add_items(playlist_id=playlist_id, items=uris)
print(sp.playlist_items(
playlist_id=playlist_id,
additional_types="track",
))
Hey guys, I keep getting an insufficient client scope error. What am I still missing in the code?
Also, when I run Angela's code it creates the playlist but can't add any songs. I am a bit lost there lol.
Hey guys, I keep getting an insufficient client scope error. What am I still missing in the code?
Also, when I run Angela's code it creates the playlist but can't add any songs. I am a bit lost there lol.
@bluebanana18 I was having the exact same issue.
The solution for me was updating the scope in the Spotify Authentication section to include both private and public playlist-modify scopes. (update to line 3 of Angela's code)
Yes, this seems counter intuitive since on line 15 of the example, the created playlist is set as private and has the playlist-modify-private scope set, so you think it would work. But looks like the Spotify API wants both scopes set.
Also you may need to delete your token.txt file before running in order for the token to reset properly.
Example:
sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
scope="playlist-modify-private playlist-modify-public",
redirect_uri="http://example.com",
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
show_dialog=True,
cache_path="token.txt"
)
)
Hope this helps
@drakewilcox
Mind blown. It was really that simple, huh. Thanks a lot!
Here is my code, it is working perfectly, I had to have a little bit of hand holding from chat gpt
import requests
from bs4 import BeautifulSoup
import spotipy
from spotipy.oauth2 import SpotifyOAuth
Set your Spotify app credentials
You will need to make a spotify web app to get this data, go to spotify dev tools
SPOTIPY_CLIENT_ID = 'YOUR CLIENT ID'
SPOTIPY_CLIENT_SECRET = 'YOUR CLIENT SECRET'
SPOTIPY_REDIRECT_URI = 'YOUR REDIRECT URI'
SCOPE = 'playlist-modify-public user-read-private'
date = input("What date would you like to travel back in time to? YYYY-MM-DD")
endpoint = f"https://www.billboard.com/charts/hot-100/{date}/"
Scrape web for top 100 songs
response = requests.get(endpoint)
html_data = response.text
soup = BeautifulSoup(html_data, "html.parser")
songs = soup.select("li ul li h3")
songs_titles = [song.getText().strip() for song in songs]
track_uris = []
Authenticate with Spotify
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
scope=SCOPE))
Find track URIs
for song in songs_titles:
try:
results = sp.search(q=f"track:{song}", type="track")
track_uri = results['tracks']['items'][0]['uri']
track_uris.append(track_uri)
except IndexError:
continue
# Authenticate with Spotify
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
scope=SCOPE))
Get current user's profile data
user_id = sp.current_user()['id']
Create a new playlist for the current user
playlist_name = f"Billboard top songs on {date}"
playlist_description = "Created with Python"
playlist = sp.user_playlist_create(user=user_id, name=playlist_name, description=playlist_description)
Get the playlist ID
playlist_id = playlist['id']
Add tracks to the playlist
sp.playlist_add_items(playlist_id=playlist_id, items=track_uris)
print(f"Playlist created and tracks added. Playlist ID: {playlist_id}")
# Use this return to check if our OAuth is successful. # return 'OAuth Successful' sp = spotipy.Spotify(auth=token_info['access_token']) user_id = sp.current_user()['id'] current_playlists = sp.current_user_playlists()['items'] song_uris = [] # Song search to add in playlist for song in song_names: result = sp.search(q=f"track:{song}", type="track", market="US") # print track: find the # print(result['tracks']['items'][0]) try: uri = result["tracks"]["items"][0]["uri"] song_uris.append(uri) except IndexError: print(f"{song} doesn't exist in Spotify. Skipped.") # print(song_uris) # Check if the playlist already exist for given date: billboard_100_playlist_id = None for playlist in current_playlists: if playlist['name'] == f'{date} Billboard 100': billboard_100_playlist_id = playlist['id'] if not billboard_100_playlist_id: new_playlist = sp.user_playlist_create(user_id, f'{date} Billboard 100', True) billboard_100_playlist_id = new_playlist['id'] # return f"Billboard top 100 playlist id for date {date} is not found."
Awesome work. However, I am having trouble carrying out the playlist check. I cannot retrieve any information running the line below immediately after the authentication steps. I can print out user_id = sp.current_user()["id"] without any problem.
Can you shed some light on what should i do?
current_playlists = sp.current_user_playlists()['items']
Is anyone else finding this project challenging? I'm having trouble grasping the concepts involved. Any help or clarification would be greatly appreciated