123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #This example uses Python 2.7 and the python-request library.
- from requests import Request, Session
- import datetime
- from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
- import json
- # https://coinmarketcap.com/api/documentation/v1/#section/Endpoint-Overview
- url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
- parameters = {
- 'start':'1',
- 'limit':'500',
- 'convert':'USD'
- }
- headers = {
- 'Accepts': 'application/json',
- 'X-CMC_PRO_API_KEY': 'b14dfc5b-ec9d-4d53-8af7-fab0e5971120',
- }
- session = Session()
- session.headers.update(headers)
- try:
- response = session.get(url, params=parameters)
- filename = 'C:/wamp64/www/NFTBoard/api/res_' + datetime.datetime.now().strftime("%Y%m%d%H%M") + '.txt'
- fo = open(filename, 'w+', encoding='utf-8')
- fo.write(response.text)
- fo.close()
- data = json.loads(response.text)
- print(data)
- print('資料總筆數 = %s' % data['status']['total_count'])
- count = 1
- # NFT資料總筆數
- nft_total_count = 0
- for d in data['data']:
- tags = d['tags']
- # 檢查是否為NFT
- if 'collectibles-nfts' in tags:
- nft_total_count += 1
- '''
- {"id":2416,"name":"THETA","symbol":"THETA","slug":"theta","num_market_pairs":61,"date_added":"2018-01-17T00:00:00.000Z","tags":["media","collectibles-nfts","content-creation","video","huobi-capital"],"max_supply":1000000000,"circulating_supply":1000000000,"total_supply":1000000000,"platform":null,"cmc_rank":18,"last_updated":"2021-07-01T07:47:06.000Z","quote":{"USD":{"price":6.47444235747614,"volume_24h":226106772.68725023,"percent_change_1h":-0.78042098,"percent_change_24h":-5.36040215,"percent_change_7d":-6.02352245,"percent_change_30d":-11.54236731,"percent_change_60d":-40.7622901,"percent_change_90d":-45.50203437,"market_cap":6474442357.47614,"last_updated":"2021-07-01T07:47:06.000Z"}}}
- '''
- id = d['id']
- icon = 'https://s2.coinmarketcap.com/static/img/coins/64x64/' + str(id) + '.png'
- name = d['name']
- symbol = d['symbol']
- price = d['quote']['USD']['price']
- if price >= 1:
- price = format(price, '.2f')
- elif price >= 0.1 and price < 1:
- price = format(price, '.4f')
- elif price >= 0.01 and price < 0.1:
- price = format(price, '.4f')
- elif price >= 0.001 and price < 0.01:
- price = format(price, '.6f')
- elif price >= 0.0001 and price < 0.001:
- price = format(price, '.8f')
- elif price >= 0.00001 and price < 0.0001:
- price = format(price, '.9f')
- else:
- price = "<$0.00001"
- percent_change_24h = format(d['quote']['USD']['percent_change_24h'], '.2f')
- percent_change_7d = format(d['quote']['USD']['percent_change_7d'], '.2f')
- market_cap = int(d['quote']['USD']['market_cap'])
- volume_24h = int(d['quote']['USD']['volume_24h'])
- circulating_supply = str(int(d['circulating_supply'])) + ' ' + symbol
- last_7d = "https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/" + str(id) + ".png"
- print('\n%s.%s' % (nft_total_count, symbol))
- print('icon: %s' % icon)
- print('name: %s' % name)
- print('price: $%s' % price)
- print('percent_change_24h: %s%%' % percent_change_24h)
- print('percent_change_7d: %s%%' % percent_change_7d)
- print('market_cap: $%s' % market_cap)
- print('volume_24h: $%s' % volume_24h)
- print('circulating_supply: %s' % circulating_supply)
- print('last_7d: %s' % last_7d)
- print('NFT資料總筆數 = %s' % nft_total_count)
- except (ConnectionError, Timeout, TooManyRedirects) as e:
- print(e)
|