-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
46 lines (35 loc) · 1001 Bytes
/
server.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
from flask import Flask, send_from_directory
from Spotify import Spotify
from json import loads
# Flask config
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
# global spotify object for api calls
spotify = Spotify()
@app.route('/api/<artist>')
def api(artist):
"""
API route - fetches and sends artist data
as JSON response
"""
artist = artist.replace('-', ' ')
return spotify.get_artist(artist)
@app.route('/js/<path:path>')
@app.route('/css/<path:path>')
def send_from_static(path):
"""
Static file route for sending JS and CSS files
"""
return send_from_directory('static', path)
@app.route('/<artist>')
@app.route('/')
def home(artist=None):
"""
Main routing - sends to artist page if artist is
in the request URL, defaults to home page
"""
file = 'artist.html' if artist else 'index.html'
return send_from_directory('static', file)
# when run as main
if __name__ == '__main__':
app.run(debug=True)