Last active
October 2, 2017 07:47
Star
You must be signed in to star a gist
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
#!/usr/bin/python | |
import os, sys | |
import time | |
import urllib.request | |
import json | |
import datetime | |
from pymongo import MongoClient | |
def btceGetTicker(pairs): | |
""" Get current coin values, return (buy_price, sell_price, avg_price)""" | |
try: | |
ticker_raw = urllib.request.urlopen("https://wex.nz/api/3/ticker/" + pairs) | |
ticker_encoding = ticker_raw.info().get_content_charset('utf-8') | |
ticker_json = json.loads(ticker_raw.read().decode(ticker_encoding)) | |
return ticker_json | |
except Exception as e: | |
print("Get ticker failed: ", e) | |
return None | |
def main(): | |
COINS = ["btc_usd", "ltc_usd", "nmc_usd", "nvc_usd", "ppc_usd", "dsh_usd", "eth_usd", "usd_rur"] | |
pairs = "-".join(COINS) | |
# setup database connection | |
dbClientConnection = MongoClient('localhost', 27017) | |
db = dbClientConnection["wex"] | |
while True: | |
ticker = btceGetTicker(pairs) | |
if ticker: | |
try: | |
ticker["update_time"] = datetime.datetime.utcnow() | |
entry = db["ticker"] | |
entry.insert_one(ticker) | |
except Exception as e: | |
print("Store ticker failed: ", e) | |
time.sleep(15) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get ticker from wex.nz and save to mongodb database.