-
Notifications
You must be signed in to change notification settings - Fork 6
/
StockApp.py
110 lines (98 loc) · 3.35 KB
/
StockApp.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
#! /usr/bin/python3
import os
from flask import Flask, render_template, request, url_for
import stocks
import graph
import stocklist
from glob import glob
import json
app = Flask(__name__)
# routes to home page
@app.route('/')
def home():
for file in glob("./templates/*-graph.html"):
os.remove(file)
names = []
tickers = []
prices = []
stocklist.save_stock_tickers(names, tickers)
return render_template("home.html", title = 'home', stocknames = names, stocktickers = tickers)
# routes to trending page
@app.route('/trending')
def render_recommended():
file = open('Data/stockData.txt', 'r')
stockDict = json.loads(file.read())
file.close()
names = []
tickers = []
upvotes = []
for i in range(5):
maxticker = list(stockDict.keys())[0]
maxUpvote = list(stockDict.values())[0]
for key, value in stockDict.items():
if(isinstance(value, int) and value > maxUpvote):
maxticker = key
maxUpvote = value
tickers.append(maxticker)
names.append(stockDict[maxticker + '-name'])
upvotes.append(str(maxUpvote))
stockDict.pop(maxticker)
stockDict.pop(maxticker+'-name')
print(list(stockDict.keys())[0])
#print(names)
#print(tickers)
#print(upvotes)
return render_template("recommended.html", title = 'Trending',stocknames = names, stocktickers= tickers, upvotes = upvotes)
# routes to stocks page
@app.route('/mystocks')
def render_my_stocks():
return render_template("mystocks.html", title = 'mystocks')
# routes to about page
@app.route('/about')
def render_about():
return render_template("about.html", title = 'about')
# routes to upvoted
@app.route('/upvoted', methods = ['GET'])
def button_pressed():
stockabbrev = request.args.get('abbrev')
file = open('Data/stockData.txt', 'r')
stockDict = json.loads(file.read())
file.close()
stockDict[stockabbrev] = stockDict[stockabbrev] + 1
file = open('Data/stockData.txt', 'w')
file.write(json.dumps(stockDict))
file.close()
for file in glob("./templates/*-graph.html"): #get rid of all previous graph htmls <- temp fix
os.remove(file)
stockname = stockDict[stockabbrev + '-name']
file = open('Data/stockData.txt', 'r')
stockDict = json.loads(file.read())
file.close()
upvotes = stockDict[stockabbrev]
data = stocks.getChart(stockabbrev)
quote = stocks.getQuote(stockabbrev)
graph.makeGraph(stockname, stockabbrev, data, quote, str(upvotes))
sheet = stockabbrev + "-graph.html"
return render_template(stockabbrev + "-graph.html", title = 'stockinfo', display = 'Upvoted')
# routes to info
@app.route('/stockinfo', methods = ['GET']) #example http query: (home url)/stockinfo?name=Microsoft&abbrev=MSFT
def showInfo():
for file in glob("./templates/*-graph.html"): #get rid of all previous graph htmls <- temp fix
os.remove(file)
stockname = request.args.get('name')
stockabbrev = request.args.get('abbrev')
file = open('Data/stockData.txt', 'r')
stockDict = json.loads(file.read())
file.close()
upvotes = stockDict[stockabbrev]
data = stocks.getChart(stockabbrev)
quote = stocks.getQuote(stockabbrev)
graph.makeGraph(stockname, stockabbrev, data, quote, str(upvotes))
sheet = stockabbrev + "-graph.html"
return render_template(sheet, title = 'stockinfo', display = 'Upvote')
@app.route('/google973af8c591e84ad7.html')
def render_ver():
return render_template("google973af8c591e84ad7.html", title = 'verification')
if __name__ == "__main__":
port = int(os.environ.get('PORT', 33507))
app.run()