CoinMarketCap v1 API中的fetching数据模块
CoinMarketCapA的Python项目详细描述
共同市场
coinmarketcap的v1 api的python实现。
您需要coinmarketcap中的api密钥才能利用此模块,您可以从here中获取密钥。由于请求率较低,我建议您在沙盒环境中测试应用程序。在使用它们的沙盒环境时,您需要一个不同的api密钥,您可以从here中获取该密钥。
安装
python3 -m pip install CoinMarketCapAPI
身份验证
如果在init期间没有提供键,模块将在$HOME/.coinmarketcap.json
中查找json文件,语法如下。
{"sandbox":"API_KEY","production":"API_KEY"}
或者,可以将密钥设置为os环境变量。使用.bashrc,添加以下两行。
export COINMARKETCAP_SANDBOX="API_KEY" export COINMARKETCAP_PRODUCTION="API_KEY"
示例
带有api密钥的init客户端。
fromcoinmarketcapimportClientkey="API_KEY"client=Client(apikey=key)```requestlimitThefollowingexampleshowshowyoucanaccessandsendarequesttoeachofCoinMarketCap's endpoint (cryptocurrency, exchange, global_metrics, tools). Notice plural and singular method endings. Plurals allow a list of `id`, `symbol` or `slug`, singular allows only a single value.```pythonfromcoinmarketcapimportClientfromdatetimeimportdatetime# Init sandbox client.client=Client(sandbox=True)# fetch cryptocurrency infoclient.cryptocurrency.info.symbols(["BTC","ETH"])client.cryptocurrency.info.ids(1)# fetch exchange infoclient.exchange.info.slugs("binance")client.exchange.info.ids([1,4])# fetch global market datatime=datetime.strptime("2018-12-21","%Y-%m-%d")client.global_metrics.quotes.latest()client.global_metrics.quotes.historical(time_start=time)amount_1=100.4amount_2="901.23"id=4# convert price historic priceclient.tools.price.convert_id(amount_1,id,time=time)client.tools.price.convert_symbol(amount_2,"BTC",convert=["USD","ETH"])
由于coinmarketcap的信用和利率限制系统,实现一个适当的请求节流器是复杂的。无论如何,我尝试了三种不同级别的节流。”分、日、月。每一级都要确保你没有超过给定计划的请求限制。不同层次的人对coinmarketcap的信用体系没有认识…
fromcoinmarketcapimportClient# By default throttling of requests are off.# Ignore the Client's keyword arguments "throttle", "plan", and "block" if# you don't want the client to throttle requests. # client_1 will not exceed the number of request each minute with the basic plan.client_1=Client(throttle="minute",plan="basic")# client_2 will not exceed the daily request limit.client_2=Client(throttle="daily",block=True,plan="hobbyist")# client_3 will never exceeded CoinMarketCap monthly request rate.client_3=Client(throttle="monthly",block=False,plan="professional")
每个请求都被缓存,数据的过期时间可以用关键字参数expire
调整。如果不需要任何缓存数据,请设置expire=0
。
fromcoinmarketcapimportClient# cached request will be removed after 1 secondclient=Client(expire=1)# cacherd request will be removed after 1 hclient=Client(expire=3600)# To remove all data from the cache, use the method clear_cacheclient.clear_cache()
待办事项
- 启用请求的适当限制。
- 测试不同的python版本。
这是我的第一个python模块。因此,我非常感谢您的反馈:)