Skip to content

Instantly share code, notes, and snippets.

Created Apr 20, 2017
What would you like to do?
import requests
import pprint
pp = pprint.PrettyPrinter(indent=4)
feed = {}
url = ""
scaleVolumeBy = 10
bases=["BTC", "CNY","BTS"]
_request_headers = {'content-type': 'application/json',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
for base in bases :
feed[base] = {}
for quote in quotes :
if base == quote :
params = {'c': quote.lower(), 'mk_type': base.lower()}
print("Checking quote (params[c]) = "+ quote.lower() + " base (params[mk_type]) = " + base.lower() + " / url ="+quote.lower()+"&mk_type="+base.lower());
response = requests.get(url=url, params=params, headers=_request_headers, timeout=20)
result = response.json()
feed["response"] = result
if "ticker" in result and \
"last" in result["ticker"] and \
"vol" in result["ticker"] :
feed[base][quote] = {"price" : (float(result["ticker"]["last"])),
"volume" : (float(result["ticker"]["vol"]) * scaleVolumeBy)}
else :
print("\nFetched data from {0} is empty!".format(type(self).__name__))
except Exception as e:
print("\nError fetching results from {1}! ({0})".format(str(e), 'btc38 script'))

This comment has been minimized.

Copy link
Owner Author

@roelandp roelandp commented Apr 20, 2017

I swapped the try - except block from outside the for loop to inside the for loop to continue on errors. Notice the first pairs
BTC/BTS hits on an non-json error from BTC38 APY. I think that is also the case with the new bitshares-pricefeed ?

Here is the output of the above script:

Checking quote (params[c]) = bts base (params[mk_type]) = btc / url =

Error fetching results from btc38 script! (Expecting value: line 1 column 1 (char 0))
Checking quote (params[c]) = btc base (params[mk_type]) = cny / url =
Checking quote (params[c]) = bts base (params[mk_type]) = cny / url =
Checking quote (params[c]) = btc base (params[mk_type]) = bts / url =
{   'BTC': {},
    'BTS': {'BTC': {'price': 8249.0, 'volume': 1907.6334700000002}},
    'CNY': {   'BTC': {'price': 8249.0, 'volume': 1907.6334700000002},
               'BTS': {'price': 0.0859, 'volume': 542431189.16969}},
    'response': {   'ticker': {   'buy': 8223,
                                  'high': 8340,
                                  'last': 8249,
                                  'low': 8126,
                                  'sell': 8249,
                                  'vol': 190.763347}}}```

This comment has been minimized.

Copy link

@xeroc xeroc commented Apr 20, 2017

Good point .. it breaks the loop if only one of the quote or base assets break .. ignoring everything that comes later on ..
Maybe i should rework the whole section. The point of the Exceptions is so that I can break the whole script if I miss a significant exchange as defined in the config. Ideally, i don't need to deal with exchanges providing non-json returns. Maybe I should add some checks for HTTP error codes ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment