More changes for python3 and improved logging/debugging. This ~works.
[kiosk.git] / stock_renderer.py
index 5a9be493e5aa9079d6c34ccd370ab9413a5be827..f8491e6a9eb73b8ba06ea35ffd1eb96f5ca0b5c2 100644 (file)
@@ -8,7 +8,7 @@ import renderer
 import random
 import secrets
 import time
-import urllib2
+import urllib.request, urllib.error, urllib.parse
 
 class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
     # format exchange:symbol
@@ -55,12 +55,12 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
             while True:
                 key = self.get_random_key()
                 url = self.prefix + "function=GLOBAL_QUOTE&symbol=%s&apikey=%s" % (symbol, key)
-                raw = urllib2.urlopen(url).read()
+                raw = urllib.request.urlopen(url).read()
                 cooked = json.loads(raw)
-                if u'Global Quote' not in cooked:
+                if 'Global Quote' not in cooked:
 #                    print "%s\n" % cooked
-                    print "Failure %d, sleep %d sec...\n" % (attempts + 1,
-                                                             2 ** attempts)
+                    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
@@ -69,11 +69,11 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
                     break
 
             # These fuckers...
-            if u'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[u'Global Quote']
+            cooked = cooked['Global Quote']
 
             # {
             #   u'Global Quote':
@@ -92,20 +92,20 @@ class stock_quote_renderer(renderer.debuggable_abstaining_renderer):
             # }
 
             price = "?????"
-            if u'05. price' in cooked:
-                price = cooked[u'05. price']
+            if '05. price' in cooked:
+                price = cooked['05. price']
                 price = price[:-2]
 
             percent_change = "?????"
-            if u'10. change percent' in cooked:
-                percent_change = cooked[u'10. change percent']
+            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 u'09. change' in cooked:
-                change = cooked[u'09. change']
+            if '09. change' in cooked:
+                change = cooked['09. change']
                 if "-" in change:
                     cell_color = "#b00000"
                 else: