-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtract.py
77 lines (63 loc) · 2.08 KB
/
Extract.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
import os
import boto3
import json
import base64
from requests import post
import requests
from os.path import join, dirname
from datetime import datetime
def lambda_handler(event, context):
# TODO implement
client_id = os.getenv("Client_id")
client_secret = os.getenv("Client_secret")
def get_token():
"""
Function to get the token
"""
auth_string = f"{client_id}:{client_secret}"
auth_base64 = base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
url = "https://accounts.spotify.com/api/token"
headers = {
"Authorization": f"Basic {auth_base64}",
"Content-Type": "application/x-www-form-urlencoded"
}
data = {"grant_type": "client_credentials"}
result = post(url, headers=headers, data=data)
json_result = json.loads(result.content)
return json_result["access_token"]
def get_auth_header(token):
"""
Function to get the auth header
"""
return {"Authorization": f"Bearer {token}"}
# Get the token from the main module
TOKEN = get_token()
def return_data(token1):
"""
Function to return data from Spotify's new releases Album
"""
# Define the headers for the request
input_variables = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer {token}".format(token=token1)
}
# Make a GET request to Spotify's new releases endpoint
r = requests.get("https://api.spotify.com/v1/browse/new-releases",
headers=input_variables)
# Parse the response as JSON
data = r.json()
# Return the parsed data
return data
data = return_data(TOKEN)
filename = "spotify_raw_" + str(datetime.now()) + '.json'
s3 = boto3.client('s3')
s3.put_object(
Bucket='spotify-api-raw-1',
Key=filename,
Body=json.dumps(data)
)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}