You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, first I want to say thank you for the script, it works wonderfully.
But I always "archieved" the weekly playlist by making a new playlist and naming it YEAR-MONTH-DAY it came out. I was doing that manually, but I didn't always remember to do that.
Your script helped me achieve a backup so I always could retrieve it from there, but I finally found some time and will to expand it a little bit.
I see the code changed quite a bit since I pulled it and used it, so I'll leave the code with my additions here
fromdotenvimportload_dotenv, find_dotenvimportrequestsimportbase64importjsonimportosfromdatetimeimportdatetimeload_dotenv(find_dotenv())
REFRESH_TOKEN=os.environ.get("REFRESH_TOKEN").strip()
CLIENT_ID=os.environ.get("CLIENT_ID").strip()
CLIENT_SECRET=os.environ.get("CLIENT_SECRET").strip()
DISCOVER_WEEKLY_ID=os.environ.get("DISCOVER_WEEKLY_ID").strip()
SAVE_TO_ID=os.environ.get("SAVE_TO_ID").strip()
OAUTH_TOKEN_URL="https://accounts.spotify.com/api/token"defrefresh_access_token():
payload= {
"refresh_token": REFRESH_TOKEN,
"grant_type": "refresh_token",
"client_id": CLIENT_ID,
}
encoded_client=base64.b64encode((CLIENT_ID+":"+CLIENT_SECRET).encode('ascii'))
headers= {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic %s"%encoded_client.decode('ascii')
}
response=requests.post(OAUTH_TOKEN_URL, data=payload, headers=headers)
returnresponse.json()
defget_playlist(access_token):
url="https://api.spotify.com/v1/playlists/%s"%DISCOVER_WEEKLY_IDheaders= {
"Content-Type": "application/json",
"Authorization": "Bearer %s"%access_token
}
response=requests.get(url, headers=headers)
returnresponse.json()
defadd_to_playlist(access_token, tracklist, playlist_id):
url="https://api.spotify.com/v1/playlists/%s/tracks"%playlist_idpayload= {
"uris" : tracklist
}
headers= {
"Content-Type": "application/json",
"Authorization": "Bearer %s"%access_token
}
response=requests.post(url, data=json.dumps(payload), headers=headers)
returnresponse.json()
# get users id for creating a new playlistdefget_user_id(access_token):
url="https://api.spotify.com/v1/me/"headers= {
"Content-Type": "application/json",
"Authorization": "Bearer %s"%access_token
}
response=requests.get(url, headers=headers)
returnresponse.json()['id']
# create a new playlist and return its iddefcreate_new_playlist_and_get_id(access_token, user_id):
url="https://api.spotify.com/v1/users/%s/playlists"%user_idpayload= {
"name": datetime.now().strftime('%Y-%m-%d'), # name playlist YEAR-MOTH-DAY"public": True
}
headers= {
"Content-Type": "application/json",
"Authorization": "Bearer %s"%access_token
}
response=requests.post(url, data=json.dumps(payload), headers=headers)
print(response.status_code)
ifnotresponse.status_code==201:
print("There was a problem creating a playlist, response "+response.status_code)
else:
returnresponse.json()["id"]
defmain():
ifREFRESH_TOKENisNoneorCLIENT_IDisNoneorCLIENT_SECRETisNoneorDISCOVER_WEEKLY_IDisNoneorSAVE_TO_IDisNone:
print("Environment variables have not been loaded!")
returnaccess_token=refresh_access_token()['access_token']
tracks=get_playlist(access_token)['tracks']['items']
tracklist= []
foritemintracks:
tracklist.append(item['track']['uri'])
user_id=get_user_id(access_token)
playlist_id=create_new_playlist_and_get_id(access_token, user_id)
response=add_to_playlist(access_token, tracklist, playlist_id)
if"snapshot_id"inresponse:
print("Successfully added all songs")
else:
print(response)
main()
It's not perfect as it'll probably crash if there's a problem when creating a new playlist, but when it works it works.
I like to keep them in a separate folder, but I couldn't find in the API documentation anything about folders, but can be done manually. What's important to me that the playlist is saved.
The text was updated successfully, but these errors were encountered:
Hi, first I want to say thank you for the script, it works wonderfully.
But I always "archieved" the weekly playlist by making a new playlist and naming it YEAR-MONTH-DAY it came out. I was doing that manually, but I didn't always remember to do that.
Your script helped me achieve a backup so I always could retrieve it from there, but I finally found some time and will to expand it a little bit.
I see the code changed quite a bit since I pulled it and used it, so I'll leave the code with my additions here
It's not perfect as it'll probably crash if there's a problem when creating a new playlist, but when it works it works.
I like to keep them in a separate folder, but I couldn't find in the API documentation anything about folders, but can be done manually. What's important to me that the playlist is saved.
The text was updated successfully, but these errors were encountered: