message_coinmarketcap.py 4.0 KB

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