Skip to content

Instantly share code, notes, and snippets.

@dangsonbk
Last active October 2, 2017 07:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dangsonbk/94239a320da8a7f63e849229ae9b0348 to your computer and use it in GitHub Desktop.
#!/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()
@dangsonbk
Copy link
Author

Get ticker from wex.nz and save to mongodb database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment