forked from efitz11/natsgifbot
/
stocks.py
119 lines (115 loc) · 4.61 KB
/
stocks.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
from urllib.request import urlopen, Request
import json
import utils
from bs4 import BeautifulSoup
def get_quote(symbol):
# url = "https://api.iextrading.com/1.0/stock/"+symbol+"/quote?displayPercent=true"
token = utils.get_keys("iex")['public']
url = "https://cloud.iexapis.com/stable/stock/%s/quote?displayPercent=true&token=%s" % (symbol, token)
print(url)
req = Request(url)
req.headers["User-Agent"] = "windows 10 bot"
# Load data
quote = json.loads(urlopen(req).read().decode("utf-8"))
try:
change = float(quote['change'])
ch = "%0.2f" %(change)
chper = "%0.2f" %(quote['changePercent'])
chytd = "%0.2f" % (quote['ytdChange'])
except TypeError:
change = "n/a"
ch = "n/a"
chper = "n/a"
chytd = "n/a"
mcap = quote['marketCap']
if mcap >= 1e12:
cap = round(mcap/1e12, 1)
cap = str(cap) + "T"
elif mcap >= 1e9:
cap = round(mcap/1e9, 1)
cap = str(cap) + "B"
elif mcap >= 1e6:
cap = round(mcap/1e6, 1)
cap = str(cap) + "M"
elif mcap >= 1e3:
cap = str(round(mcap/1e3,1))+ "k"
else:
cap = str(mcap)
if change != "n/a" and change > 0:
ch = "+" + ch
chper = "+" + chper
output = "%s - %s:```python\n Last price: %s (%s, %s%%, %s%% YTD" % \
(symbol.upper(),quote['companyName'],quote['latestPrice'],ch,chper,chytd)+")"
output = output + " %s mkt cap\n" % cap
output = output + " 52w high: %.02f\t52w low:%.02f" % (quote['week52High'], quote['week52Low'])
output = output + "```"
return output
def get_stocks():
output = "Latest quotes:\n```python\n"
stocks = []
token = utils.get_keys("iex")['public']
for symbol in ["DIA","VOO","VTI","ONEQ","VXUS"]:
url = "https://cloud.iexapis.com/stable/stock/%s/quote?displayPercent=true&token=%s" % (symbol, token)
# url = "https://api.iextrading.com/1.0/stock/"+symbol+"/quote?displayPercent=true"
req = Request(url)
req.headers["User-Agent"] = "windows 10 bot"
# Load data
quote = json.loads(urlopen(req).read().decode("utf-8"))
stock = dict()
change = float(quote['change'])
ch = "%0.2f" %(change)
chper = "%0.2f" %(quote['changePercent'])
chytd = "%0.2f" % (quote['ytdChange'])
if change > 0:
ch = "+" + ch
chper = "+" + chper
stock['symbol'] = symbol.upper()
stock['price'] = "%.2f" % float(quote['latestPrice'])
stock['change'] = ch
stock['%'] = chper
stock['% YTD'] = chytd
stock['description'] = quote['companyName']
stock['high52w'] = quote['week52High']
stock['low52w'] = quote['week52Low']
stocks.append(stock)
# output = output + "%s - %s (%s, %s%%, %s%% YTD) - %s\n" % (symbol.upper(),quote['latestPrice'],ch,chper,chytd,quote['companyName'])
# output = "%s - %s:```python\n Last price: %s (%s, %s%%, %s%% YTD" % (symbol.upper(),quote['companyName'],quote['latestPrice'],ch,chper,chytd)+")"
labels = ['symbol','price','change','%','% YTD','description']
left = ['symbol','description']
output = output + utils.format_table(labels, stocks, left_list=left)
output = output + "```"
return output
def get_indexes():
indexes = {'Dow','S&P 500','Nasdaq'}
labels = ['Index','Last','Change','%']
left = [labels[0]]
url = "https://money.cnn.com/data/markets"
req = Request(url, headers={'User-Agent' : "ubuntu"})
data = urlopen(req).read().decode('utf-8')
soup = BeautifulSoup(data, 'html.parser')
l = soup.find("ul", class_="three-equal-columns wsod")
# print(l)
items = l.findAll('li')
rows = list()
for item in items:
ticker = dict()
ticker['Index'] = item.find("span", class_="ticker-name").get_text()
ticker['Last'] = item.find("span", class_="ticker-points").get_text()
ticker['%'] = item.find("span", class_="ticker-name-change").get_text()
ticker['Change'] = item.find("span", class_="ticker-points-change").get_text()
rows.append(ticker)
# table_data = [[cell.text.strip() for cell in row("td")]
# for row in table("tr")]
# rows = []
# for row in table_data:
# if row[1] in indexes:
# idx = {}
# idx[labels[0]] = row[1]
# idx[labels[1]] = row[2]
# idx[labels[2]] = row[3]
# idx[labels[3]] = row[4]
# rows.append(idx)
return "```%s```" % utils.format_table(labels,rows, left_list=left)
if __name__ == "__main__":
# print(get_quote("msft"))
print(get_indexes())