Created
April 5, 2019 06:25
-
-
Save troyscott/6f5459116d41cda39f9771f9c75f76da 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
from decimal import Decimal | |
from bson.decimal128 import Decimal128 | |
import datetime | |
import time | |
import config | |
import json | |
import requests | |
from pymongo import MongoClient | |
from cryptolib import utils | |
def crypto_test(): | |
print('running test ...') | |
def crypto_prices(): | |
current_timestamp = datetime.datetime.utcnow() | |
print(current_timestamp) | |
current_datetime = utils.getdatetime(current_timestamp) | |
current_date = utils.getdate(current_timestamp) | |
current_time = utils.gettime(current_timestamp) | |
batch_id = utils.getbatchid(current_timestamp) | |
# Call the local API which queries Binance server for data | |
url = base_url + '/crypto/prices' | |
print("Get data ...") | |
r = requests.get(url) | |
data = r.json() | |
print("Processing batch ...") | |
for d in data: | |
trade_pair = d['symbol'][-3:] | |
if trade_pair == 'BTC': | |
ticker = utils.getticker(d['symbol']) | |
trade_pair_symbol = d['symbol'] | |
btcusd_price = Decimal(utils.getusd(data)) | |
btc_price = Decimal(d['price']) | |
usd_price = Decimal(btc_price) * Decimal(btcusd_price) | |
# Create document/record | |
doc = { | |
"batch_id": batch_id, | |
"price_utc_timestamp": current_timestamp, | |
"price_utc_datetime": current_datetime, | |
"price_utc_date": current_date, | |
"price_utc_time": current_time, | |
"ticker": ticker, | |
"trade_pair_symbol": trade_pair_symbol, | |
"trade_pair": trade_pair, | |
"btc_price": Decimal128(Decimal(btc_price)), | |
"usd_price": Decimal128(Decimal(usd_price)), | |
"btcusd_price": Decimal128(Decimal(btcusd_price)) | |
} | |
prices_id = prices.insert_one(doc).inserted_id | |
print("Inserted: " + str(prices_id)) | |
print("Batch complete ...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment