Skip to content

Commit

Permalink
Update to exchange sources
Browse files Browse the repository at this point in the history
  • Loading branch information
acidsploit authored Feb 27, 2018
1 parent f4b67a4 commit 5a7e93b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 97 deletions.
162 changes: 67 additions & 95 deletions exchangerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -106,59 +105,32 @@ 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)
_table = _db[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


Expand Down
5 changes: 3 additions & 2 deletions xpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down

0 comments on commit 5a7e93b

Please sign in to comment.