Added a top 100 song playlist maker
This commit is contained in:
parent
9476f242b5
commit
c59b35d9bd
BIN
Visual Studio Code Projects/Song-Time-Machine/.DS_Store
vendored
Normal file
BIN
Visual Studio Code Projects/Song-Time-Machine/.DS_Store
vendored
Normal file
Binary file not shown.
47
Visual Studio Code Projects/Song-Time-Machine/main.py
Normal file
47
Visual Studio Code Projects/Song-Time-Machine/main.py
Normal file
@ -0,0 +1,47 @@
|
||||
from bs4 import BeautifulSoup
|
||||
import requests
|
||||
import spotipy
|
||||
from spotipy.oauth2 import SpotifyOAuth
|
||||
|
||||
# Scraping Billboard 100
|
||||
date = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD: ")
|
||||
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/" + date
|
||||
response = requests.get(url=billboard_url, headers=header)
|
||||
|
||||
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]
|
||||
|
||||
# Spotify Authentication
|
||||
sp = spotipy.Spotify(
|
||||
auth_manager=SpotifyOAuth(
|
||||
scope="playlist-modify-private",
|
||||
redirect_uri="http://example.com",
|
||||
client_id=YOUR-CLIENT-ID,
|
||||
client_secret=YOUR-CLIENT-SECRET,
|
||||
show_dialog=True,
|
||||
cache_path="token.txt"
|
||||
)
|
||||
)
|
||||
user_id = sp.current_user()["id"]
|
||||
print(user_id)
|
||||
|
||||
# Searching Spotify for songs by title
|
||||
song_uris = []
|
||||
year = date.split("-")[0]
|
||||
for song in song_names:
|
||||
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} doesn't exist in Spotify. Skipped.")
|
||||
|
||||
# Creating a new private playlist in Spotify
|
||||
playlist = sp.user_playlist_create(user=user_id, name=f"{date} Billboard 100", public=False)
|
||||
print(playlist)
|
||||
|
||||
# Adding songs found into the new playlist
|
||||
sp.playlist_add_items(playlist_id=playlist["id"], items=song_uris)
|
||||
@ -0,0 +1,3 @@
|
||||
beautifulsoup4==4.12.3
|
||||
spotipy==2.24.0
|
||||
requests==2.32.3
|
||||
Loading…
Reference in New Issue
Block a user