Skip to content

Instantly share code, notes, and snippets.

@roelandp
Created April 20, 2017 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roelandp/580d14fd9d866afdbca43efe92e91e2e to your computer and use it in GitHub Desktop.
Save roelandp/580d14fd9d866afdbca43efe92e91e2e to your computer and use it in GitHub Desktop.
import requests
import pprint
pp = pprint.PrettyPrinter(indent=4)
feed = {}
url = "http://api.btc38.com/v1/ticker.php"
scaleVolumeBy = 10
quotes=["BTC","BTS"]
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 :
try:
feed[base] = {}
for quote in quotes :
if base == quote :
continue
params = {'c': quote.lower(), 'mk_type': base.lower()}
print("Checking quote (params[c]) = "+ quote.lower() + " base (params[mk_type]) = " + base.lower() + " / url = http://api.btc38.com/v1/ticker.php?c="+quote.lower()+"&mk_type="+base.lower());
response = requests.get(url=url, params=params, headers=_request_headers, timeout=20)
print(response.content)
result = response.json()
#print(result)
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__))
continue
except Exception as e:
print("\nError fetching results from {1}! ({0})".format(str(e), 'btc38 script'))
pass
pp.pprint(feed)
@roelandp
Copy link
Author

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 = http://api.btc38.com/v1/ticker.php?c=bts&mk_type=btc
b'fail#2'

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 = http://api.btc38.com/v1/ticker.php?c=btc&mk_type=cny
b'{"ticker":{"high":8340,"low":8126,"last":8249,"vol":190.763347,"buy":8223,"sell":8249}}'
Checking quote (params[c]) = bts base (params[mk_type]) = cny / url = http://api.btc38.com/v1/ticker.php?c=bts&mk_type=cny
b'{"ticker":{"high":0.0942,"low":0.077,"last":0.0859,"vol":54243118.916969,"buy":0.086,"sell":0.0867}}'
Checking quote (params[c]) = btc base (params[mk_type]) = bts / url = http://api.btc38.com/v1/ticker.php?c=btc&mk_type=bts
b'{"ticker":{"high":8340,"low":8126,"last":8249,"vol":190.763347,"buy":8223,"sell":8249}}'
{   '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}}}```

@xeroc
Copy link

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