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
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
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':
# }
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: