message_coinmarketcap.py 7.3 KB

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