-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
148 lines (132 loc) · 4.75 KB
/
hello.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from flask import Flask
from flask import render_template, redirect, url_for, request, session
from flask_bootstrap import Bootstrap
import random
import spotipy
import sys
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
import json
scope = 'user-library-read'
spotify = spotipy.Spotify()
app = Flask(__name__)
app.secret_key = 'You Will Never Guess'
Bootstrap(app)
@app.route("/home", methods=['GET','POST'])
def hello_world():
if request.method == "GET":
session['codes'] = ['chill', 'happy', 'emo', 'lit', 'basic', 'unique']
session['userCount'] = 0
session['users'] = []
session['minPopular'] = 100000
session['maxPopular'] = 0
session['minDanceability'] = 1
session['maxDanceability'] = 0
session['minValence'] = 1
session['maxValence'] = 0
session['minInstrumental'] = 1
session['maxInstrumental'] = 0
session['minAcoustic'] = 1
session['maxAcoustic'] = 0
session['maxLit'] = 0
session['maxChill'] = 0
session['mostBasic'] = ""
session['mostUnique'] = ""
session['mostEmo'] = ""
session['mostHappy'] = ""
session['mostLit'] = ""
session['mostChill'] = ""
return render_template("index.html", count=session['userCount'])
if request.method == "POST":
username = request.form['username']
session['users'].append(username)
token = util.prompt_for_user_token(username, scope,client_id='04b743a8c3714b37a04cad2f516518af',client_secret='ee75f1b3dc774da0a921a8a8c10b0cac',redirect_uri = 'http://www.cs.cmu.edu/~15251/schedule.html')
tracks = []
energyTotal = 0
popularTotal = 0
danceabilityTotal = 0
valenceTotal = 0
instrumentalnessTotal = 0
acousticnessTotal = 0
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
tids = []
counter = 0
for t in (results['items']):
print("popularity",t['track']['popularity'])
popularTotal+=t['track']['popularity']
tids.append(t['track']['uri'])
features = sp.audio_features(tids)
currentFeature = features[counter]
#energy = features[len(tids)-1]['energy']
energyTotal+=features[counter]['energy']
danceabilityTotal+=features[counter]['danceability']
valenceTotal+=features[counter]['valence']
instrumentalnessTotal+=features[counter]['instrumentalness']
acousticnessTotal+=features[counter]['acousticness']
counter+=1
else:
print "Can't get token for", username
if popularTotal < session['minPopular']:
session['mostUnique'] = username
session['minPopular'] = popularTotal
if popularTotal > session['maxPopular']:
session['mostBasic'] = username
session['maxPopular'] = popularTotal
if danceabilityTotal + energyTotal > session['maxLit']:
session['maxLit'] = danceabilityTotal + energyTotal
session['mostLit'] = username
if valenceTotal < session['minValence']:
session['minValence'] = valenceTotal
session['mostEmo'] = username
if valenceTotal > session['maxValence']:
session['maxValence'] = valenceTotal
session['mostHappy'] = username
if acousticnessTotal > session['maxChill']:
session['maxChill'] = acousticnessTotal
session['mostChill'] = username
session['userCount'] +=1
session['questions']= {
"chill":session['mostChill'],
"happy":session['mostHappy'],
"emo":session['mostEmo'],
"lit":session['mostLit'],
"basic":session['mostBasic'],
"unique":session['mostUnique'],
}
if (session['userCount']==2):
return render_template("instructions.html")
return render_template("index.html")
@app.route("/calculations", methods=['GET', 'POST'])
def calc():
#do stuff
return render_template("instructions.html")
@app.route("/mostLikely", methods=['GET', 'POST'])
def play():
if request.method == "GET":
questions = session['questions']
print(questions)
codes = session['codes']
rand = random.randint(0,5)
if rand==2:
rand = 3
print(rand)
session['currq'] = session['codes'][rand]
session['curra'] = session['questions'].get(codes[rand])
print(session['curra'])
return render_template("mostLikely.html", question=codes[rand], u1 = session['users'][0], u2 = session['users'][1])
#session['uname'] = request.form['uname']
#if session['uname'] == session['curra']:
# return render_template("results.html", message="Congrats, that's correct!")
@app.route("/results", methods=['GET', 'POST'])
def res():
session['uname'] = request.form['submitbutton']
print(session['uname'])
print(session['curra'])
#return render_template("results.html", message=message)
if session['uname'] == session['curra']:
return render_template("results.html", message="Congrats, that's correct!")
else:
return render_template("results.html", message="No, sorry, the actual answer was "+session['curra'])
#return render_template("results.html", message="No, sorry, the actual answer was "+session['curra'])