From 5a7e93bd622da7da198b74bb7ac988743207e43d Mon Sep 17 00:00:00 2001 From: acidsploit Date: Tue, 27 Feb 2018 22:14:03 +0100 Subject: [PATCH] Update to exchange sources --- exchangerate.py | 162 ++++++++++++++++++++---------------------------- xpub.py | 5 +- 2 files changed, 70 insertions(+), 97 deletions(-) diff --git a/exchangerate.py b/exchangerate.py index 8dfc450..00d8bef 100644 --- a/exchangerate.py +++ b/exchangerate.py @@ -19,85 +19,84 @@ db_name = 'sqlite:///pyxpub.db?check_same_thread=False' -CRYPTOCOMPARE = ["EUR", "USD", "GBP", "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR"] +SOURCES = { + 'cryptocompare': ["EUR", "USD", "GBP", "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", + "CZK", "DKK", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", + "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", + "SGD", "THB", "TRY", "TWD", "ZAR"], + 'coinmarketcap': ["EUR", "USD", "GBP", "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", + "CZK", "DKK", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", + "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", + "SGD", "THB", "TRY", "TWD", "ZAR"], + 'kraken': ["EUR", "USD"], + 'coinbase': ["EUR", "USD"], + 'bitstamp': ["EUR", "USD"], + 'coinfloor': ["GBP"], + 'bitbay': ["EUR", "USD", "PLN"], + 'bitflip': ["RUB", "USD", "UAH"], + } -COINBASE = ["EUR", "USD"] -KRAKEN = ["EUR", "USD"] +def is_supported(currency, source): + if source in SOURCES: + if currency in SOURCES[source]: + return True + return False -def is_supported(currency, source): - _db = dataset.connect(db_name, row_type=stuf) - _table = _db[source] - _record = _table.find_one(currency=currency) - - if _record: - return True - else: - return False def get_currencies(source): - if source == "cryptocompare": - return {'currencies': CRYPTOCOMPARE} - elif source == "coinbase": - return {'currencies': COINBASE} - elif source == "kraken": - return {'currencies': KRAKEN} - -def update_cryptocompare(): + if source in SOURCES: + return {'currencies': SOURCES[source]} + +def get_sources(): + _keys = [] + for key in SOURCES: + _keys.append(key) + + return {'sources': _keys} + +def update_coinmarketcap(currency): _db = dataset.connect(db_name, row_type=stuf) - _table = _db['cryptocompare'] + _table = _db['coinmarketcap'] - _api = "https://min-api.cryptocompare.com/data/price?fsym=BCH&tsyms={currencies}" - _string = ','.join(map(str, CRYPTOCOMPARE)) - _currencies = {'currencies': _string} - _query = _api.format(**_currencies) + _api = "https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/?convert={}" + _query = _api.format(currency) print("PYXPUB - FETCH: " + _query) _response = requests.get(_query) _json = _response.json() - for key in _json: - _record = _table.find_one(currency=key) - if not _record: - print('INSERT: ' + key) - with dataset.connect(db_name, row_type=stuf) as tx: - tx['cryptocompare'].insert(dict(currency=key, rate=_json[key], timestamp=time.time())) - else: - _table.update(dict(currency=key, rate=_json[key], timestamp=time.time()), ['currency']) - #print('UPDATE: ' + key) - -def update_coinbase(): - _db = dataset.connect(db_name, row_type=stuf) - _table = _db['coinbase'] - - _api = "https://min-api.cryptocompare.com/data/price?fsym=BCH&tsyms={currencies}&e=Coinbase" - _string = ','.join(map(str, COINBASE)) - _currencies = {'currencies': _string} - _query = _api.format(**_currencies) + _price = _json[0]['price_' + currency.lower()] - print("PYXPUB - FETCH: " + _query) - _response = requests.get(_query) - _json = _response.json() + _record = _table.find_one(currency=currency) + if not _record: + print('PYXPUB - INSERT: ' + currency) + with dataset.connect(db_name, row_type=stuf) as tx: + tx['coinmarketcap'].insert(dict(currency=currency, rate=_price, timestamp=time.time())) + else: + _table.update(dict(currency=currency, rate=_price, timestamp=time.time()), ['currency']) + print('PYXPUB - UPDATE: ' + currency) + - for key in _json: - _record = _table.find_one(currency=key) - if not _record: - print('INSERT: ' + key) - with dataset.connect(db_name, row_type=stuf) as tx: - tx['coinbase'].insert(dict(currency=key, rate=_json[key], timestamp=time.time())) - else: - _table.update(dict(currency=key, rate=_json[key], timestamp=time.time()), ['currency']) - #print('UPDATE: ' + key) - -def update_kraken(): +def update_db(source, currency): _db = dataset.connect(db_name, row_type=stuf) - _table = _db['kraken'] + _table = _db[source] - _api = "https://min-api.cryptocompare.com/data/price?fsym=BCH&tsyms={currencies}&e=Kraken" - _string = ','.join(map(str, KRAKEN)) - _currencies = {'currencies': _string} - _query = _api.format(**_currencies) + if source == 'coinmarketcap': + update_coinmarketcap(currency) + return 0 + + if source == 'cryptocompare': + _api = "https://min-api.cryptocompare.com/data/price?fsym=BCH&tsyms={currencies}" + else: + _api = "https://min-api.cryptocompare.com/data/price?fsym=BCH&tsyms={currencies}&e={source}" + + _string = ','.join(map(str, SOURCES[source])) + _filler = {'currencies': _string, + 'source': source + } + _query = _api.format(**_filler) print("PYXPUB - FETCH: " + _query) _response = requests.get(_query) @@ -106,13 +105,12 @@ def update_kraken(): for key in _json: _record = _table.find_one(currency=key) if not _record: - print('INSERT: ' + key) + print('PYXPUB - INSERT: ' + key) with dataset.connect(db_name, row_type=stuf) as tx: - tx['kraken'].insert(dict(currency=key, rate=_json[key], timestamp=time.time())) + tx[source].insert(dict(currency=key, rate=_json[key], timestamp=time.time())) else: _table.update(dict(currency=key, rate=_json[key], timestamp=time.time()), ['currency']) #print('UPDATE: ' + key) - def get_rate(currency, source): _db = dataset.connect(db_name, row_type=stuf) @@ -120,45 +118,19 @@ def get_rate(currency, source): _record = _table.find_one(currency=currency) _now = time.time() - if source == "cryptocompare": + if source in SOURCES: if not _record: print("PYXPUB - TABLE: {} ENTRY: {} NOT FOUND - UPDATING".format(source, currency)) - update_cryptocompare() + update_db(source, currency) _record = _table.find_one(currency=currency) else: if ((_now - _record.timestamp) > 30): print("PYXPUB - TABLE {} OUTDATED - UPDATING".format(source)) - update_cryptocompare() + update_db(source, currency) _record = _table.find_one(currency=currency) else: _record = _table.find_one(currency=currency) - - elif source == "coinbase": - if not _record: - print("PYXPUB - TABLE: {} ENTRY: {} NOT FOUND - UPDATING".format(source, currency)) - update_coinbase() - _record = _table.find_one(currency=currency) - else: - if ((_now - _record.timestamp) > 30): - print("PYXPUB - TABLE {} OUTDATED - UPDATING".format(source)) - update_coinbase() - _record = _table.find_one(currency=currency) - else: - _record = _table.find_one(currency=currency) - - elif source == "kraken": - if not _record: - print("PYXPUB - TABLE: {} ENTRY: {} NOT FOUND - UPDATING".format(source, currency)) - update_kraken() - _record = _table.find_one(currency=currency) - else: - if ((_now - _record.timestamp) > 30): - print("PYXPUB - TABLE {} OUTDATED - UPDATING".format(source)) - update_kraken() - _record = _table.find_one(currency=currency) - else: - _record = _table.find_one(currency=currency) - + return _record diff --git a/xpub.py b/xpub.py index 4b9fb95..6181331 100644 --- a/xpub.py +++ b/xpub.py @@ -355,12 +355,13 @@ def generate_rate(parameters, ip_addr): 'price' : _r.rate, } return _price + # TODO: Error handling is_supported() elif 'source' in parameters: _r = exchangerate.get_currencies(parameters.source) return _r else: - abort(400, "ERROR: Incorrect use of api! ") - + _r = exchangerate.get_sources() + return _r