-
-
Save roelandp/580d14fd9d866afdbca43efe92e91e2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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
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: