2021070101.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #This example uses Python 2.7 and the python-request library.
  2. from requests import Request, Session
  3. import datetime
  4. from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
  5. import json
  6. # https://coinmarketcap.com/api/documentation/v1/#section/Endpoint-Overview
  7. url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
  8. parameters = {
  9. 'start':'1',
  10. 'limit':'500',
  11. 'convert':'USD'
  12. }
  13. headers = {
  14. 'Accepts': 'application/json',
  15. 'X-CMC_PRO_API_KEY': 'b14dfc5b-ec9d-4d53-8af7-fab0e5971120',
  16. }
  17. session = Session()
  18. session.headers.update(headers)
  19. try:
  20. response = session.get(url, params=parameters)
  21. filename = 'C:/wamp64/www/NFTBoard/api/res_' + datetime.datetime.now().strftime("%Y%m%d%H%M") + '.txt'
  22. fo = open(filename, 'w+', encoding='utf-8')
  23. fo.write(response.text)
  24. fo.close()
  25. data = json.loads(response.text)
  26. print(data)
  27. print('資料總筆數 = %s' % data['status']['total_count'])
  28. count = 1
  29. # NFT資料總筆數
  30. nft_total_count = 0
  31. for d in data['data']:
  32. tags = d['tags']
  33. # 檢查是否為NFT
  34. if 'collectibles-nfts' in tags:
  35. nft_total_count += 1
  36. '''
  37. {"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"}}}
  38. '''
  39. id = d['id']
  40. icon = 'https://s2.coinmarketcap.com/static/img/coins/64x64/' + str(id) + '.png'
  41. name = d['name']
  42. symbol = d['symbol']
  43. price = d['quote']['USD']['price']
  44. if price >= 1:
  45. price = format(price, '.2f')
  46. elif price >= 0.1 and price < 1:
  47. price = format(price, '.4f')
  48. elif price >= 0.01 and price < 0.1:
  49. price = format(price, '.4f')
  50. elif price >= 0.001 and price < 0.01:
  51. price = format(price, '.6f')
  52. elif price >= 0.0001 and price < 0.001:
  53. price = format(price, '.8f')
  54. elif price >= 0.00001 and price < 0.0001:
  55. price = format(price, '.9f')
  56. else:
  57. price = "<$0.00001"
  58. percent_change_24h = format(d['quote']['USD']['percent_change_24h'], '.2f')
  59. percent_change_7d = format(d['quote']['USD']['percent_change_7d'], '.2f')
  60. market_cap = int(d['quote']['USD']['market_cap'])
  61. volume_24h = int(d['quote']['USD']['volume_24h'])
  62. circulating_supply = str(int(d['circulating_supply'])) + ' ' + symbol
  63. last_7d = "https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/" + str(id) + ".png"
  64. print('\n%s.%s' % (nft_total_count, symbol))
  65. print('icon: %s' % icon)
  66. print('name: %s' % name)
  67. print('price: $%s' % price)
  68. print('percent_change_24h: %s%%' % percent_change_24h)
  69. print('percent_change_7d: %s%%' % percent_change_7d)
  70. print('market_cap: $%s' % market_cap)
  71. print('volume_24h: $%s' % volume_24h)
  72. print('circulating_supply: %s' % circulating_supply)
  73. print('last_7d: %s' % last_7d)
  74. print('NFT資料總筆數 = %s' % nft_total_count)
  75. except (ConnectionError, Timeout, TooManyRedirects) as e:
  76. print(e)