message_coinmarketcap.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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():
  13. url_list = [
  14. ['BTC', 'https://coinmarketcap.com/currencies/bitcoin/'],
  15. ['ETH', 'https://coinmarketcap.com/currencies/ethereum/'],
  16. ['SOL', 'https://coinmarketcap.com/currencies/solana/'],
  17. ['GRASS', 'https://coinmarketcap.com/currencies/grass/'],
  18. ['SUI', 'https://coinmarketcap.com/currencies/sui/'],
  19. ['DOGE', 'https://coinmarketcap.com/currencies/dogecoin/'],
  20. ['ARB', 'https://coinmarketcap.com/currencies/arbitrum/'],
  21. ['ATH', 'https://coinmarketcap.com/currencies/aethir/']
  22. ]
  23. headers = {
  24. "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"
  25. }
  26. result = ''
  27. for data_list in url_list:
  28. url = data_list[1]
  29. target = data_list[0]
  30. try:
  31. resp = httpx.get(url=url, headers=headers)
  32. except Exception as e:
  33. print(f'target: {target}, {e}')
  34. time.sleep(5)
  35. continue
  36. result += f'{target}\n'
  37. if resp.status_code == 301:
  38. print(resp.text)
  39. return None
  40. elif resp.status_code != 200:
  41. print(resp.status_code)
  42. return None
  43. resp.encoding = 'utf-8'
  44. page = resp.text
  45. text = re.findall('<strong>(.*?)sc-65e7f566-0', page)[0] if re.findall(
  46. '<strong>(.*?)sc-65e7f566-0', page) else 'No Data'
  47. text = re.sub(r'</strong>', '', text)
  48. text = re.sub(r'<!-- -->', '', text)
  49. text = re.sub(r'</p></div></div><div class="', '', text)
  50. prices = re.findall(r'\$(\d+\.\d+)', text)
  51. if prices:
  52. result += f'prices: ${prices[0]}\n'
  53. volumes = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  54. if volumes:
  55. result += f'24-hour trading volume: ${volumes[1]}\n'
  56. change = re.findall(r'(up|down) (\d+\.\d+)%', text)
  57. if change:
  58. c = ' '.join(change[0])
  59. result += f'change: {c}%\n'
  60. live_market_cap = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  61. if live_market_cap:
  62. if len(live_market_cap) == 3:
  63. result += f'live market cap: ${live_market_cap[2]}\n'
  64. else:
  65. pass
  66. max_circulating_supply = re.findall(r'(\d{1,3}(?:,\d{3})*)', text)
  67. if max_circulating_supply:
  68. result += f'max circulating supply: {max_circulating_supply[-1]}\n'
  69. result += '\n'
  70. print(f'已获取 {target} 数据')
  71. time.sleep(2)
  72. return result + '\n\n'
  73. def get_vix_data():
  74. url = 'https://api-ddc-wscn.awtmt.com/market/real?'
  75. headers = {
  76. "Accept": "*/*",
  77. "Accept-Encoding": "gzip, deflate, br, zstd",
  78. "Accept-Language": "zh-CN,zh;q=0.9",
  79. "Connection": "keep-alive",
  80. "Host": "api-ddc-wscn.awtmt.com",
  81. "If-None-Match": "AMJ+W5ydwGIw9oqT3fOBeQ==",
  82. "Origin": "https://wallstreetcn.com",
  83. "Referer": "https://wallstreetcn.com/",
  84. "Sec-CH-UA": '"Chromium";v="130", "Brave";v="130", "Not?A_Brand";v="99"',
  85. "Sec-CH-UA-Mobile": "?0",
  86. "Sec-CH-UA-Platform": '"macOS"',
  87. "Sec-Fetch-Dest": "empty",
  88. "Sec-Fetch-Mode": "cors",
  89. "Sec-Fetch-Site": "cross-site",
  90. "Sec-GPC": "1",
  91. "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"
  92. }
  93. params = {
  94. "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",
  95. "prod_code": "VIX.OTC"
  96. }
  97. resp = httpx.get(url=url, headers=headers, params=params)
  98. if resp.status_code != 200:
  99. print(resp.status_code)
  100. return None
  101. try:
  102. data = resp.json()['data']['snapshot']['VIX.OTC']
  103. except Exception as e:
  104. print(e)
  105. return None
  106. result = f'{data[1]}:{data[2]}'
  107. print('已获取 vix 指数')
  108. return result + '\n\n'
  109. def get_crypto_fear_and_greed_index():
  110. url = 'https://coinmarketcap.com/charts/fear-and-greed-index/'
  111. headers = {
  112. "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"
  113. }
  114. time.sleep(1)
  115. resp = httpx.get(url=url, headers=headers)
  116. if resp.status_code != 200:
  117. print(resp.status_code)
  118. return None
  119. resp.encoding = 'utf-8'
  120. page = resp.text
  121. print('已获取 crypto 贪婪指数')
  122. re_text = re.findall(
  123. 'fearGreedIndexData":\{"currentIndex":\{"score":(.*?),"maxScore":100,"name":"(.*?)","updateTime":"(.*?)"}',
  124. page)
  125. if re_text:
  126. text = f'crypto fear and greed index: {re_text[0][0]}\n{re_text[0][1]}\nupdate time: {re_text[0][2]}\n'
  127. return text
  128. else:
  129. return None
  130. def get_gas_value():
  131. url = "https://a5.maiziqianbao.net/api/v1/chains/EVM/1/gas_price"
  132. headers = {
  133. "Host": "a5.maiziqianbao.net",
  134. "Connection": "keep-alive",
  135. "x-req-token": "MDbO4FsaSUPdjCdvTUs2zY4V3rnvvYatvYyjz7SfY+aCJ8r+RFm06X2dGR8eEDK7Gc5g1TLEQySEhGerRXbDT/NS+e5QAWRU68yD8m4y/aKK+TBkIv90VwvxmvYId2BVoDPDHQCGG4o3EqRWkS93eV0twYQ7w7qvNUj2e3tpDcUZYuplPyLozgYVTegFPnDk",
  136. "Accept": "*/*",
  137. "x-app-type": "iOS-5",
  138. "x-app-ver": "1.0.1",
  139. "x-app-udid": "419815AD-3015-4B5A-92CA-3BCBED24ACEC",
  140. "x-app-locale": "en",
  141. "Accept-Language": "zh-Hans-CN;q=1.0, en-CN;q=0.9",
  142. "Accept-Encoding": "br;q=1.0, gzip;q=0.9, deflate;q=0.8",
  143. "User-Agent": "MathGas/1.0.1 (MathWallet.MathGas; build:3; macOS 13.5.0) Alamofire/5.4.4"
  144. }
  145. response = httpx.get(url, headers=headers)
  146. if response.status_code != 200:
  147. print("Error:", response.status_code)
  148. return None
  149. if not response.json():
  150. print("Not Find GAS Data. Error: No response")
  151. return None
  152. remove_last_n_chars = lambda n, n_chars=9: int(str(n)[:-n_chars]) if len(str(n)) > n_chars else n
  153. result = '\nGAS:\n'
  154. try:
  155. data = response.json()['data']
  156. fastest = remove_last_n_chars(data['fastest']['price'])
  157. fast = remove_last_n_chars(data['fast']['price'])
  158. standard = remove_last_n_chars(data['standard']['price'])
  159. low = remove_last_n_chars(data['low']['price'])
  160. base = remove_last_n_chars(data['base']['price'])
  161. result += f'fastest: {fastest}\nfast: {fast}\nstandard: {standard}\nlow: {low}\nbase: {base}'
  162. return result
  163. except Exception as e:
  164. print(e)
  165. return None
  166. def main():
  167. text = ''
  168. text = get_coinmarketcap_coin_price()
  169. res = get_vix_data()
  170. if res:
  171. text += res
  172. fear_and_greed = get_crypto_fear_and_greed_index()
  173. if fear_and_greed:
  174. text += fear_and_greed
  175. gas = get_gas_value()
  176. if gas:
  177. text += gas
  178. if text:
  179. print(text)
  180. GotifyNotifier('Real-time coin price\n', text, 'AgfOJESqDKftBTQ').send_message()
  181. else:
  182. print('No Data')
  183. if __name__ == '__main__':
  184. main()