message_coinmarketcap.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 使用 httpx 获取 coinmarketcap 最新数字币数据
  4. '''
  5. import time
  6. import os
  7. import sys
  8. import httpx
  9. import re
  10. sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
  11. from utils.utils_send_gotify import *
  12. def get_coinmarketcap_coin_price(url, target):
  13. headers = {
  14. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107"
  15. }
  16. resp = httpx.get(url=url, headers=headers)
  17. if resp.status_code == 301:
  18. print(resp.text)
  19. return None
  20. elif resp.status_code != 200:
  21. print(resp.status_code)
  22. return None
  23. resp.encoding = 'utf-8'
  24. page = resp.text
  25. text = re.findall('<strong>(.*?)</p></div><div class="sc-65e7f566-0', page)[0] if re.findall(
  26. '<strong>(.*?)</p></div><div class="sc-65e7f566-0', page) else 'No Data'
  27. text = re.sub(r'</strong>', '', text)
  28. text = re.sub(r'<!-- -->', '', text)
  29. result = f'target: {target}\n'
  30. prices = re.findall(r'\$(\d+\.\d+)', text)
  31. if prices:
  32. result += f'prices: ${prices[0]}\n'
  33. volumes = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  34. if volumes:
  35. result += f'24-hour trading volume: ${volumes[1]}\n'
  36. change = re.findall(r'(up|down) (\d+\.\d+)%', text)
  37. if change:
  38. c = ' '.join(change[0])
  39. result += f'change: {c}%\n'
  40. live_market_cap = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  41. if live_market_cap:
  42. result += f'live market cap: ${live_market_cap[2]}\n'
  43. max_circulating_supply = re.findall(r'(\d{1,3}(?:,\d{3})*)', text)
  44. if max_circulating_supply:
  45. result += f'max circulating supply: {max_circulating_supply[-1]}'
  46. return result + '\n\n'
  47. def get_vix_data():
  48. url = 'https://api-ddc-wscn.awtmt.com/market/real?'
  49. headers = {
  50. "Accept": "*/*",
  51. "Accept-Encoding": "gzip, deflate, br, zstd",
  52. "Accept-Language": "zh-CN,zh;q=0.9",
  53. "Connection": "keep-alive",
  54. "Host": "api-ddc-wscn.awtmt.com",
  55. "If-None-Match": "AMJ+W5ydwGIw9oqT3fOBeQ==",
  56. "Origin": "https://wallstreetcn.com",
  57. "Referer": "https://wallstreetcn.com/",
  58. "Sec-CH-UA": '"Chromium";v="130", "Brave";v="130", "Not?A_Brand";v="99"',
  59. "Sec-CH-UA-Mobile": "?0",
  60. "Sec-CH-UA-Platform": '"macOS"',
  61. "Sec-Fetch-Dest": "empty",
  62. "Sec-Fetch-Mode": "cors",
  63. "Sec-Fetch-Site": "cross-site",
  64. "Sec-GPC": "1",
  65. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
  66. }
  67. params = {
  68. "fields": "symbol,en_name,prod_name,last_px,px_change,px_change_rate,high_px,low_px,open_px,preclose_px,market_value,turnover_volume,turnover_ratio,turnover_value,dyn_pb_rate,amplitude,dyn_pe,trade_status,circulation_value,update_time,price_precision,week_52_high,week_52_low,static_pe,source",
  69. "prod_code": "VIX.OTC"
  70. }
  71. resp = httpx.get(url=url, headers=headers, params=params)
  72. if resp.status_code != 200:
  73. print(resp.status_code)
  74. return None
  75. data = resp.json()['data']['snapshot']['VIX.OTC']
  76. result = f'{data[1]}:{data[2]}'
  77. return result
  78. if __name__ == '__main__':
  79. url_list = [
  80. ['BTC', 'https://coinmarketcap.com/currencies/bitcoin/'],
  81. ['ETH', 'https://coinmarketcap.com/currencies/ethereum/'],
  82. ['SOL', 'https://coinmarketcap.com/currencies/solana/'],
  83. ['SUI', 'https://coinmarketcap.com/currencies/sui/'],
  84. ['DOGE', 'https://coinmarketcap.com/currencies/dogecoin/'],
  85. ['ARB', 'https://coinmarketcap.com/currencies/arbitrum/'],
  86. ['ATH', 'https://coinmarketcap.com/currencies/aethir/']
  87. ]
  88. text = ''
  89. for data_list in url_list:
  90. res = get_coinmarketcap_coin_price(data_list[1], data_list[0])
  91. if res:
  92. text += res
  93. time.sleep(2)
  94. res = get_vix_data()
  95. if res:
  96. text += res
  97. print(text)
  98. if text:
  99. GotifyNotifier('Real-time coin price\n', text).send_message()
  100. else:
  101. print('No Data')