-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_handler.py
More file actions
101 lines (83 loc) · 3.26 KB
/
lambda_handler.py
File metadata and controls
101 lines (83 loc) · 3.26 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
import os
import json
import boto3
import base64
import requests
from datetime import date
import csv
def handler(event, context):
client_id = os.environ['CLIENT_ID']
client_secret = os.environ['CLIENT_SECRET']
# The following code to establish a token for the requests is adapted from https://stackoverflow.com/questions/65435932/spotify-api-authorization-code-flow-with-python
# URLS
AUTH_URL = 'https://accounts.spotify.com/authorize'
TOKEN_URL = 'https://accounts.spotify.com/api/token'
# Make a request to the /authorize endpoint to get an authorization code
auth_code = requests.get(AUTH_URL, {
'client_id': client_id,
'response_type': 'code',
'redirect_uri': 'http://savemymizes/callback',
'scope': 'playlist-read-private',
})
# Encode the header
auth_header = base64.urlsafe_b64encode((client_id + ':' + client_secret).encode())
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic %s' % auth_header.decode('ascii')
}
payload = {
'grant_type': 'client_credentials',
'code': auth_code,
'redirect_uri': 'http://savemymizes/callback',
'client_id': client_id,
'client_secret': client_secret,
}
# Make a request to the /token endpoint to get an access token
access_token_request = requests.post(url=TOKEN_URL, data=payload, headers=headers)
# Convert the response to JSON
access_token_response_data = access_token_request.json()
# Save the access token to a variable
access_token = access_token_response_data['access_token']
# Now that we have a token; track down the playlist -> first finding my user
daily_mix_req = requests.get('https://api.spotify.com/v1/playlists/37i9dQZF1E38wUQ750W2hc', headers={'Authorization': 'Bearer ' + access_token})
daily_mix = daily_mix_req.json()
# Instantiate a csv to write the artists and song titles to
filename = date.today().strftime('%Y-%m-%d') + '.csv'
f = open('/tmp/' + filename, 'w')
writer = csv.writer(f)
# Iterate through the playlist to find the song name and artists
for item in daily_mix['tracks']['items']:
# Get the name of the song
name = item['track']['name']
# Get the artists
artists = []
for artist in item['track']['artists']:
artists.append(artist['name'])
# Create the line (name, artists)
csv_line = [name, artists]
# Write to the csv in memory
writer.writerow(csv_line)
f.close()
# Reopen and read as a binary
put_data = open('/tmp/' + filename, 'rb')
# Instantiate an s3 client
s3 = boto3.client('s3')
# Try and dump the bytes object into the S3 bucket
try:
s3.put_object(Body=put_data, Bucket=os.environ['BUCKET_NAME'], Key=filename)
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': os.environ['BUCKET_NAME'],
'Key': filename
},
ExpiresIn=24 * 3600
)
return url
except FileNotFoundError:
print("The file was not found")
return None
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
handler(None, None)