11-13 12 views
上一篇文章 我们介绍了:Python获取CoinMarketCap的token交易信息
本文章我们就实战一下:获取的实时交易信息放到elasticsearch这种时间序列数据存储里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from elasticsearch import Elasticsearch import elasticsearch.helpers from django.conf import settings def refresh_ticker_to_es(): """ 所有 ticker 信息刷新到 elasticsearch 中 :return: """ logger.info("开始获取所有ticker的信息到elasticsearch中") for ticker_instance in ticker.models.Ticker.objects.filter(enabled=True): logger.info("开始获取代币{0}的信息到elasticsearch中".format(ticker_instance.symbol)) coinmarketcap = Market() ticker_dict = coinmarketcap.ticker(ticker_instance.tickerid) if ticker_dict['metadata']['error']: logger.warning("获取代币{0}数据错误,错误内容:{1}".format(ticker_instance.symbol, ticker_dict['metadata']['error'])) else: ticker_dict['data']['@timestamp'] = datetime.now().isoformat() ticker_dict['data']['quotes']['USD']['nvt'] = ticker_dict['data']['quotes']['USD']['market_cap'] / ticker_dict['data']['quotes']['USD']['volume_24h'] actions_ticker = [ { '_op_type': 'index', '_index': "tickers-index-" + datetime.now().strftime('%Y.%m.%d'), '_type': "tickers-type", '_source': ticker_dict['data'] } ] try: es = Elasticsearch([settings.ES_HOST], http_auth=(settings.ES_USER, settings.ES_PASSWORD), port=settings.ES_PORT, ) elasticsearch.helpers.bulk(es, actions_ticker) logger.info("成功获取代币{0}信息到elasticsearch".format(ticker_instance.symbol)) except Exception as e: logger.exception(e) logger.error("获取代币{0}信息到elasticsearch时失败,暂时跳过处理".format(ticker_instance.symbol)) logger.error(traceback.format_exc()) logger.info("结束获取所有代币的信息到elasticsearch中") |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!