1
+ from dotenv import load_dotenv
2
+ import os
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+ import spotipy
6
+ from spotipy .oauth2 import SpotifyOAuth
7
+ from pprint import pprint
8
+ load_dotenv ()
9
+
10
+ # Authentication with Spotify
11
+ sp = spotipy .Spotify (
12
+ auth_manager = SpotifyOAuth (
13
+ client_id = os .getenv ('CLIENT_ID' ),
14
+ client_secret = os .getenv ('CLIENT_SECRET' ),
15
+ redirect_uri = "http://example.com" ,
16
+ scope = "playlist-modify-private" ,
17
+ show_dialog = True ,
18
+ cache_path = "token.txt"
19
+ )
20
+ )
21
+ user_id = sp .current_user ()["id" ]
22
+
23
+ date = input ("Which year do you want to travel to? Type date in this format YYYY-MM-DD:\n " )
24
+ billboard_url = "https://www.billboard.com/charts/hot-100/" + date
25
+
26
+ response = requests .get (billboard_url ).text
27
+ soup = BeautifulSoup (response ,'html.parser' )
28
+
29
+ song_titles = soup .find_all (name = "span" , class_ = "chart-element__information__song text--truncate color--primary" )
30
+ song_names = []
31
+ for title in song_titles :
32
+ song_names .append (title .getText ())
33
+
34
+ # Search Spotify for the songs
35
+ song_uris = []
36
+ year = date .split ('-' )[0 ]
37
+ for song in song_names :
38
+ result = sp .search (q = f"track:{ song } year:{ year } " , type = "track" )
39
+ try :
40
+ uri = (result ['tracks' ]['items' ][0 ]['uri' ])
41
+ song_uris .append (uri )
42
+ except IndexError :
43
+ print (f"{ song } Skipped." )
44
+
45
+ # Adding songs to new playlist
46
+ playlist = sp .user_playlist_create (user_id , name = f"{ date } Billboard 100" , public = False )
47
+ # print(playlist)
48
+ sp .playlist_add_items (playlist_id = playlist ["id" ], items = song_uris )
0 commit comments