message_coinmarketcap.py 7.5 KB

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