Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d16ae2a

Browse files
committed
Automate Spotify Discover Weekly Playlist
Add a python script which connects to the spotify API and integrates OAuth. Use OAuth to authenticate with Spotify, get an access token and store it in a Flask session retrieving spotify user data from token.
1 parent cd7106a commit d16ae2a

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

‎Spotify Playlist Automation/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Spotify Playlist Automation
2+
3+
# Description
4+
Its an application which allows the user to automate saving the "Discover Weekly" Playlist into a new playlist called as- "Saved Weekly". Since new songs are added every week, the user wont have to add songs everytime.
5+
6+
# Prerequisite tasks:
7+
1. Make sure you have made the "Discover Weekly" Playlist public by selecting the playlist to be displayed on profile.
8+
9+
2. Browse to https://developer.spotify.com/dashboard/applications.
10+
11+
3. Log in with your Spotify account.
12+
13+
4. Click on ‘Create an app’.
14+
15+
5. Pick an ‘App name’ and ‘App description’ of your choice and mark the checkboxes.
16+
17+
6. After creation, you see your ‘Client Id’ and you can click on ‘Show client secret` to unhide your ’Client secret’
18+
19+
# Getting Started
20+
21+
1. Install the dependencies via the requirements txt file:
22+
23+
$ pip install -r requirements.txt
24+
25+
2. After installation, Update the client_id and secret_key in the configuration file to authenticate the spotipy api
26+
27+
3. After updation run the file spotify_discover_weekly.py in the terminal
28+
29+
4. Click on the link which is generated. It is basically running the app on the port locally.

‎Spotify Playlist Automation/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
client_id = "" #insert your client id
2+
secret_key = "" #insert your secret key
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==2.3.2
2+
spotipy==2.23.0
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import config
2+
import spotipy
3+
import time
4+
from spotipy.oauth2 import SpotifyOAuth
5+
from flask import Flask,request,url_for,session,redirect
6+
7+
app= Flask(__name__)
8+
app.config['SESSION_COOKIE_NAME']= 'spotify cookie'
9+
app.secret_key='10)293@han2%^s*hka02zkls'
10+
TOKEN_INFO='token_info'
11+
@app.route('/')
12+
def login():
13+
auth_url=create_spotify_oauth().get_authorize_url()
14+
return redirect(auth_url)
15+
16+
@app.route('/redirect')
17+
def redirect_page():
18+
session.clear()
19+
code = request.args.get('code')
20+
token_info = create_spotify_oauth().get_access_token(code)
21+
session[TOKEN_INFO]= token_info
22+
return redirect(url_for('save_discover_weekly', external= True))
23+
24+
@app.route('/saveDiscoverWeekly')
25+
def save_discover_weekly():
26+
try:
27+
token_info =get_token()
28+
except:
29+
print("User not logged in")
30+
return redirect ('/')
31+
sp = spotipy.Spotify(auth= token_info['access_token'])
32+
# to get user's playlists which are public
33+
current_playlists = sp.current_user_playlists()['items']
34+
discover_weekly_playlist_id = None
35+
saved_weekly_playlist_id = None
36+
user_id= sp.current_user()['id']
37+
playlist_names=[]
38+
playlist_id=[]
39+
k=0
40+
# find the Discover Weekly and Saved Weekly playlists
41+
for playlist in current_playlists:
42+
playlist_names.append(playlist['name'])
43+
playlist_id.append(playlist['id'])
44+
for playlist in playlist_names:
45+
if playlist=='Discover Weekly':
46+
discover_weekly_playlist_id=playlist_id[k]
47+
if playlist== 'Saved Weekly':
48+
saved_weekly_playlist_id=playlist_id[k]
49+
k+=1
50+
if discover_weekly_playlist_id== None:
51+
return 'Discover Weekly not found.'
52+
if saved_weekly_playlist_id== None:
53+
new_playlist= sp.user_playlist_create(user_id, 'Saved Weekly', True)
54+
saved_weekly_playlist_id= new_playlist['id']
55+
56+
# get the tracks from the Discover Weekly playlist
57+
discover_weekly_playlist = sp.playlist_items(discover_weekly_playlist_id)
58+
song_uris = []
59+
for song in discover_weekly_playlist['items']:
60+
song_uri= song['track']['uri']
61+
song_uris.append(song_uri)
62+
# we need uris because when we want to add songs to the playlist, we need uris parameter
63+
sp.user_playlist_add_tracks(user_id,saved_weekly_playlist_id,song_uris, None)
64+
return("Playlist Update Success")
65+
66+
def get_token():
67+
#checking two edge cases, 1. If the token is expired, 2. Token doesn't exist
68+
token_info= session.get(TOKEN_INFO,None )
69+
if not token_info:
70+
redirect_page(url_for('login', external =False))
71+
current_time= int(time.time())
72+
is_expired = token_info['expires_at'] - current_time <60
73+
if (is_expired):
74+
spotify_oauth= create_spotify_oauth()
75+
token_info= spotify_oauth.refresh_access_token(token_info['refresh_token'])
76+
return token_info
77+
78+
def create_spotify_oauth():
79+
return SpotifyOAuth(client_id= config.client_id, client_secret=config.secret_key, redirect_uri= url_for('redirect_page', _external= True), scope='user-library-read playlist-modify-public playlist-modify-private')
80+
81+
app.run(debug=True)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /