-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spotify_Auto_Discover_Weekly_Playlist_Backup.py
151 lines (113 loc) 路 6.56 KB
/
Spotify_Auto_Discover_Weekly_Playlist_Backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#Made by Thomas Mirbey, Discord : tfx_, github: github.com/ThomasM2568
#This Python script automates the process of creating a weekly backup playlist of the "Discover Weekly"
#playlist on Spotify. It uses the Spotipy library to interact with the Spotify Web API.
#The script checks if the "Discover Weekly" playlist exists in the user's profile and creates a new playlist named a
#fter the current week if it doesn't exist. Then, it copies the songs from the "Discover Weekly" playlist to the new backup playlist
#ensuring that your favorite weekly recommendations are saved for future listening. The script requires proper authentication using Spotify's OAuth flow
#and it may need to be authorized with the necessary scopes to perform playlist-related actions.
#Make sure to set up your Spotify credentials and authentication details before running the script.
# -----------------------------------------------------------------------------------------------
# Importing modules
# -----------------------------------------------------------------------------------------------
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import datetime
# -----------------------------------------------------------------------------------------------
# Client Information
# -----------------------------------------------------------------------------------------------
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
redirect_uri = 'http://localhost:8080'
scope = 'playlist-read-private playlist-modify-public playlist-modify-private'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope))
# -----------------------------------------------------------------------------------------------
# Functions
# -----------------------------------------------------------------------------------------------
def get_user_playlists(sp):
playlists={}
try:
results = sp.current_user_playlists()
for idx, item in enumerate(results['items']):
playlists[item['name']]=item['id']
except:
print("An error occured")
return playlists
def get_weekly_discovery_playlist_name():
current_date = datetime.datetime.now().isocalendar()[1]
date1=str(datetime.datetime.now().date()).split("-")
date2=date1[0]+"-"+date1[1]
playlist_name="Weekly Playlist - Week "+str(current_date)+" ("+date2+")"
return playlist_name
def is_playlist_in_dict(playlist_name,dictionnary):
if not(playlist_name in dictionnary.keys()):
return False
else:
return True
def get_user_id():
return sp.me()["id"]
def get_playlist_id(playlist_name, sp):
playlists = get_user_playlists(sp)
if playlist_name in playlists.keys():
return playlists[playlist_name]
return None
def main(sp):
try:
playlists = get_user_playlists(sp)
playlist_name = get_weekly_discovery_playlist_name()
if not is_playlist_in_dict('Discover Weekly', playlists):
print("No Discover Weekly playlist in user's profile")
else:
weekly_discover_playlist_id = playlists["Discover Weekly"]
if is_playlist_in_dict(playlist_name, playlists):
print("Weekly Discover playlist backup already exists")
else:
user_info = get_user_id()
week = datetime.datetime.now().isocalendar()[1]
sp.user_playlist_create(user=user_info, name=playlist_name, public=False, collaborative=False, description='Copy of the Discover Weekly playlist (Week '+str(week)+')')
playlist_name_id = get_playlist_id(playlist_name, sp)
if playlist_name_id is not None:
try:
for songs in sp.user_playlist_tracks(user=user_info, playlist_id=weekly_discover_playlist_id, fields=None, limit=100, offset=0, market=None)["items"]:
sp.user_playlist_add_tracks(user=user_info, playlist_id=playlist_name_id, tracks=[songs["track"]["uri"]], position=None)
return("Playlist created successfully")
except spotipy.exceptions.SpotifyException as e:
print("Error while adding tracks:", e)
else:
return("Error in get_playlist_id()")
except Exception as e:
# Exception handling code
return str(e)
# -----------------------------------------------------------------------------------------------
# Code Execution
# -----------------------------------------------------------------------------------------------
print(main(sp))
#Old code in case it does work
"""
try:
if not("Discover Weekly" in playlists.keys()):
print("No Discover Weekly playlist in user's profile")
else:
weekly_discover_playlist_id=playlists["Discover Weekly"]
if(playlist_name in playlists.keys()):
print("Weekly Discover playlist backup already exists")
else:
user_info=sp.me()["id"]
sp.user_playlist_create(user=user_info, name=playlist_name, public=False, collaborative=False, description='Copy of the recommanded playlist of the '+str(date_actuelle)+' week')
try:
results = sp.current_user_playlists()
for idx, item in enumerate(results['items']):
if(item['name']==playlist_name):
playlist_name_id=item["id"]
except:
print("An error occured")
try:
for songs in sp.user_playlist_tracks(user=user_info, playlist_id=weekly_discover_playlist_id, fields=None, limit=100, offset=0, market=None)["items"]:
sp.user_playlist_add_tracks(user=user_info, playlist_id=playlist_name_id, tracks=[songs["track"]["uri"]], position=None)
except spotipy.exceptions.SpotifyException as e:
print("Error while adding tracks :", e)
except spotipy.exceptions.SpotifyException as e:
print("Error:", e)
"""