-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stonk.py
34 lines (30 loc) · 1.52 KB
/
Stonk.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
import http.client
import json
class Stonk:
def __init__(self, ticker):
self.ticker = ticker
self.currentPrice = None
self.previousClosePrice = None
self.pChange = None
# self.updateStockInfo()
def updateStockInfo(self):
try:
conn = http.client.HTTPSConnection("query1.finance.yahoo.com")
conn.request("GET", "/v8/finance/chart/" + self.ticker +
"?region=US&lang=en-US&includePrePost=false&interval=2m&useYfid=true&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance")
jsonData = json.loads(conn.getresponse().read())
self.currentPrice = jsonData["chart"]["result"][0]["meta"]["regularMarketPrice"]
self.previousClosePrice = jsonData["chart"]["result"][0]["meta"]["previousClose"]
self.pChange = (
(self.currentPrice - self.previousClosePrice) / self.previousClosePrice) * 100
except Exception as e:
print('\033[91m' + "ERROR: Failed to get " +
self.ticker + " info: " + str(e) + '\033[0m')
def __str__(self):
# self.updateStockInfo()
if self.pChange < 0:
return (self.ticker + ": $%.2f" % self.currentPrice + " %.2f" % self.pChange + "%")
elif self.pChange > 0:
return (self.ticker + ": $%.2f" % self.currentPrice + " %.2f" % self.pChange + "%")
else:
return (self.ticker + ": $%.2f" % self.currentPrice + " %.2f" % self.pChange + "%")