-
Notifications
You must be signed in to change notification settings - Fork 4
/
spotify.py
92 lines (82 loc) · 3.38 KB
/
spotify.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
#**********************************************************************************
#Spotify_Handle V0.01
#By:Ben Bellerose
#Description: This program handles all spotify interaction for the application.
#Reference: https://github.com/drshrey/spotify-flask-auth-example
#**********************************************************************************
import json
import requests
import base64
import urllib
from flask import request
# Client Keys
CLIENT_ID = "<FILL IN YOUR CLIENT ID>"
CLIENT_SECRET = "<FILL IN YOUR CLIENT SECRET>"
#Spotify URLS
SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
SPOTIFY_API_BASE_URL = "https://api.spotify.com"
API_VERSION = "v1"
SPOTIFY_API_URL = "{}/{}".format(SPOTIFY_API_BASE_URL, API_VERSION)
#Server-side Parameters
CLIENT_SIDE_URL = "http://127.0.0.1"
PORT = 8080
REDIRECT_URI = "{}:{}/callback/q".format(CLIENT_SIDE_URL, PORT)
SCOPE = "user-library-read"
STATE = ""
SHOW_DIALOG_bool = True
SHOW_DIALOG_str = str(SHOW_DIALOG_bool).lower()
#Authorization of application with spotify
def app_Authorization():
auth_query_parameters = {
"response_type": "code",
"redirect_uri": REDIRECT_URI,
"scope": SCOPE,
# "state": STATE,
# "show_dialog": SHOW_DIALOG_str,
"client_id": CLIENT_ID
}
url_args = "&".join(["{}={}".format(key,urllib.quote(val)) for key,val in auth_query_parameters.iteritems()])
auth_url = "{}/?{}".format(SPOTIFY_AUTH_URL, url_args)
return auth_url
#User allows us to acces there spotify
def user_Authorization():
auth_token = request.args['code']
code_payload = {
"grant_type": "authorization_code",
"code": str(auth_token),
"redirect_uri": REDIRECT_URI
}
base64encoded = base64.b64encode("{}:{}".format(CLIENT_ID, CLIENT_SECRET))
headers = {"Authorization": "Basic {}".format(base64encoded)}
post_request = requests.post(SPOTIFY_TOKEN_URL, data=code_payload, headers=headers)
# Tokens are Returned to Application
response_data = json.loads(post_request.text)
access_token = response_data["access_token"]
refresh_token = response_data["refresh_token"]
token_type = response_data["token_type"]
expires_in = response_data["expires_in"]
# Use the access token to access Spotify API
authorization_header = {"Authorization":"Bearer {}".format(access_token)}
return authorization_header
#Gathering of profile information
def Profile_Data(header):
# Get user profile data
user_profile_api_endpoint = "{}/me".format(SPOTIFY_API_URL)
profile_response = requests.get(user_profile_api_endpoint, headers=header)
profile_data = json.loads(profile_response.text)
return profile_data
#Gathering of playlist information
def Playlist_Data(header,profile):
# Get user playlist data
playlist_api_endpoint = "{}/playlists".format(profile["href"])
playlists_response = requests.get(playlist_api_endpoint, headers=header)
playlist_data = json.loads(playlists_response.text)
return playlist_data
#Gathering of album information
def Album_Data(header,profile,limit,offset):
# Get user albums data
artist_api_endpoint = ("{}/albums?limit=" + str(limit) + "&offset=" + str(offset)).format(profile["href"])
artist_response = requests.get(artist_api_endpoint, headers=header)
artist_data = json.loads(artist_response.text)
return artist_data