1 from bs4 import BeautifulSoup
2 from threading import Thread
11 import urllib.request, urllib.error, urllib.parse
14 class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
15 # format exchange:symbol
16 def __init__(self, name_to_timeout_dict, symbols):
17 super(stock_quote_renderer, self).__init__(name_to_timeout_dict, False)
18 self.symbols = symbols
19 self.prefix = "https://www.alphavantage.co/query?"
22 def debug_prefix(self):
25 def get_random_key(self):
26 return random.choice(secrets.alphavantage_keys)
28 def periodic_render(self, key):
29 now = datetime.datetime.now()
32 or now.hour >= (17 - 3)
33 or datetime.datetime.today().weekday() > 4
35 self.debug_print("The stock market is closed so not re-rendering")
38 if self.thread is None or not self.thread.is_alive():
39 self.debug_print("Spinning up a background thread...")
40 self.thread = Thread(target=self.thread_internal_render, args=())
44 def thread_internal_render(self):
46 f = file_writer.file_writer("stock_3_86400.html")
47 f.write("<H1>Stock Quotes</H1><HR>")
48 f.write("<TABLE WIDTH=99%>")
49 for symbol in self.symbols:
50 # print "---------- Working on %s\n" % symbol
52 # https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=<key>
54 # https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=<key>
59 key = self.get_random_key()
60 url = self.prefix + "function=GLOBAL_QUOTE&symbol=%s&apikey=%s" % (
64 raw = urllib.request.urlopen(url).read()
65 cooked = json.loads(raw)
66 if "Global Quote" not in cooked:
67 # print "%s\n" % cooked
69 "Failure %d, sleep %d sec...\n" % (attempts + 1, 2 ** attempts)
71 time.sleep(2 ** attempts)
73 if attempts > 10: # we'll wait up to 512 seconds per symbol
79 if "Global Quote" not in cooked:
80 print("Can't get data for symbol %s: %s\n" % (symbol, raw))
82 cooked = cooked["Global Quote"]
87 # u'01. symbol': u'MSFT',
88 # u'02. open': u'151.2900',
89 # u'03. high': u'151.8900',
90 # u'04. low': u'150.7650',
91 # u'05. price': u'151.1300',
92 # u'06. volume': u'16443559',
93 # u'07. latest trading day': u'2019-12-10',
94 # u'08. previous close': u'151.3600',
95 # u'09. change': u'-0.2300'
96 # u'10. change percent': u'-0.1520%',
101 if "05. price" in cooked:
102 price = cooked["05. price"]
105 percent_change = "?????"
106 if "10. change percent" in cooked:
107 percent_change = cooked["10. change percent"]
108 if not "-" in percent_change:
109 percent_change = "+" + percent_change
112 cell_color = "#bbbbbb"
113 if "09. change" in cooked:
114 change = cooked["09. change"]
116 cell_color = "#b00000"
118 cell_color = "#009000"
121 if symbols_finished % 4 == 0:
122 if symbols_finished > 0:
125 symbols_finished += 1
129 <TD WIDTH=20%% HEIGHT=150 BGCOLOR="%s">
131 <DIV style="position:relative;
134 <DIV style="position:absolute;
137 -webkit-transform:rotate(-90deg);
139 font-family: helvetica, arial, sans-serif;
141 -webkit-text-stroke: 2px black;
145 <!-- Current price, Change today and percent change today -->
146 <DIV style="position:absolute;
150 font-family: helvetica, arial, sans-serif;
158 % (cell_color, symbol, price, percent_change, change)
160 f.write("</TR></TABLE>")
165 # x = stock_quote_renderer({}, ["MSFT", "GOOG", "GOOGL", "OPTAX", "VNQ"])
166 # x.periodic_render(None)
167 # x.periodic_render(None)