forked from RegsonDR/spotify-save-playlists-cron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (125 loc) · 4.82 KB
/
main.py
File metadata and controls
151 lines (125 loc) · 4.82 KB
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
from time import sleep
from urllib import response
from dotenv import load_dotenv, find_dotenv
import requests
import base64
import json
import os
import datetime
import ftplib
load_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()
FTP_SERV = os.environ.get("FTP_SERV").strip()
FTP_USER = os.environ.get("FTP_USER").strip()
FTP_PASS = os.environ.get("FTP_PASS").strip()
OAUTH_TOKEN_URL = "https://accounts.spotify.com/api/token"
def ftp_login():
ftp = ftplib.FTP_TLS(FTP_SERV)
ftp.login(user=FTP_USER, passwd=FTP_PASS)
return ftp
def refresh_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)
return response.json()
def get_playlist(access_token, playlist_id):
url = "https://api.spotify.com/v1/playlists/%s" % playlist_id
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer %s" % access_token
}
response = requests.get(url, headers=headers)
return response.json()
def create_playlist(access_token, name):
#url = "https://api.spotify.com/v1/users/lenoxfro/playlists"
url = "https://api.spotify.com/v1/users/31uyjhexjxswkhnqf5mlib3xx2fi/playlists"
payload = {
"name": name,
"description": name,
"public": False
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer %s" % access_token
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
return response.json()['id']
def add_to_playlist(access_token, tracklist, new_playlist):
url = "https://api.spotify.com/v1/playlists/%s/tracks" % new_playlist
payload = {
"uris" : tracklist
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer %s" % access_token
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
return response.json()
def find_user(json, name):
for user in json['users']:
if user['name'] == name:
return user
def update_user(json, newUser):
for user in json['users']:
if user['name'] == newUser['name']:
user = newUser
return json
def get_playlist_tracks(access_token, playlist):
response = get_playlist(access_token, playlist)
return response['tracks']['total']
def get_playlist_image(access_token, playlist):
response = get_playlist(access_token, playlist)
return response['images'][0]['url']
def main():
if REFRESH_TOKEN is None or CLIENT_ID is None or CLIENT_SECRET is None:
print("Environment variables have not been loaded!")
return
today = datetime.date.today()
access_token = refresh_access_token()['access_token']
ftp = ftp_login()
exportFile = open('export.json', 'wb')
ftp.retrbinary('RETR playlists.json', exportFile.write, 1024)
exportFile.close()
export = json.load(open('export.json', 'r'))
#get all playlists to save
f = open('playlists.json')
playlistConfig = json.load(f)
for username in playlistConfig:
playlist = playlistConfig[username]
name = str(today.isocalendar().year) + ' ' + playlist['prefix'] + ' ' + str(today.isocalendar().week)
tracks = get_playlist(access_token, playlist['weekly'])['tracks']['items']
tracklist = []
for item in tracks:
tracklist.append(item['track']['uri'])
newPlaylistId = create_playlist(access_token, name)
response = add_to_playlist(access_token, tracklist, newPlaylistId)
sleep(5)
newPlaylistObj = {
"id": newPlaylistId,
"name": name,
"image": get_playlist_image(access_token, newPlaylistId),
"week": "KW " + str(today.isocalendar().week),
"trackCount": get_playlist_tracks(access_token, newPlaylistId)
}
user = find_user(export, username)
user['playlists'].insert(1, newPlaylistObj)
export = update_user(export, user)
json.dump(export, open('export.json', 'w'))
if "snapshot_id" in response:
print("Successfully added all songs to \"" + name + "\"")
else:
print(response)
exportFile.close()
#upload File
ftp.storbinary('STOR playlists.json', open('export.json', 'rb'))
main()