Format codebase w/ black.
[kiosk.git] / stock_renderer.py
index f8491e6a9eb73b8ba06ea35ffd1eb96f5ca0b5c2..7b34455eb610283946eeb37a9db571449b9e3c22 100644 (file)
@@ -10,6 +10,7 @@ import secrets
 import time
 import urllib.request, urllib.error, urllib.parse
 
+
 class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
     # format exchange:symbol
     def __init__(self, name_to_timeout_dict, symbols):
@@ -26,25 +27,27 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
 
     def periodic_render(self, key):
         now = datetime.datetime.now()
-        if (now.hour < (9 - 3) or
-            now.hour >= (17 - 3) or
-            datetime.datetime.today().weekday() > 4):
+        if (
+            now.hour < (9 - 3)
+            or now.hour >= (17 - 3)
+            or datetime.datetime.today().weekday() > 4
+        ):
             self.debug_print("The stock market is closed so not re-rendering")
             return True
 
-        if (self.thread is None or not self.thread.is_alive()):
+        if self.thread is None or not self.thread.is_alive():
             self.debug_print("Spinning up a background thread...")
-            self.thread = Thread(target = self.thread_internal_render, args=())
+            self.thread = Thread(target=self.thread_internal_render, args=())
             self.thread.start()
         return True
 
     def thread_internal_render(self):
         symbols_finished = 0
-        f = file_writer.file_writer('stock_3_86400.html')
+        f = file_writer.file_writer("stock_3_86400.html")
         f.write("<H1>Stock Quotes</H1><HR>")
         f.write("<TABLE WIDTH=99%>")
         for symbol in self.symbols:
-#            print "---------- Working on %s\n" % symbol
+            #            print "---------- Working on %s\n" % symbol
 
             # https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=<key>
 
@@ -54,26 +57,29 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
             cooked = ""
             while True:
                 key = self.get_random_key()
-                url = self.prefix + "function=GLOBAL_QUOTE&symbol=%s&apikey=%s" % (symbol, key)
+                url = self.prefix + "function=GLOBAL_QUOTE&symbol=%s&apikey=%s" % (
+                    symbol,
+                    key,
+                )
                 raw = urllib.request.urlopen(url).read()
                 cooked = json.loads(raw)
-                if 'Global Quote' not in cooked:
-#                    print "%s\n" % cooked
-                    print("Failure %d, sleep %d sec...\n" % (attempts + 1,
-                                                             2 ** attempts))
+                if "Global Quote" not in cooked:
+                    #                    print "%s\n" % cooked
+                    print(
+                        "Failure %d, sleep %d sec...\n" % (attempts + 1, 2 ** attempts)
+                    )
                     time.sleep(2 ** attempts)
                     attempts += 1
-                    if attempts > 10: # we'll wait up to 512 seconds per symbol
+                    if attempts > 10:  # we'll wait up to 512 seconds per symbol
                         break
                 else:
                     break
 
             # These fuckers...
-            if 'Global Quote' not in cooked:
-                print("Can't get data for symbol %s: %s\n" % (
-                    symbol, raw))
+            if "Global Quote" not in cooked:
+                print("Can't get data for symbol %s: %s\n" % (symbol, raw))
                 continue
-            cooked = cooked['Global Quote']
+            cooked = cooked["Global Quote"]
 
             # {
             #   u'Global Quote':
@@ -92,20 +98,20 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
             # }
 
             price = "?????"
-            if '05. price' in cooked:
-                price = cooked['05. price']
+            if "05. price" in cooked:
+                price = cooked["05. price"]
                 price = price[:-2]
 
             percent_change = "?????"
-            if '10. change percent' in cooked:
-                percent_change = cooked['10. change percent']
-                if not '-' in percent_change:
+            if "10. change percent" in cooked:
+                percent_change = cooked["10. change percent"]
+                if not "-" in percent_change:
                     percent_change = "+" + percent_change
 
             change = "?????"
             cell_color = "#bbbbbb"
-            if '09. change' in cooked:
-                change = cooked['09. change']
+            if "09. change" in cooked:
+                change = cooked["09. change"]
                 if "-" in change:
                     cell_color = "#b00000"
                 else:
@@ -113,12 +119,13 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
                 change = change[:-2]
 
             if symbols_finished % 4 == 0:
-                if (symbols_finished > 0):
+                if symbols_finished > 0:
                     f.write("</TR>")
                 f.write("<TR>")
             symbols_finished += 1
 
-            f.write("""
+            f.write(
+                """
 <TD WIDTH=20%% HEIGHT=150 BGCOLOR="%s">
   <!-- Container -->
   <DIV style="position:relative;
@@ -147,15 +154,14 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
             <B>$%s</B>
     </DIV>
   </DIV>
-</TD>""" % (cell_color,
-            symbol,
-            price,
-            percent_change,
-            change))
+</TD>"""
+                % (cell_color, symbol, price, percent_change, change)
+            )
         f.write("</TR></TABLE>")
         f.close()
         return True
 
-#x = stock_quote_renderer({}, ["MSFT", "GOOG", "GOOGL", "OPTAX", "VNQ"])
-#x.periodic_render(None)
-#x.periodic_render(None)
+
+# x = stock_quote_renderer({}, ["MSFT", "GOOG", "GOOGL", "OPTAX", "VNQ"])
+# x.periodic_render(None)
+# x.periodic_render(None)