Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get data from API & no cache available !? #104

Open
emmgr23 opened this issue Jan 13, 2022 · 18 comments
Open

Unable to get data from API & no cache available !? #104

emmgr23 opened this issue Jan 13, 2022 · 18 comments

Comments

@emmgr23
Copy link

emmgr23 commented Jan 13, 2022

hi, im trying to get some price on the notch bar, but instead of the price, I have this error message:

"Unable to get data from API & no cache available"
Screenshot 2022-01-13 at 10 38 18

can you help me resolve this issue please ? ☺️

@chrislennon
Copy link
Owner

Hey @emmgr23 thanks for reaching out. I can see you selected BTC, ETH and another one. Could you share the (FIAT) currency you are trying to convert to and I will look to reproduce this experience

@emmgr23
Copy link
Author

emmgr23 commented Jan 13, 2022

thanks for the fast answer ☺️ I used your website and choose usd as fiat for the 4 crypto I added

`#!/usr/bin/python

-- coding: utf-8 --

import urllib2,json,sys

coin_ticker = "ETH" if "ETH"[0] != "{" else "BTC"
fiat_ticker = "USD" if "USD"[0] != "{" else "USD"
fiat_symbol = "$" if "$"[0] != "{" else "$"
num_format = "{:0.2f}" if "{:0.2f}"[2:8] != "format" else "{}"
mod_percent = float("0") if "0"[0] != "{" else float(0)
output_type = "simple" if "simple"[0] != "{" else "mktcap"
api_type = "live" if "live"[0] != "{" else "live"
extraOptions = "False" if "False"[0] != "{" else "&limit=1&aggregate=1&toTs=1514376000"
offline_cache = "true" if "true"[0] != "{" else "false"
percentageRound = int("0") if "0"[0] != "{" else int(0)
literalRound = int("0") if "0"[0] != "{" else int(0)

try:
if (api_type == "live"):

    url = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms={}&tsyms={}".format(coin_ticker, fiat_ticker)

    data = urllib2.urlopen(url)
    obj=json.load(data)

    raw_current = float(obj["RAW"][coin_ticker][fiat_ticker]["PRICE"])
    raw_opening = float(obj["RAW"][coin_ticker][fiat_ticker]["OPEN24HOUR"])
    raw_high = float(obj["RAW"][coin_ticker][fiat_ticker]["HIGHDAY"])
    raw_low = float(obj["RAW"][coin_ticker][fiat_ticker]["LOWDAY"])
    raw_mktcap = float(obj["RAW"][coin_ticker][fiat_ticker]["MKTCAP"])
    current = num_format.format(raw_current)
    opening = num_format.format(raw_opening)
    high = num_format.format(raw_high)
    low = num_format.format(raw_low)

    if (raw_mktcap > 1000000):
        mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap / 1000000) + " M"
    else:
        mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap)

    if (raw_current > raw_opening):
        trend = "▲"
    else:
        trend = "▼"

    if (output_type is "no"):
        output = fiat_symbol + current
    elif (output_type is "simple"):
        output = fiat_symbol + current + " " + trend
    elif (output_type is "mktcap"):
        output = fiat_symbol + current + " (" + fiat_symbol + mktcap + ")"
    elif (output_type is "absolute"):
        output = fiat_symbol + current + " (L: " + fiat_symbol + low + " H: " + fiat_symbol + high + ")"
    elif (output_type is "relative"):
        output = fiat_symbol + current + " (L: -" + fiat_symbol + str(round(raw_current - raw_low, literalRound)) + " H: +" + fiat_symbol + str(round(raw_high - raw_current,literalRound)) + ")"
    elif (output_type is "current-percentage"):
        output = fiat_symbol + current + " (" + str(round(((raw_current - raw_opening) / raw_current) * 100, percentageRound)) + "%)"
    elif (output_type is "range-percentage"):
        output = fiat_symbol + current + " (L: -" + str(round (((raw_current - raw_low) / raw_current) * 100, percentageRound)) + "% H: +" + str(round (((raw_high - raw_current) / raw_current) * 100, percentageRound)) + "%)"
    elif (output_type is "user-percentage"):
        output = fiat_symbol + current + " (L: " + fiat_symbol + str(round(raw_current - (raw_current * mod_percent), literalRound)) + " H: " + fiat_symbol + str(round(raw_current + (raw_current * mod_percent), literalRound)) + ")"

    if (offline_cache is "true"):
        tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "w")
        tmp_file.write(output)
        tmp_file.close()

    print(output)

elif (api_type == "historical"):
    url = "https://min-api.cryptocompare.com/data/histohour?fsym={}&tsym={}" + extraOptions
    url = url.format(coin_ticker, fiat_ticker)

    data = urllib2.urlopen(url)
    obj=json.load(data)

    raw_high = float(obj["Data"][1]["high"])
    high = num_format.format(raw_high)

    output = fiat_symbol + high

    if (offline_cache is "true"):
        tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "w")
        tmp_file.write(output)
        tmp_file.close() 

    print(output)

except urllib2.URLError, e:
try:
tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "r")
print 'CACHED ' + tmp_file.read()
except IOError, e:
print('Unable to get data from API & no cache available')
except ValueError, e:
print('There was an error formatting the output: %s' % e)

Please submit any issues https://github.com/chrislennon/Crypto-Touchbar-App/issues with the above script`

@chrislennon
Copy link
Owner

Awesome! Thanks so much. If it's an issue of the notch/touch bar side itself I'll take a look into this in a few hours.

Just to double check it's not something else, do you see prices returned when you visit this URL directly?

https://min-api.cryptocompare.com/data/histohour?fsym=BTC&tsym=USD

@emmgr23
Copy link
Author

emmgr23 commented Jan 13, 2022

yes i do see the price

@emmgr23
Copy link
Author

emmgr23 commented Jan 13, 2022

on my side i tried to check, but i dont see where the problem is, and i dont have a mbp with touchbar, to see if its working fine on it. btw thank you for your time ☺️

@chrislennon
Copy link
Owner

Hey @emmgr23 - appreciate the confirmation. So here's my take and a few things to try (also tagging @fifafu if he has any ideas). If you are able to access the API above and get data, its likely not a connection issue. I've also set up a few widgets on my notch bar and it seems to pull data fine, so its doesn't appear to be a code/template issue.

Thinking how to isolate this further, do you perhaps have a firewall blocking BetterTouchTool, or perhaps even the underlying python process?

To perhaps expose the actual error you could modify the script in the following location (red) inside Better Touch Tool, this removes the error handling and will likely show the actual error you are experiencing (in the blue area) after running the script with the run button (in green)

Screenshot 2022-01-13 at 20 36 47

#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2,json,sys

coin_ticker = "BTC" if "BTC"[0] != "{" else "BTC"
fiat_ticker = "USD" if "USD"[0] != "{" else "USD"
fiat_symbol = "$" if "$"[0] != "{" else  "$"
num_format = "{}" if "{}"[2:8] != "format" else  "{}"
mod_percent = float("0") if "0"[0] != "{" else float(0)
output_type = "no" if "no"[0] != "{" else  "mktcap"
api_type = "live" if "live"[0] != "{" else  "live"
extraOptions = "False" if "False"[0] != "{" else  "&limit=1&aggregate=1&toTs=1514376000"
offline_cache = "false" if "false"[0] != "{" else "false"
percentageRound = int("0") if "0"[0] != "{" else int(0)
literalRound = int("0") if "0"[0] != "{" else int(0)


if (api_type == "live"):

    url = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms={}&tsyms={}".format(coin_ticker, fiat_ticker)

    data = urllib2.urlopen(url)
    obj=json.load(data)

    raw_current = float(obj["RAW"][coin_ticker][fiat_ticker]["PRICE"])
    raw_opening = float(obj["RAW"][coin_ticker][fiat_ticker]["OPEN24HOUR"])
    raw_high = float(obj["RAW"][coin_ticker][fiat_ticker]["HIGHDAY"])
    raw_low = float(obj["RAW"][coin_ticker][fiat_ticker]["LOWDAY"])
    raw_mktcap = float(obj["RAW"][coin_ticker][fiat_ticker]["MKTCAP"])
    current = num_format.format(raw_current)
    opening = num_format.format(raw_opening)
    high = num_format.format(raw_high)
    low = num_format.format(raw_low)

    if (raw_mktcap > 1000000):
        mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap / 1000000) + " M"
    else:
        mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap)

    if (raw_current > raw_opening):
        trend = "▲"
    else:
        trend = "▼"

    if (output_type is "no"):
        output = fiat_symbol + current
    elif (output_type is "simple"):
        output = fiat_symbol + current + " " + trend
    elif (output_type is "mktcap"):
        output = fiat_symbol + current + " (" + fiat_symbol + mktcap + ")"
    elif (output_type is "absolute"):
        output = fiat_symbol + current + " (L: " + fiat_symbol + low + " H: " + fiat_symbol + high + ")"
    elif (output_type is "relative"):
        output = fiat_symbol + current + " (L: -" + fiat_symbol + str(round(raw_current - raw_low, literalRound)) + " H: +" + fiat_symbol + str(round(raw_high - raw_current,literalRound)) + ")"
    elif (output_type is "current-percentage"):
        output = fiat_symbol + current + " (" + str(round(((raw_current - raw_opening) / raw_current) * 100, percentageRound)) + "%)"
    elif (output_type is "range-percentage"):
        output = fiat_symbol + current + " (L: -" + str(round (((raw_current - raw_low) / raw_current) * 100, percentageRound)) + "% H: +" + str(round (((raw_high - raw_current) / raw_current) * 100, percentageRound)) + "%)"
    elif (output_type is "user-percentage"):
        output = fiat_symbol + current + " (L: " + fiat_symbol + str(round(raw_current - (raw_current * mod_percent), literalRound)) + " H: " + fiat_symbol + str(round(raw_current + (raw_current * mod_percent), literalRound)) + ")"

    if (offline_cache is "true"):
        tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "w")
        tmp_file.write(output)
        tmp_file.close()

    print(output)

elif (api_type == "historical"):
    url = "https://min-api.cryptocompare.com/data/histohour?fsym={}&tsym={}" + extraOptions
    url = url.format(coin_ticker, fiat_ticker)

    data = urllib2.urlopen(url)
    obj=json.load(data)

    raw_high = float(obj["Data"][1]["high"])
    high = num_format.format(raw_high)

    output = fiat_symbol + high

    if (offline_cache is "true"):
        tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "w")
        tmp_file.write(output)
        tmp_file.close() 

    print(output)

@emmgr23
Copy link
Author

emmgr23 commented Jan 13, 2022

ha yes i didnt see the result section before:
im sorry i dont understand much of what's happen here
Traceback (most recent call last): File "<string>", line 22, in <module> File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 429, in open response = self._open(req, data) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 447, in _open '_open', req) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 407, in _call_chain result = func(*args) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1241, in https_open context=self._context) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1198, in do_open raise URLError(err) urllib2.URLError: <urlopen error [Errno 9] B

@chrislennon
Copy link
Owner

Hmmmmmmmmm 🤔

Looks like part of the error got cut off, but I suspect it's something like

errorno 9: Bad file descriptor

I am not sure why this could occur, I'll have a think and see if anything makes sense. I assume you have tried things like restarting your machine etc? 😅 (Not that I'm clutching at straws)

@emmgr23
Copy link
Author

emmgr23 commented Jan 14, 2022

i think i found the bug
Screenshot 2022-01-14 at 11 22 46
it was not enable by default, but better touch tool was still displaying the widget inside the notchbar but without internet access, and now everything work fine, sorry i lost your time for a stupid bug

@emmgr23
Copy link
Author

emmgr23 commented Jan 14, 2022

i have a question for you , it is possible to color in red and green the little triangle , sorry i dont know python
if (raw_current > raw_opening): trend = "▲" else: trend = "▼"

@chrislennon
Copy link
Owner

Ah! Superb catch, I think it would be sensible for me to have it enabled by default, I'll have that added at some point soon(ish).

With regards to the colour of the arrows, I don't think this is possible unless @fifafu has any ideas. The python side just provides the output to better touch tool. The fonts (and their styles) are then handled by BTT itself. Unless we could add some identifying character which BTT would then use as a conditional for the font change, I'm not sure it would be possible.

Perhaps if there are coloured emojis already in existence that could be a workaround 🤔

@emmgr23
Copy link
Author

emmgr23 commented Jan 14, 2022

ok, im gonna have a look and ill let you know if i find something ☺️

@fifafu
Copy link
Contributor

fifafu commented Jan 14, 2022

The Notch Bar can display arbitrary HTML / CSS, but it's not yet enabled for the apple script / shell script widgets. Will be available with the next version.

Or maybe, I could update the Notch Bar template used in this project to output one of the new HTML/JS widgets, then we could get rid of the Python dependency and get all the power of HTML/CSS/JS rendering immediately. It should be very easy to convert this Python to JS, I'll have a look!

@chrislennon
Copy link
Owner

@fifafu this could be a massive improvement, it's always made me a tad uncomfortable invoking a sub process (be it pyhon or node or whatever) for these. If the notch (and touch bar) could render small pieces of html (with all the css/js magic) could really open up the interface side of things

@emmgr23
Copy link
Author

emmgr23 commented Jan 17, 2022

hi, chris :) , is there a problem with the api today , because even your website seems to have a problem to get the data?

@chrislennon
Copy link
Owner

Seems fine from my side - can always check it directly (using BTC -> USD as an example)

https://min-api.cryptocompare.com/data/histohour?fsym=BTC&tsym=USD

Screenshot 2022-01-17 at 22 57 17

@emmgr23
Copy link
Author

emmgr23 commented Jan 17, 2022

ha sorry again, i didnt know i was limited with this api
Screenshot 2022-01-17 at 22 33 00

@chrislennon
Copy link
Owner

yeah its a limitation of the API service that the extension makes use of. It's on an IP basis so you can check out issue #100 covers quite a lot of questions on this some resolutions too for example in this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants