-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
298 lines (255 loc) · 10 KB
/
helpers.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import requests
import urllib.parse
import pyEX as p
import datetime
from numerize import numerize
from flask import redirect, render_template, request, session
from functools import wraps
if not os.environ.get('IEX_API_KEY'):
raise RuntimeError('IEX_API_KEY NOT SET')
IEX_API_KEY = os.environ['IEX_API_KEY']
if not os.environ.get('NEWS_API_KEY'):
raise RuntimeError('NEWS_API_KEY NOT SET')
NEWS_API_KEY = os.environ['NEWS_API_KEY']
api_one = p.Client(api_token='sk_bdf4a921914e4977b60e9b40fc9f1b3e') #whocares
api_two = p.Client(api_token='pk_13bea402dd284dd994c2a87b076d4d9f')#cities
news_key = p.Client(api_token=NEWS_API_KEY)
api_key = p.Client(api_token=IEX_API_KEY)
def apology(message, code=400):
"""Render message as an apology to user."""
def escape(s):
"""
Escape special characters.
https://github.com/jacebrowning/memegen#special-characters
"""
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"),
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]:
s = s.replace(old, new)
return s
return render_template("apology.html", top=code, bottom=escape(message)), code
def login_required(f):
"""
Decorate routes to require login.
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
# def lookup(symbol):
# """Look up quote for symbol."""
# # Contact API
# try:
# api_key = pk_a8218b82cc0b4e929be5cb4a3795e82c
# url = f"https://cloud.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}"
# response = requests.get(url)
# response.raise_for_status()
# except requests.RequestException:
# return None
# # Parse response
# try:
# quote = response.json()
# return {
# "name": quote["companyName"],
# "price": float(quote["latestPrice"]),
# "symbol": quote["symbol"]
# }
# except (KeyError, TypeError, ValueError):
# return None
def Most_active():
ma = api_key.list()[:9]
if not ma:
return None
return [
{
"symbol": ma[0]["symbol"],
"companyName": ma[0]["companyName"],
"latestPrice": round(ma[0]["latestPrice"], 2),
"changePercent": round(ma[0]["changePercent"] * 100, 2)
},
{
"symbol": ma[1]["symbol"],
"companyName": ma[1]["companyName"],
"latestPrice": round(ma[1]["latestPrice"], 2),
"changePercent": round(ma[1]["changePercent"] * 100, 2)
},
{
"symbol": ma[2]["symbol"],
"companyName": ma[2]["companyName"],
"latestPrice": round(ma[2]["latestPrice"], 2),
"changePercent": round(ma[2]["changePercent"] * 100, 2)
},
{
"symbol": ma[3]["symbol"],
"companyName": ma[3]["companyName"],
"latestPrice": round(ma[3]["latestPrice"], 2),
"changePercent": round(ma[3]["changePercent"] * 100, 2)
},
{
"symbol": ma[4]["symbol"],
"companyName": ma[4]["companyName"],
"latestPrice": round(ma[4]["latestPrice"], 2),
"changePercent": round(ma[4]["changePercent"] * 100, 2)
},
{
"symbol": ma[5]["symbol"],
"companyName": ma[5]["companyName"],
"latestPrice": round(ma[5]["latestPrice"], 2),
"changePercent": round(ma[5]["changePercent"] * 100, 2)
},
{
"symbol": ma[6]["symbol"],
"companyName": ma[6]["companyName"],
"latestPrice": round(ma[6]["latestPrice"], 2),
"changePercent": round(ma[6]["changePercent"] * 100, 2)
},
{
"symbol": ma[7]["symbol"],
"companyName": ma[7]["companyName"],
"latestPrice": round(ma[7]["latestPrice"], 2),
"changePercent": round(ma[7]["changePercent"] * 100, 2)
},
{
"symbol": ma[8]["symbol"],
"companyName": ma[8]["companyName"],
"latestPrice": round(ma[8]["latestPrice"], 2),
"changePercent": round(ma[8]["changePercent"] * 100, 2)
}
]
def is_market_open():
status = api_key.quote('aapl')
return status["isUSMarketOpen"]
def get_stock_price(symbol,n):
return round(api_key.quote(symbol)["latestPrice"], n)
def get_stock_info(symbol):
stock_info = api_key.quote(symbol)
if stock_info["latestVolume"] is None:
stock_info["latestVolume"] = "---"
else:
stock_info["latestVolume"] = "{:,}".format(stock_info["latestVolume"])
return {
"Symbol": symbol,
"CompanyName": stock_info["companyName"],
"Market Cap": numerize.numerize(stock_info["marketCap"]),
"PE Ratio (TTM)": stock_info["peRatio"],
"52 week High": stock_info["week52High"],
"52 week Low": stock_info["week52Low"],
"YTD Change": round(stock_info["ytdChange"] * 100, 2),
"Volume": stock_info["latestVolume"],
"LatestPrice": round(stock_info["latestPrice"], 2),
"Change": stock_info["change"],
"ChangePercent": round(stock_info["changePercent"] * 100, 2)
}
def usd(value):
"""Format value as USD."""
return f"${value:,.2f}"
def news(symbol):
try:
articles = news_key.news(symbol)[:3]
return [
{
"Date": datetime.datetime.fromtimestamp(articles[0]["datetime"]/1000).strftime("%B %d"),
"ImageUrl": articles[0]["image"],
"Headline": articles[0]["headline"],
"Summary": articles[0]["summary"][0:120],
"Url": articles[0]["url"]
},
{
"Date": datetime.datetime.fromtimestamp(articles[1]["datetime"]/1000).strftime("%B %d"),
"ImageUrl": articles[1]["image"],
"Headline": articles[1]["headline"],
"Summary": articles[1]["summary"][0:120],
"Url": articles[1]["url"]
},
{
"Date": datetime.datetime.fromtimestamp(articles[2]["datetime"]/1000).strftime("%B %d"),
"ImageUrl": articles[2]["image"],
"Headline": articles[2]["headline"],
"Summary": articles[2]["summary"][0:120],
"Url": articles[2]["url"]
}
]
except:
return None
def getOneDayChart(symbol):
labels = []
data = []
chart = {
"labels":[],
"data":[]
}
url = f"https://cloud.iexapis.com/stable/stock/{symbol}/intraday-prices/chartIEXOnly=true/?token={IEX_API_KEY}"
response = requests.get(url).json()
if not response:
api_key = 'SAOS0Y8B63XM4DPK'
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=1min&apikey={api_key}"
response = requests.get(url).json()["Time Series (1min)"]
for label in response["Time Series (1min)"]:
labels.append(label.split(" ")[1][:5])
for label in response["Time Series (1min)"]:
data.append(round(float(response["Time Series (1min)"][label]["4. close"]),2))
for label in labels:
chart["labels"].append(label)
for dat in data:
chart["data"].append(dat)
chart["labels"].reverse()
chart["data"].reverse()
return chart
for ever_minute in response:
if ever_minute["close"] is not None:
labels.append(ever_minute["minute"])
data.append(ever_minute["close"])
chart["labels"] = labels
chart["data"] = data
return chart
def getTopGainers():
url = f"https://cloud.iexapis.com/stable/stock/market/list/gainers/?token={IEX_API_KEY}"
res = requests.get(url)
stocks = res.json()
topGainers = []
for stock in stocks:
if stock is not None and stock["change"] is not None:
topGainers.append({
"Symbol": stock["symbol"],
"CompanyName": stock["companyName"],
"LatestPrice": round(stock["latestPrice"],2),
"Change": round(stock["change"],2),
"52 Week High": round(stock["week52High"],2),
"52 Week Low": round(stock["week52Low"],2)
})
return topGainers[:7]
def getTopLosers():
url = f"https://cloud.iexapis.com/stable/stock/market/list/losers/?token={IEX_API_KEY}"
res = requests.get(url)
stocks = res.json()
topLosers = []
for stock in stocks:
if stock is not None and stock["change"] is not None:
topLosers.append({
"Symbol": stock["symbol"],
"CompanyName": stock["companyName"],
"LatestPrice": round(stock["latestPrice"],2),
"Change": round(stock["change"],2),
"52 Week High": round(stock["week52High"],2),
"52 Week Low": round(stock["week52Low"],2)
})
return topLosers[:7]
def getTopVolume():
url = f"https://cloud.iexapis.com/stable/stock/market/list/iexvolume/?token={IEX_API_KEY}"
res = requests.get(url)
stocks = res.json()
topVolume = []
for stock in stocks:
if stock is not None and stock["change"] is not None:
topVolume.append({
"Symbol": stock["symbol"],
"CompanyName": stock["companyName"],
"LatestPrice": round(stock["latestPrice"],2),
"Change": round(stock["change"],2),
"52 Week High": round(stock["week52High"],2),
"52 Week Low": round(stock["week52Low"],2)
})
return topVolume[:7]