Last active
November 18, 2021 23:20
-
-
Save xthesaintx/4c75faf391572b02cc50ac794e9e4e03 to your computer and use it in GitHub Desktop.
Makes a copy of Spotify's Discover Weekly or Release Radar depending on day of week [M-Th = DW / F-Su = RR] and names it [DW/RR] - [YEAR] - [WEEK]
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
#!/usr/bin/env python3 | |
import spotipy | |
import datetime | |
import pprint | |
from spotipy.oauth2 import SpotifyOAuth | |
# CHECKS IF PLAYLIST EXSISTS | |
def does_playlist_exist(u,s,pl): | |
x = s.user_playlists(u) | |
for x in x['items']: | |
if x['name'] == pl: | |
return True | |
return False | |
# CREATES PLAYLIST | |
def create_playlist(u, s, pl): | |
s.trace = False | |
s.user_playlist_create(u, pl, True) # True indicates it is a public playlist | |
# GET PLAYLIST ID of Created PL | |
def get_playlist_id(u,s,pl): | |
x = s.user_playlists(u) | |
for x in x['items']: | |
if x['name'] == pl: | |
return x['id'] | |
# GET TRACK IDs | |
def get_track_ids(tracks): | |
track_ids=[] | |
for i, item in enumerate(tracks['items']): | |
track = item['track'] | |
track_ids.append(track['id']) | |
print(" %d %32.32s %s" % (i, track['artists'][0]['name'], track['name'])) | |
return track_ids | |
# GET PLAYLIST ID | |
def add_new_songs(u,s,pl,plcy): | |
results = s.user_playlist('spotify',plcy) | |
tracks = results['tracks'] | |
track_ids = get_track_ids(tracks) | |
results = sp.user_playlist_add_tracks(u, pl, track_ids) | |
print (results) | |
# MAIN CODE | |
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR ID", | |
client_secret="YOUR SECRET", | |
redirect_uri="YOUR URI", | |
username='YOUR USER ID', | |
scope="user-library-read playlist-modify-public playlist-modify-private")) | |
# Set some vars | |
dw = "YOUR DW PLAYLIST ID" | |
rr = "YOUR RR PLAYLIST ID" | |
dwdays = ["Mon","Tue","Wed","Thu"] | |
# dwdays = ["Mon"] | |
username = 'YOUR USER ID' | |
# GET THE WEEK NUMBER | |
today = datetime.datetime.now() | |
# Get day of week | |
dayofweek = today.strftime("%a") | |
# DW | |
playlistname = "DW - "+today.strftime("%y")+" - "+today.strftime("%V") | |
playlisttocopy = dw | |
playlistdescription = "Discover Weekly for the Week "+today.strftime("%V")+" - "+today.strftime("%y") | |
if does_playlist_exist(username,sp,playlistname) == False: | |
create_playlist(username, sp, playlistname) | |
plid = get_playlist_id(username,sp,playlistname) | |
print('--- DISCOVER WEEKLY '+today.strftime("%V")+' ---') | |
print('Adding the following songs:') | |
add_new_songs(username, sp, plid ,playlisttocopy) | |
# RR | |
# SET THE WEEK back one if it's not Fri-Sun Weeks start on MONDAY | |
dwdays = ["Mon","Tue","Wed","Thu"] | |
if dayofweek in dwdays: | |
weekadjust = str(int(today.strftime("%V"))-1) | |
else: | |
weekadjust = str(int(today.strftime("%V"))) | |
playlistname = "RR - "+today.strftime("%y")+" - "+weekadjust | |
playlisttocopy = rr | |
playlistdescription = "Release Raday for the Week "+weekadjust+" - "+today.strftime("%y") | |
if does_playlist_exist(username,sp,playlistname) == False: | |
create_playlist(username, sp, playlistname) | |
plid = get_playlist_id(username,sp,playlistname) | |
print('') | |
print('--- RELEASE RADAR '+weekadjust+' ---') | |
print('Adding the following songs:') | |
add_new_songs(username, sp, plid ,playlisttocopy) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I automate this on my pi, you need to grab a auth token, easiest way is to run the script on your desktop first, on the mac it will create a hidden file on the desktop (auth token), copy this to the script location on the pi. Now it doesn't need to open a web browser to auth.
I run it from cron twice a week
0 8 * * 1 cd /home/pi/Scripts && /home/pi/Scripts/spotify.py > /home/pi/Scripts/spotify.log 2>&1