forked from jennielees/funky-music
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
141 lines (93 loc) · 4.23 KB
/
app2.py
File metadata and controls
141 lines (93 loc) · 4.23 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
from flask import Flask, request, render_template, jsonify, session, redirect
from spotify import get_music
import requests
app = Flask(__name__)
app.secret_key = 'ilovesecrets'
SPOTIFY_CLIENT_ID = '7c9a81122ea446d9a75dd69a38a1fc33'
SPOTIFY_CLIENT_SECRET = '4909ccc6903645de89c1dfa4b09d7669'
@app.route('/')
def signin():
# We are going to immediately redirect to spotify. We could ask the user
# to sign in by clicking a button, if this were a more interesting app.
return render_template('login.html')
@app.route('/index', methods=["post"] )
def index():
# We are going to immediately redirect to Google. We could ask the user
# to sign in by clicking a button, if this were a more interesting app.
oauth()
return render_template('index.html')
#return redirect(signin_url)
def oauth():
# We are going to immediately redirect to Google. We could ask the user
# to sign in by clicking a button, if this were a more interesting app.
signin_url = 'https://accounts.spotify.com/authorize/?'
parameters = [
'client_id={}'.format(SPOTIFY_CLIENT_ID),
'redirect_uri={}'.format('http://localhost:8080/oauth2callback'),
'response_type=code',
'scope=user-read-private user-read-email']
#signin_url = 'https://accounts.google.com/o/oauth2/auth?'
signin_url += '&'.join(parameters)
return redirect(signin_url)
@app.route('/oauth2callback')
def callback():
print "CALLBACK REDIRECT"
code = request.args.get('code')
error = request.args.get('error')
if error:
return("Error! {}".format(error))
# Exchange the code for an access token
token_url = 'https://spotify.com/o/oauth2/token'
data = {
'client_id': SPOTIFY_CLIENT_ID,
'client_secret': SPOTIFY_CLIENT_SECRET,
'redirect_uri': 'http://localhost:8080/oauth2callback',
'code': code,
'grant_type': 'authorization_code'
}
r = requests.post(token_url, data=data)
# In an ideal world we would store this token and use it to make all our
# future API requests.
token = r.json().get('access_token')
# Now we're going to call a Google API, in this case the userinfo API
r = requests.get('https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token={}'.format(token))
info = r.json()
print info
user_name = info.get('name')
return "Thank you for signing in, {}".format(user_name)
def get_playlist_tracks(user_id, playlist_id):
token = SPOTIFY_TOKEN
#token = 'BQD3LH8JXyl-KZ_sDSmZu0_zjyNy1mc7ZeMK-3XlqeHB9_Zo5xeMDZUZMz2JeQ_Ec2wiu4sEAy158oIrz9GBo8tZhMcJKiiCJ93F7fYpR8eBQS9XEyJRQW1opeQUu_bPuegh1cbTNItC-6rC'
headers = { 'Authorization': 'Bearer {}'.format(token) }
url = 'https://api.spotify.com/v1/users/{user_id}/playlists/{playlist_id}/tracks'
return requests.get(url, headers=headers).json()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/music', methods=['POST'])
def music():
mood = request.form.get('mood')
music = get_music(mood)
# Store the mood in the session so we don't need to send it up all the time
session['mood'] = mood
print '$$$$$$$$$$$$$$$$$$$$ music done $$$$$$$$$$$$$$$$$$$'
return render_template('music.html', music=music)
#@app.route('/api/music', methods=['POST'])
#def music_api():
# print ' #####################entering api ##################################'
# offset = request.form.get('offset')
# mood = session.get('mood')
# We can skip arguments with defaults - we don't need to supply 'limit'
# but we do need to specify that the thing we're sending is 'offset'
# music = get_music(mood, offset=offset)
# print '##################### api done #######################'
# return jsonify({'playlists': music}) # you can't just jsonify a list
@app.route('/playlist/<username>/<playlist_id>', methods=['POST'])
def playlist(username, playlist_id):
print '############################# inside of playlist ###################'
username = '1112389416'
playlist_id = '2yCIJgYWeWZmShpthNazPB'
tracks = get_playlist_tracks(username, playlist_id)
return render_template('playlist.html', tracks=tracks)
if __name__ == "__main__":
app.run(port=8080, debug=True)