Created
November 7, 2019 13:05
-
-
Save yuyasugano/93a90d466d9377c6aa2ea13f9068d46f to your computer and use it in GitHub Desktop.
Elasticsearch bitbank.cc API data crawling every second
This file contains hidden or 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 json | |
import time | |
import python_bitbankcc | |
from datetime import datetime | |
from elasticsearch import Elasticsearch | |
def get_ticker(pub): | |
ret = pub.get_ticker('btc_jpy') | |
return ret | |
def main(): | |
# by default we connect to localhost:9200 | |
es = Elasticsearch() | |
pub = python_bitbankcc.public() | |
while True: | |
# every second we record cvt data in a minute | |
for i in range(60): | |
try: | |
ret = get_ticker(pub) | |
doc = { | |
'close': int(ret['last']), | |
'volume': float(ret['vol']), | |
'timestamp': int(ret['timestamp']) | |
} | |
print("----------------------------------------------------------------------") | |
print(json.dumps(doc, indent=4, separators=(',', ': '))) | |
except Exception as e: | |
print("Error occurred: {}".format(e)) | |
try: | |
res = es.index(index="btcjpy", id=i+1, body=doc) | |
print("----------------------------------------------------------------------") | |
print("Elasticsearch collected a second data: {}".format(res['result'])) | |
except Exception as e: | |
print("Error occurred: {}".format(e)) | |
time.sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment