-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
89 lines (70 loc) · 3.04 KB
/
server.py
File metadata and controls
89 lines (70 loc) · 3.04 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
import os
from flask import (
Flask,
render_template,
session,
request,
redirect,
url_for,
flash,
)
from spotipy import Spotify, CacheHandler
from spotipy.oauth2 import SpotifyOAuth
from utils import get_top_artists, get_ai_judgment, get_ai_reccomendations
import json
SPOITFY_CLIENT_ID = os.environ.get("SPOTIFY_CLIENT_ID")
SPOTIFY_CLIENT_SECRET = os.environ.get("SPOTIFY_CLIENT_SECRET")
class CacheSessionHandler(CacheHandler):
def __init__(self, session, token_key):
self.token_key = token_key
self.session = session
def get_cached_token(self):
return self.session.get(self.token_key)
def save_token_to_cache(self, token_info):
self.session[self.token_key] = token_info
session.modified = True
app = Flask(__name__)
env = os.environ.get("ENV", "DEV")
app.secret_key = os.environ.get("SECRET_KEY") if "ENV" == "PROD" else "dev"
oauth_manager = SpotifyOAuth(
client_id=SPOITFY_CLIENT_ID,
client_secret=SPOTIFY_CLIENT_SECRET,
redirect_uri="http://localhost:5000" if env == "DEV" else "http://localhost:8000" if env == "STAGE" else "https://musicreviewer.onrender.com/",
scope="user-top-read",
cache_handler=CacheSessionHandler(session, "spotify_token"),
show_dialog=True,
)
@app.route("/")
def homepage():
session.pop("spotify_token", None)
if request.args.get("code"):
oauth_manager.get_access_token(request.args.get("code"))
logged_in = oauth_manager.validate_token(oauth_manager.get_cached_token())
return render_template(
"index.html", spotify_auth_url=oauth_manager.get_authorize_url(), logged_in=logged_in
)
@app.route("/spotify-info", methods=["GET", "POST"])
def show_spotify_info():
data = request.form
time_period_map = {"short_term": "Past Week", "medium_term": "Past Month", "long_term": "Past Year"}
return render_template("show_spotify_info.html", time_period=data["time_period"] if data else "long_term", time_period_words=time_period_map[data["time_period"] if data else "Past Year"])
@app.route("/ai_judgment", methods=["POST"])
def ai_judgment():
if not oauth_manager.validate_token(oauth_manager.get_cached_token()):
flash("You need to log in to spotify first")
return redirect("/")
data = request.json
sp = Spotify(auth_manager=oauth_manager)
toptracks_text, toptracks_list = get_top_artists(sp, time_range=data["time_period"] if data else "long_term")
ai_judgment = get_ai_judgment(toptracks_text)
return json.dumps({"judgment": ai_judgment, "toptracks": toptracks_list, "toptracks_text": toptracks_text})
@app.route("/ai_reccomendations", methods=["POST"])
def ai_reccomendations():
if not oauth_manager.validate_token(oauth_manager.get_cached_token()):
flash("You need to log in to spotify first")
return redirect("/")
data = request.json
ai_judgment = get_ai_reccomendations(data["toptracks"])
return json.dumps({"reccomendations": ai_judgment})
if __name__ == "__main__":
app.run(debug=True, use_reloader=True, use_debugger=True)