message_coinmarketcap.py 3.9 KB

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