2021070102.py 3.7 KB

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