From 4c2bc8c1d309e854b99a3f6de3a32595ebe825a9 Mon Sep 17 00:00:00 2001 From: Neil Innes Date: Mon, 14 Jun 2021 23:25:30 +0100 Subject: [PATCH 1/6] Direct Kraken Exchange API price lookups --- btcticker.py | 78 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/btcticker.py b/btcticker.py index 99c556d..b87f378 100644 --- a/btcticker.py +++ b/btcticker.py @@ -83,24 +83,49 @@ def getData(config,other): The function to update the ePaper display. There are two versions of the layout. One for portrait aspect ratio, one for landscape. """ headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} - whichcoin,fiat=configtocoinandfiat(config) + whichcoin,krakenpair,fiat=configtocoinandfiat(config) logging.info("Getting Data") days_ago=int(config['ticker']['sparklinedays']) endtime = int(time.time()) starttime = endtime - 60*60*24*days_ago starttimeseconds = starttime endtimeseconds = endtime - # Get the price + # Get the price + + geckourlhistorical = "https://api.coingecko.com/api/v3/coins/"+whichcoin+"/market_chart/range?vs_currency="+fiat+"&from="+str(starttimeseconds)+"&to="+str(endtimeseconds) + logging.info(geckourlhistorical) + rawtimeseries = requests.get(geckourlhistorical, headers=headers).json() + logging.info("Got price for the last "+str(days_ago)+" days from CoinGecko") + timeseriesarray = rawtimeseries['prices'] + timeseriesstack = [] + length=len (timeseriesarray) + i=0 + while i < length: + timeseriesstack.append(float (timeseriesarray[i][1])) + i+=1 + # A little pause before hiting the api again + time.sleep(1) + if config['ticker']['exchange']=='default': geckourl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency="+fiat+"&ids="+whichcoin logging.info(geckourl) rawlivecoin = requests.get(geckourl, headers=headers).json() - logging.info(rawlivecoin[0]) + logging.info(rawlivecoin[0]) liveprice = rawlivecoin[0] pricenow= float(liveprice['current_price']) alltimehigh = float(liveprice['ath']) other['market_cap_rank'] = int(liveprice['market_cap_rank']) other['volume'] = float(liveprice['total_volume']) + elif config['ticker']['exchange']=='kraken': + geckourl="https://api.kraken.com/0/public/Ticker?pair="+krakenpair; + logging.info(geckourl) + rawlivecoin = requests.get(geckourl, headers=headers).json() + logging.info(rawlivecoin['result']) + liveprice = rawlivecoin['result'][krakenpair] + pricenow= float(liveprice['c'][0]) + alltimehigh = 1000000.0 # For non-default the ATH does not show in the API, so show it when price reaches *pinky in mouth* ONE MILLION DOLLARS + other['market_cap_rank'] = 0 # For non-default the Rank does not show in the API, so leave blank + other['volume'] = float(liveprice['v'][1])*pricenow else: geckourl= "https://api.coingecko.com/api/v3/exchanges/"+config['ticker']['exchange']+"/tickers?coin_ids="+whichcoin+"&include_exchange_logo=false" logging.info(geckourl) @@ -122,19 +147,7 @@ def getData(config,other): other['volume'] = float(liveprice['converted_volume']['usd']) alltimehigh = 1000000.0 # For non-default the ATH does not show in the API, so show it when price reaches *pinky in mouth* ONE MILLION DOLLARS logging.info("Got Live Data From CoinGecko") - geckourlhistorical = "https://api.coingecko.com/api/v3/coins/"+whichcoin+"/market_chart/range?vs_currency="+fiat+"&from="+str(starttimeseconds)+"&to="+str(endtimeseconds) - logging.info(geckourlhistorical) - # A little pause before hiting the api again - time.sleep(5) - rawtimeseries = requests.get(geckourlhistorical, headers=headers).json() - logging.info("Got price for the last "+str(days_ago)+" days from CoinGecko") - timeseriesarray = rawtimeseries['prices'] - timeseriesstack = [] - length=len (timeseriesarray) - i=0 - while i < length: - timeseriesstack.append(float (timeseriesarray[i][1])) - i+=1 + timeseriesstack.append(pricenow) if pricenow>alltimehigh: other['ATH']=True @@ -191,7 +204,7 @@ def updateDisplay(config,pricestack,other): originalcoin=originalconfig['ticker']['currency'] originalcoin_list = originalcoin.split(",") originalcoin_list = [x.strip(' ') for x in originalcoin_list] - whichcoin,fiat=configtocoinandfiat(config) + whichcoin,krakenpair,fiat=configtocoinandfiat(config) days_ago=int(config['ticker']['sparklinedays']) symbolstring=currency.symbol(fiat.upper()) if fiat=="jpy" or fiat=="cny": @@ -244,16 +257,15 @@ def updateDisplay(config,pricestack,other): if other['ATH']==True: image.paste(ATHbitmap,(190,85)) draw.text((110,90),str(days_ago)+" day : "+pricechange,font =font_date,fill = 0) - # uncomment the line below to show volume - #draw.text((110,105),"24h vol : " + human_format(other['volume']),font =font_date,fill = 0) + if 'showvolume' in config['display'] and config['display']['showvolume']: + draw.text((110,105),"24h vol : " + human_format(other['volume']),font =font_date,fill = 0) writewrappedlines(image, symbolstring+pricenowstring,50,55,8,10,"Roboto-Medium" ) image.paste(sparkbitmap,(80,40)) image.paste(tokenimage, (0,10)) - # uncomment the lines below to show rank - #if other['market_cap_rank'] > 0: - # draw.text((10,105),"Rank: " + str("%d" % other['market_cap_rank']),font =font_date,fill = 0) + if 'showrank' in config['display'] and config['display']['showrank'] and other['market_cap_rank'] > 0: + draw.text((10,105),"Rank: " + str("%d" % other['market_cap_rank']),font =font_date,fill = 0) if (config['display']['trendingmode']==True) and not (str(whichcoin) in originalcoin_list): writewrappedlines(image, whichcoin,11,24,8,25,"PixelSplitter-Bold" ) @@ -332,7 +344,7 @@ def fullupdate(config,lastcoinfetch): time.sleep(0.2) except Exception as e: message="Data pull/print problem" - image=beanaproblem(str(e)) + image=beanaproblem(str(e)+" Line: "+str(e.__traceback__.tb_lineno)) display_image(image) time.sleep(20) lastgrab=lastcoinfetch @@ -340,10 +352,12 @@ def fullupdate(config,lastcoinfetch): def configtocoinandfiat(config): crypto_list = currencystringtolist(config['ticker']['currency']) + kraken_list = currencystringtolist(config['ticker']['krakenpairs']) fiat_list=currencystringtolist(config['ticker']['fiatcurrency']) currency=crypto_list[0] + krakenpair=kraken_list[0] fiat=fiat_list[0] - return currency, fiat + return currency, krakenpair, fiat def gettrending(config): print("ADD TRENDING") @@ -359,8 +373,12 @@ def gettrending(config): config['ticker']['currency']=coinlist return config -def main(): - logging.basicConfig(level=logging.DEBUG) +def main(): + loglevel = logging.WARNING + # To debug, uncomment this line + #loglevel = logging.DEBUG + + logging.basicConfig(level=loglevel) try: logging.info("epd2in7 BTC Frame") # Get the configuration from config.yaml @@ -419,8 +437,12 @@ def main(): datapulled = True except IOError as e: logging.info(e) - image=beanaproblem(str(e)) - display_image(image) + image=beanaproblem(str(e)+" Line: "+str(e.__traceback__.tb_lineno)) + display_image(image) + except Exception as e: + logging.info(e) + image=beanaproblem(str(e)+" Line: "+str(e.__traceback__.tb_lineno)) + display_image(image) except KeyboardInterrupt: logging.info("ctrl + c:") image=beanaproblem("Keyboard Interrupt") From f1dbfe117d6554d2474dcb7bb60b5a2753e6ac80 Mon Sep 17 00:00:00 2001 From: Neil Innes Date: Mon, 14 Jun 2021 23:29:17 +0100 Subject: [PATCH 2/6] Add krakenpairs config --- config.yaml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/config.yaml b/config.yaml index f347351..a49be15 100644 --- a/config.yaml +++ b/config.yaml @@ -3,10 +3,14 @@ display: inverted: false orientation: 90 trendingmode: false + showvolume: false + showrank: false ticker: currency: bitcoin,ethereum,cardano - exchange: default fiatcurrency: usd,eur,gbp + # krakenpairs is only required if using kraken as the exchange, this comma seperated list must match the currency and fiatcurrency list + # A full list of support Kraken pairs can be found here: https://support.kraken.com/hc/en-us/articles/360000920306-Ticker-pairs + krakenpairs: XXBTZUSD, XETHZUSD, ADAUSD + exchange: default sparklinedays: 1 updatefrequency: 300 - From 007a6bb522e61d6ed2336df75a1967447c2f8fa3 Mon Sep 17 00:00:00 2001 From: Neil Innes Date: Thu, 8 Jul 2021 21:23:16 +0100 Subject: [PATCH 3/6] Update btcticker.py --- btcticker.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/btcticker.py b/btcticker.py index c11eeac..df6f516 100644 --- a/btcticker.py +++ b/btcticker.py @@ -86,13 +86,21 @@ def getgecko(url): geckojson={} return geckojson, connectfail +def convertkraken(whichcoin,fiat): + if (whichcoin == "bitcoin"): + krakenpair = "BTC" + fiat.upper() + else: + krakenpair = whichcoin[:3] + fiat.upper() + + return krakenpair + def getData(config,other): """ The function to grab the data (TO DO: need to test properly) """ sleep_time = 10 num_retries = 5 - whichcoin,krakenpair,fiat=configtocoinandfiat(config) + whichcoin,fiat=configtocoinandfiat(config) logging.info("Getting Data") days_ago=int(config['ticker']['sparklinedays']) endtime = int(time.time()) @@ -141,6 +149,7 @@ def getData(config,other): else: other['ATH']=False elif config['ticker']['exchange']=='kraken': + krakenpair = convertkraken(whichcoin,fiat) geckourl="https://api.kraken.com/0/public/Ticker?pair="+krakenpair; logging.info(geckourl) rawlivecoin , connectfail = getgecko(geckourl) @@ -148,7 +157,8 @@ def getData(config,other): pass else: logging.info(rawlivecoin['result']) - liveprice = rawlivecoin['result'][krakenpair] + first_key = list(rawlivecoin['result'].keys())[0] + liveprice = rawlivecoin['result'][first_key] pricenow= float(liveprice['c'][0]) alltimehigh = 1000000.0 config['display']['showrank']=False @@ -239,7 +249,7 @@ def updateDisplay(config,pricestack,other): originalcoin=originalconfig['ticker']['currency'] originalcoin_list = originalcoin.split(",") originalcoin_list = [x.strip(' ') for x in originalcoin_list] - whichcoin,krakenpair,fiat=configtocoinandfiat(config) + whichcoin,fiat=configtocoinandfiat(config) days_ago=int(config['ticker']['sparklinedays']) symbolstring=currency.symbol(fiat.upper()) if fiat=="jpy" or fiat=="cny": @@ -394,12 +404,10 @@ def fullupdate(config,lastcoinfetch): def configtocoinandfiat(config): crypto_list = currencystringtolist(config['ticker']['currency']) - kraken_list = currencystringtolist(config['ticker']['krakenpairs']) fiat_list=currencystringtolist(config['ticker']['fiatcurrency']) currency=crypto_list[0] - krakenpair=kraken_list[0] fiat=fiat_list[0] - return currency, krakenpair, fiat + return currency, fiat def gettrending(config): print("ADD TRENDING") @@ -437,9 +445,9 @@ def main(loglevel=logging.WARNING): # Time of start lastcoinfetch = time.time() # Quick Sanity check on update frequency, waveshare says no faster than 180 seconds - if float(config['ticker']['updatefrequency'])<180: - logging.info("Throttling update frequency to 180 seconds") - updatefrequency=180.0 + if float(config['ticker']['updatefrequency'])<30: + logging.info("Throttling update frequency to 30 seconds") + updatefrequency=30.0 else: updatefrequency=float(config['ticker']['updatefrequency']) while internet() ==False: From 4568a4cae709a8b2e6e5cad2cebf8e0d66f144eb Mon Sep 17 00:00:00 2001 From: Neil Innes Date: Thu, 8 Jul 2021 21:24:32 +0100 Subject: [PATCH 4/6] Update config.yaml --- config.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/config.yaml b/config.yaml index a49be15..9bbf7ee 100644 --- a/config.yaml +++ b/config.yaml @@ -7,10 +7,7 @@ display: showrank: false ticker: currency: bitcoin,ethereum,cardano - fiatcurrency: usd,eur,gbp - # krakenpairs is only required if using kraken as the exchange, this comma seperated list must match the currency and fiatcurrency list - # A full list of support Kraken pairs can be found here: https://support.kraken.com/hc/en-us/articles/360000920306-Ticker-pairs - krakenpairs: XXBTZUSD, XETHZUSD, ADAUSD exchange: default + fiatcurrency: usd,eur,gbp sparklinedays: 1 updatefrequency: 300 From f0da8a7bd61beb5bd9607acc9ada90c1fbfffb7c Mon Sep 17 00:00:00 2001 From: Neil Innes Date: Thu, 8 Jul 2021 21:24:46 +0100 Subject: [PATCH 5/6] Update config.yaml From 490fbb5d48b85b54d2cd11c62480f30d6e4193fa Mon Sep 17 00:00:00 2001 From: Neil Innes Date: Thu, 8 Jul 2021 21:25:53 +0100 Subject: [PATCH 6/6] Update btcticker.py --- btcticker.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/btcticker.py b/btcticker.py index df6f516..f8d3bee 100644 --- a/btcticker.py +++ b/btcticker.py @@ -87,6 +87,8 @@ def getgecko(url): return geckojson, connectfail def convertkraken(whichcoin,fiat): + # krakenpairs is only required if using kraken as the exchange, this comma seperated list must match the currency and fiatcurrency list + # A full list of support Kraken pairs can be found here: https://support.kraken.com/hc/en-us/articles/360000920306-Ticker-pairs if (whichcoin == "bitcoin"): krakenpair = "BTC" + fiat.upper() else: