Skip to content

Instantly share code, notes, and snippets.

@jasonneurohr jasonneurohr/btc_dump.py
Last active Jun 12, 2017

Embed
What would you like to do?
"""This script connects to the BTC Markets API
Downloads ticker data, and writes the data to a local MongoDB instance
Author: Jason Neurohr
Version: 0.1
"""
import json
import pymongo
import requests
def mongo_insert(rec):
"""Insert record into MongoDB
"""
client = pymongo.MongoClient('localhost', 27017)
db = client.btc
collection = db['ticks']
try:
result = collection.insert_one(rec)
rec_id = result.inserted_id
except pymongo.errors.ConnectionFailure as err:
print('Connection Error: ', err, sep='')
def get_tick(url):
"""Get tick data from BTCMarkets API
Returns:
dict: Tick data as a dictionary
"""
req = requests.session()
res = req.get(url)
res_text = json.loads(res.text)
return res_text
def main():
"""Main function
"""
# Initialise ticker URLs list
tick_urls = [
'https://api.btcmarkets.net/market/ETH/AUD/tick',
'https://api.btcmarkets.net/market/BTC/AUD/tick'
]
# For each ticker URL in the tick_urls list
# get the data and then commit to MongoDB
for ticker in tick_urls:
rec = get_tick(ticker)
mongo_insert(rec)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.