From 775ef507ea5be750254d24c39eeaf3c62efd73ae Mon Sep 17 00:00:00 2001 From: andrewso <9V5f1FkzI2LD> Date: Fri, 7 Feb 2020 21:17:39 +0000 Subject: [PATCH] [07.02.20] path changes, fixing issues and finalising connection to graphql --- src/main.py | 4 ++-- src/pricing/bitfinex.py | 8 +++---- src/pricing/coinbase.py | 4 ++-- src/pricing/collector.py | 44 +++++++++++++++++++----------------- src/pricing/gemini.py | 39 ++++++++++++++++++++++---------- src/utils/databaseConnect.py | 10 ++++---- 6 files changed, 63 insertions(+), 46 deletions(-) diff --git a/src/main.py b/src/main.py index 5144954..fb9b85d 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,7 @@ #!/usr/bin/env python from threading import Thread -import src.pricing.collector as collector +from pricing.collector import collector def callCollector(args): collector(args) @@ -11,4 +11,4 @@ if __name__=='__main__': currencies = [ "btc_gbp" ] for i in range(len(currencies)): - Thread(target=callCollector, args=(1,)).start() \ No newline at end of file + Thread(target=callCollector, args=[currencies[i]]).start() \ No newline at end of file diff --git a/src/pricing/bitfinex.py b/src/pricing/bitfinex.py index 46d01ab..8899d8c 100644 --- a/src/pricing/bitfinex.py +++ b/src/pricing/bitfinex.py @@ -5,7 +5,7 @@ import requests, json, sys def bitfinexPublicTicker(type): try: - uri = "https://api.bitfinex.com/v2/tickers?symbols=" + "t"+type.toLower().replace('_', '') + uri = "https://api.bitfinex.com/v2/tickers?symbols=" + "t"+type.upper().replace('_', '') response = requests.request("GET", uri) response = json.loads(response.text) @@ -14,7 +14,7 @@ def bitfinexPublicTicker(type): price = round(price, 3) return price except KeyError as e: - print("Error: %s" % str(e)) + print("Bitfinex Spot Price Error: %s" % str(e)) sys.stdout.flush() price = 0 return price @@ -22,7 +22,7 @@ def bitfinexPublicTicker(type): def bitfinexHighLowVol(type): try: - uri = "https://api.bitfinex.com/v2/tickers?symbols=" + "t"+type.toLower().replace('_', '') + uri = "https://api.bitfinex.com/v2/tickers?symbols=" + "t"+type.upper().replace('_', '') response = requests.request("GET", uri) response = json.loads(response.text) @@ -33,6 +33,6 @@ def bitfinexHighLowVol(type): return high, low, vol except KeyError as e: - print("Error: %s" % str(e)) + print("Bitfinex High Low Volume Error: %s" % str(e)) sys.stdout.flush() return 0, 0, 0 \ No newline at end of file diff --git a/src/pricing/coinbase.py b/src/pricing/coinbase.py index eaf154f..4e5ce81 100644 --- a/src/pricing/coinbase.py +++ b/src/pricing/coinbase.py @@ -20,7 +20,7 @@ def coinbasePublicTicker(type): api_key = keys().api_key api_secret = keys().api_secret - type = type.toUpper().replace('_', '-') + type = type.upper().replace('_', '-') try: client = Client(api_key, api_secret) @@ -29,7 +29,7 @@ def coinbasePublicTicker(type): price = round(price, 3) return price except KeyError as e: - print("Error: %s" % str(e)) + print("Coinbase Spot Price Error: %s" % str(e)) sys.stdout.flush() price = 0 return price \ No newline at end of file diff --git a/src/pricing/collector.py b/src/pricing/collector.py index c8d8bfa..2604a39 100644 --- a/src/pricing/collector.py +++ b/src/pricing/collector.py @@ -11,7 +11,7 @@ from pricing.gemini import geminiPublicTicker, geminiHighLowVol, geminiOpenClose from utils.databaseConnect import send -btc_usd="resources/sql/V1_INSERT_NEW_PRICE_RECORD_BTC.graphql" +btc_usd="src/resources/queries/V1_INSERT_NEW_PRICE_RECORD_BTC.graphql" def getInsertForType(type): if type == "btc_usd": @@ -75,35 +75,37 @@ def getHighLowVol(type): return high, low, vol -def sendToGateway(type, timestamp, av_price, high, low, vol, open, close): - try: - with open(btc_usd, 'r') as queryFile: - data = queryFile.read() - query = data % (type, timestamp, av_price, high, low, vol, open, close) +def sendToGateway(c_type, timestamp, av_price, high, low, vol, o_price, c_price): - status, response = send(query) + with open(btc_usd, 'r') as file: + data = file.read() - print("Status: ", status) - print("Response: ", response) - return True - except BaseException as exception: - print("Error: %s" % str(exception)) - sys.stdout.flush() - return False + timestamp = '"'+timestamp.strftime('%Y-%m-%dT%H:%M:%S+00:00')+'"' + type = '"'+c_type+'"' + + query = data % (timestamp, type, float(av_price), float(high), float(low), float(vol), float(o_price), float(c_price)) + + print(query) + + status, response = send(query) + + print("Status: ", status) + print("Response: ", response) def getOpenClose(type): - open, close = geminiOpenClose() + open, close = geminiOpenClose(type) return open, close # Dynamically Spin up Child process for each type wanting to track -def collector(type): +def collector(c_type): print("Console: ", "== Historical Price Collector ==") while True: - sleep(3600) - av_price, timestamp = averager(type) - high, low, vol = getHighLowVol(type) - open, close = getOpenClose(type) + av_price, timestamp = averager(c_type) + high, low, vol = getHighLowVol(c_type) + o_price, c_price = getOpenClose(c_type) - sendToGateway(type, timestamp, av_price, high, low, vol, open, close) \ No newline at end of file + sendToGateway(c_type, timestamp, av_price, high, low, vol, o_price, c_price) + + sleep(3600) \ No newline at end of file diff --git a/src/pricing/gemini.py b/src/pricing/gemini.py index 28c262b..e3fcd61 100644 --- a/src/pricing/gemini.py +++ b/src/pricing/gemini.py @@ -1,51 +1,66 @@ #!/usr/bin/env python import requests, json, sys +from currency_converter import CurrencyConverter def geminiPublicTicker(type): + converter = CurrencyConverter() + try: - uri = "https://api.gemini.com/v1/pubticker/" + type.toUpper().replace('_', '') + uri = "https://api.gemini.com/v1/pubticker/" + (type.lower().replace('gbp', 'usd')).replace('_', '') + response = requests.request("GET", uri) response = json.loads(response.text) - price = (float(response['last']) + float(response['ask']) + float(response['bid']))/3 + price = (converter.convert(float(response['last']), 'USD', 'GBP') + + converter.convert(float(response['ask']), 'USD', 'GBP') + + converter.convert(float(response['bid']), 'USD', 'GBP'))/3 price = round(price, 3) return price except KeyError as e: - print("Error: %s" % str(e)) + print("Gemini Spot Price Error: %s" % str(e)) sys.stdout.flush() price = 0 return price def geminiHighLowVol(type): + + converter = CurrencyConverter() + try: - uri = "https://api.gemini.com/v2/ticker/" + type.toUpper().replace('_', '') + uri = "https://api.gemini.com/v2/ticker/" + (type.lower().replace('gbp', 'usd')).replace('_', '') response = requests.request("GET", uri) response = json.loads(response.text) - high = float(response['high']) - low = float(response['low']) - vol = float(response['volo']) + high = converter.convert(float(response['high']), 'USD', 'GBP') + low = converter.convert(float(response['low']), 'USD', 'GBP') + + uri = "https://api.gemini.com/v1/pubticker/" + (type.lower().replace('gbp', 'usd')).replace('_', '') + response = requests.request("GET", uri) + response = json.loads(response.text) + vol = converter.convert(float(response['volume']['BTC']), 'USD', 'GBP') return high, low, vol except KeyError as e: - print("Error: %s" % str(e)) + print("Gemini High Low Volume Error: %s" % str(e)) sys.stdout.flush() return 0, 0, 0 def geminiOpenClose(type): + converter = CurrencyConverter() + try: - uri = "https://api.gemini.com/v2/ticker/" + type.toUpper().replace('_', '') + uri = "https://api.gemini.com/v2/ticker/" + (type.lower().replace('gbp', 'usd')).replace('_', '') response = requests.request("GET", uri) response = json.loads(response.text) - open = float(response['open']) - close = float(response['close']) + open = converter.convert(float(response['open']), 'USD', 'GBP') + close = converter.convert(float(response['close']), 'USD', 'GBP') return open, close except KeyError as e: - print("Error: %s" % str(e)) + print("Gemini Open Close Error: %s" % str(e)) sys.stdout.flush() return 0, 0 diff --git a/src/utils/databaseConnect.py b/src/utils/databaseConnect.py index cda409a..89e8bd1 100644 --- a/src/utils/databaseConnect.py +++ b/src/utils/databaseConnect.py @@ -4,23 +4,23 @@ import requests, os, json from dotenv import load_dotenv from pathlib import Path # python3 only -env_path = Path('.') / 'configuration/dbgateway.env.env' +env_path = Path('.') / 'configuration/dbgateway.env' load_dotenv(dotenv_path=env_path) class keys(): def __init__(self): - self.uri = os.getenv('URI') + self.uri = os.getenv("URI") self.port = os.getenv("PORT") def send(query): uri = keys().uri + ":"+ keys().port + "/graphql" - headers = {'Content-type': 'application/graphql'} + headers = {'Content-type': 'application/json'} - response = requests.post(uri, data=query, headers=headers) + response = requests.post(uri, json={'query': query}, headers=headers) - statusCode = json.loads(response.status_code) + statusCode = response.status_code response = json.loads(response.text) return statusCode, response \ No newline at end of file