-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.py
131 lines (93 loc) · 3.56 KB
/
app.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
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
'''
This code was based on these repositories,
so special thanks to:
https://github.com/datademofun/spotify-flask
https://github.com/drshrey/spotify-flask-auth-example
'''
from flask import Flask, request, redirect, g, render_template, session
from spotify_requests import spotify
app = Flask(__name__)
app.secret_key = 'some key for session'
# ----------------------- AUTH API PROCEDURE -------------------------
@app.route("/auth")
def auth():
return redirect(spotify.AUTH_URL)
@app.route("/callback/")
def callback():
auth_token = request.args['code']
auth_header = spotify.authorize(auth_token)
session['auth_header'] = auth_header
return profile()
def valid_token(resp):
return resp is not None and not 'error' in resp
# -------------------------- API REQUESTS ----------------------------
@app.route("/")
def index():
return render_template('index.html')
@app.route('/search/')
def search():
try:
search_type = request.args['search_type']
name = request.args['name']
return make_search(search_type, name)
except:
return render_template('search.html')
@app.route('/search/<search_type>/<name>')
def search_item(search_type, name):
return make_search(search_type, name)
def make_search(search_type, name):
if search_type not in ['artist', 'album', 'playlist', 'track']:
return render_template('index.html')
data = spotify.search(search_type, name)
api_url = data[search_type + 's']['href']
items = data[search_type + 's']['items']
return render_template('search.html',
name=name,
results=items,
api_url=api_url,
search_type=search_type)
@app.route('/artist/<id>')
def artist(id):
artist = spotify.get_artist(id)
if artist['images']:
image_url = artist['images'][0]['url']
else:
image_url = 'http://bit.ly/2nXRRfX'
tracksdata = spotify.get_artist_top_tracks(id)
tracks = tracksdata['tracks']
related = spotify.get_related_artists(id)
related = related['artists']
return render_template('artist.html',
artist=artist,
related_artists=related,
image_url=image_url,
tracks=tracks)
@app.route('/profile')
def profile():
if 'auth_header' in session:
auth_header = session['auth_header']
# get profile data
profile_data = spotify.get_users_profile(auth_header)
# get user playlist data
playlist_data = spotify.get_users_playlists(auth_header)
# get user recently played tracks
recently_played = spotify.get_users_recently_played(auth_header)
if valid_token(recently_played):
return render_template("profile.html",
user=profile_data,
playlists=playlist_data["items"],
recently_played=recently_played["items"])
return render_template('profile.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/featured_playlists')
def featured_playlists():
if 'auth_header' in session:
auth_header = session['auth_header']
hot = spotify.get_featured_playlists(auth_header)
if valid_token(hot):
return render_template('featured_playlists.html', hot=hot)
return render_template('profile.html')
if __name__ == "__main__":
app.run(debug=True, port=spotify.PORT)