From 88cb06cd55e8f933efb0c77cd8be3637ec1b9961 Mon Sep 17 00:00:00 2001 From: Ioan Loosley Date: Tue, 5 Jul 2016 19:00:13 +0100 Subject: [PATCH] Made the process for getting the currency more effecent using the bitcoinaverage.com api to make the downloading and the processing more efficient by only downloading the data for the currency, this code should work --- gbot.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/gbot.py b/gbot.py index 23a3723..aa9f231 100755 --- a/gbot.py +++ b/gbot.py @@ -131,16 +131,28 @@ def listusr(info,users): print(users) def btc(info,usrs): money = 0 - cur = 'USD' msg = info['msg'].split() - url = "https://api.bitcoinaverage.com/ticker/global/all" - req = urllib.request.urlopen(url) - resp = req.read() - data = json.loads(resp.decode('utf8')) if(len(msg) > 0): - if(msg[0] in data): - cur = msg[0] - say(info['user'] + ": 1 BTC = " + str(data[cur]['ask']) + " " + cur) + cur = msg[0].upper() + else: + cur = "USD" + try: + url = "https://api.bitcoinaverage.com/ticker/global/"+cur + req = urllib.request.urlopen(url) + resp = req.read() + data = json.loads(resp.decode('utf8')) + + say(info['user'] + ": 1 BTC = " + str(data['ask']) + " " + cur) + except urllib.request.HTTPerror as e: + if e.code == 404: + say("Bitcoin Average does not have any data on that currency") + elif e.code == 500: + say("bitcoinaverage.com had a server error") + else: + say("I got a " + str(e.code) + " from the server") + except: + say("A error happened") + def lenny(info,usrs): usr = "" msg = info['msg'].split()