Skip to content

Commit 48f08f5

Browse files
Update Day-46
1 parent 0585bac commit 48f08f5

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Day-46-MusicalTimeMachine/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Musical Time Machine
2+
3+
### Create your own Spotify playlist, provide any date and listen to Billboard-Hot-100 from that week.
4+
5+
<img src= 'https://user-images.githubusercontent.com/65078610/108618849-6622f880-7447-11eb-9c0e-4c02671b31fe.PNG' width="800">

Day-46-MusicalTimeMachine/main.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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)

Day-46-MusicalTimeMachine/token.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"access_token": "BQBQxolS_juVWoMzNx8hT-dep2E72ZzcOuDYaWM4ktahRc8IOHx7_4FkHu01neu0_iNU-do8ES-2PLTtbClvCYzhZcIagtDGa6MlD8paI-IMnROwF8DfStbnMOSzxwKgzP6LBAzHSDBy25A8kPnbnVMBQymwgmJ8HTNc00H31TNRxbc5eOhoMArnIqesJ5zbowFEpg", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "AQDhm0ML__0OIadIoJTYThp2jwOi1wwY55-K6gCfvgGZA-RSVu6tQaXB3EmoV97JXwzs9fFM53I6gb-Dq0d4c7EorLRD3-9KlKv2-mQiyZLh1ex0pbsxwAwAD-rVxFs6dLQ", "scope": "playlist-modify-private", "expires_at": 1613897864}

0 commit comments

Comments
 (0)