Skip to content

Instantly share code, notes, and snippets.

@jasonneurohr jasonneurohr/
Last active Jun 12, 2017

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']
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
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 = [
# 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)
if __name__ == "__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.