-
-
Save TheMuellenator/c84616c21f0f9ce68c12c357d3e1c794 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have added few more features:
This way we can have new playlist on every run.
`
GOAL - to create a spotify playlist of top 100 songs on billboard.com on a selected day
import requests
from bs4 import BeautifulSoup
import time
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pprint
spotify_client_id = "..."
spotify_client_secret = "..."
spotify_redirect_uri = "https://example.com/"
def get_top_100_titles(selected_date) -> list:
"""
Fetches the top 100 titles for a selected date from billboard.com and returns
:return: list of top 100 titles
"""
print(f"Getting top 100 titles on {selected_date} from billboard...")
def write_data(selected_date, data: list) -> None:
"""
Writes the data to a text file
:param selected_date: user selected date
:param data: list
:return: None
"""
print("Adding the top 100 songs to the file...")
def humanize_time(seconds) -> None:
intervals = [("d", 86400), ("h", 3600), ("m", 60)]
parts = []
if seconds:
for label, count in intervals:
value, seconds = divmod(seconds, count)
def add_to_playlist(date, titles_to_add):
print("Authenticating with Spotify...")
# steps to add the title to the user's spotify playlist
# 1. get client_id, client_secret from spotify
#--------------------#--------------------#
get the date from the user
user_chosen_date = input("Which year do you want to travel to? Type the date in YYYY-MM-DD: ")
to track the time for program execution
start_t = time.time()
get the top 100 songs/titles for the selected date
top_100_titles = get_top_100_titles(user_chosen_date)
if top_100_titles:
# write the titles to a text file
write_data(user_chosen_date, top_100_titles)
# add tracks to the user's playlist
add_to_playlist(user_chosen_date, top_100_titles)
end_t = time.time()
humanize_time(end_t - start_t)
`