forked from vstempel/EmotionStockMarket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
277 lines (241 loc) · 10.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from flask import Flask
from flask import session,render_template,url_for,redirect,request
import urllib2,json
import googleauth,utils,otterapi
from bson import BSON, json_util
import time,threading
app = Flask(__name__)
app.secret_key="secret key" # Since we'll be using sessions
@app.route("/")
def index():
if not session.has_key('user'):
return redirect(url_for('login'))
return redirect(url_for("about"))
@app.route('/login',methods=['GET','POST'])
def login():
if request.method=="GET":
return render_template('login.html')
elif request.method=="POST":
if request.form['button']=='GOOGLE':
url=googleauth.build_redirect_url()
return redirect(url)
elif request.form['button']=='Login':
username=request.form['username']
utils.add_user(username)
session["user"]=username
return redirect(url_for("about"))
@app.route("/auth2callback")
def googleoauth2callback():
# this code comes from the google login page
code=request.args.get('code','')
# if we didn't get a code, go back to login
if code=='':
return redirect(url_for('login'))
# We have a code so we have to convert it to an access token
access_token = googleauth.code_to_access_token(code)
# and convert it to userinfo
userinfo=googleauth.access_token_to_info(access_token)
session['user']=userinfo['email']
utils.add_user(userinfo['email'])
return redirect(url_for('about'));
def updateStocks():
date=utils.get_date()
#print "here"
if (date == 0 or date < otterapi.create_times()[5]):
print [date,otterapi.create_times()[5]]
utils.update_date(otterapi.create_times()[5])
utils.update_market()
#threading.Timer(1,updateStocks).start()
@app.route("/logout")
def logout():
session.pop('user')
return redirect(url_for('login'))
@app.route('/getXcoords')
def getXcoords():
updateStocks()
return json.dumps(utils.get_market_x(),sort_keys=True,indent=4,default=json_util.default)
@app.route('/getYcoords')
def getYcoords():
updateStocks()
return json.dumps(utils.get_market_y(),sort_keys=True,indent=4,default=json_util.default)
@app.route('/getStocks/')
def getStocks():
updateStocks()
return json.dumps(utils.get_market(),sort_keys=True,indent=4,default=json_util.default)
@app.route('/stockNames')
def getStockNames():
return json.dumps(utils.get_stocks_names(),sort_keys=True,indent=4,default=json_util.default)
@app.route('/about', methods=["GET","POST"])
def about():
if not session.has_key('user'):
return render_template('about.html',loggedin=False)
else:
if request.method=="GET":
return render_template('about.html',loggedin=True)
@app.route('/profile',methods=["GET","POST"])
def profile():
if not session.has_key('user'):
return redirect(url_for('login'))
elif request.method=="GET":
d=session['user']
money=utils.get_money(d)
stock=utils.get_stocks(d)[0]
soul=utils.get_soul(d)
gain=utils.get_stocks(d)[1]
total=money+gain
return render_template('profile.html',d=d,money=money,stock=stock,soul=soul,gain=gain,total=total)
return redirect(url_for('profile'))
@app.route('/bank', methods=["GET","POST"])
def bank():
if not session.has_key('user'):
return redirect(url_for('login'))
d=session['user']
soul=utils.get_soul(d)
b=100-soul
money=utils.get_money(d)
if request.method=="GET":
return render_template('bank.html', d=d, soul=soul, b=b, money=money)
elif request.method=="POST":
button=request.form["button"]
if button=="Sell":
try:
amt_sell=int(request.form['selling'])
if amt_sell>100 or amt_sell<0 or amt_sell>soul:
return render_template("bank1.html",d=d,soul=soul,b=b,money=money)
value=utils.sell_soul(d,amt_sell)
return redirect(url_for("bank"))
except Exception:
return render_template("bank1.html",d=d,soul=soul,b=b,money=money)
elif button=="Buy":
try:
amt_buy=int(request.form['buying'])
if amt_buy>100 or amt_buy<0 or amt_buy>b:
return render_template("bank1.html",d=d,soul=soul,b=b,money=money)
value=utils.buy_soul(d,amt_buy)
return redirect(url_for("bank"))
except Exception:
return render_template("bank1.html",d=d,soul=soul,b=b,money=money)
return redirect(url_for("profile"))
@app.route('/graph',methods=["GET","POST"])
def graph():
if not session.has_key('user'):
return redirect(url_for('login'))
prices=utils.get_market_y()
sadp=prices["sad"][len(prices["sad"])-1]
boredp=prices["bored"][len(prices["bored"])-1]
lovep=prices["love"][len(prices["love"])-1]
tiredp=prices["tired"][len(prices["tired"])-1]
happyp=prices["happy"][len(prices["happy"])-1]
sickp=prices["sick"][len(prices["sick"])-1]
madp=prices["mad"][len(prices["mad"])-1]
boredi=utils.get_icon("bored")
sadi=utils.get_icon("sad")
lovei=utils.get_icon("love")
tiredi=utils.get_icon("tired")
happyi=utils.get_icon("happy")
sicki=utils.get_icon("sick")
madi=utils.get_icon("mad")
if request.method == "GET":
return render_template("graph.html",bored="bored",boredp=boredp,lovep=lovep,tiredp=tiredp,happyp=happyp,sickp=sickp,madp=madp,sadp=sadp,boredi=boredi[0],sadi=sadi[0],lovei=lovei[0],tiredi=tiredi[0],happyi=happyi[0],sicki=sicki[0],madi=madi[0],boredip=boredi[1],loveip=lovei[1],tiredip=tiredi[1],happyip=happyi[1],sickip=sicki[1],madip=madi[1],sadip=sadi[1]);
if request.method == "POST":
d=session['user']
value=request.form["button"]
value=value.split(" ")
if (str(value[1])=="buy"):
text=request.form[str(value[0])]
if (text!=""):
try:
text=int(text)
holder=utils.buy_stock(d,str(value[0]),int(text))
except Exception:
holder=False
if (str(value[1])=="sell"):
text=request.form[str(value[0])+" sold"]
if (text!=""):
try:
text=int(text)
holder=utils.sell_stock(d,str(value[0]),int(text))
except Exception:
holder=False
if (text=="" or holder == False):
return redirect(url_for("crash"))
else:
return redirect(url_for("transact"))
return render_template("graph.html",bored="bored",boredp=boredp,lovep=lovep,tiredp=tiredp,happyp=happyp,sickp=sickp,madp=madp,sadp=sadp);
'''
'''
@app.route("/crash", methods=['GET','POST'])
def crash():
if request.method == "GET":
prices=utils.get_market_y()
sadp=prices["sad"][len(prices["sad"])-1]
boredp=prices["bored"][len(prices["bored"])-1]
lovep=prices["love"][len(prices["love"])-1]
tiredp=prices["tired"][len(prices["tired"])-1]
happyp=prices["happy"][len(prices["happy"])-1]
sickp=prices["sick"][len(prices["sick"])-1]
madp=prices["mad"][len(prices["mad"])-1]
boredi=utils.get_icon("bored")
sadi=utils.get_icon("sad")
lovei=utils.get_icon("love")
tiredi=utils.get_icon("tired")
happyi=utils.get_icon("happy")
sicki=utils.get_icon("sick")
madi=utils.get_icon("mad")
return render_template("graph1.html",bored="bored",boredp=boredp,lovep=lovep,tiredp=tiredp,happyp=happyp,sickp=sickp,madp=madp,sadp=sadp,boredi=boredi[0],sadi=sadi[0],lovei=lovei[0],tiredi=tiredi[0],happyi=happyi[0],sicki=sicki[0],madi=madi[0],boredip=boredi[1],loveip=lovei[1],tiredip=tiredi[1],happyip=happyi[1],sickip=sicki[1],madip=madi[1],sadip=sadi[1]);
@app.route("/success",methods=["GET","POST"])
def transact():
if request.method == "GET":
prices=utils.get_market_y()
sadp=prices["sad"][len(prices["sad"])-1]
boredp=prices["bored"][len(prices["bored"])-1]
lovep=prices["love"][len(prices["love"])-1]
tiredp=prices["tired"][len(prices["tired"])-1]
happyp=prices["happy"][len(prices["happy"])-1]
sickp=prices["sick"][len(prices["sick"])-1]
madp=prices["mad"][len(prices["mad"])-1]
boredi=utils.get_icon("bored")
sadi=utils.get_icon("sad")
lovei=utils.get_icon("love")
tiredi=utils.get_icon("tired")
happyi=utils.get_icon("happy")
sicki=utils.get_icon("sick")
madi=utils.get_icon("mad")
return render_template("graph2.html",bored="bored",boredp=boredp,lovep=lovep,tiredp=tiredp,happyp=happyp,sickp=sickp,madp=madp,sadp=sadp,boredi=boredi[0],sadi=sadi[0],lovei=lovei[0],tiredi=tiredi[0],happyi=happyi[0],sicki=sicki[0],madi=madi[0],boredip=boredi[1],loveip=lovei[1],tiredip=tiredi[1],happyip=happyi[1],sickip=sicki[1],madip=madi[1],sadip=sadi[1]);
#Oauth
'''
@app.route("/")
def index():
if not session.has_key('user'):
return redirect(url_for('login'))
return render_template("index.html",d=session['user'])
@app.route("/login",methods=['GET','POST'])
def login():
if request.method=='GET':
return render_template('login.html')
elif request.form['button']=='GOOGLE':
#print "clicked"
url=googleauth.build_redirect_url()
return redirect(url)
@app.route("/logout")
def logout():
session.pop('user')
return redirect(url_for('login'))
@app.route("/auth2callback")
def googleoauth2callback():
# this code comes from the google login page
code=request.args.get('code','')
# if we didn't get a code, go back to login
if code=='':
return redirect(url_for('login'))
# We have a code so we have to convert it to an access token
access_token = googleauth.code_to_access_token(code)
# and convert it to userinfo
userinfo=googleauth.access_token_to_info(access_token)
session['user']=userinfo
print userinfo['email']
return redirect(url_for('index'));
'''
if __name__=="__main__":
app.debug=True
app.run(port=6007)