message_coinmarketcap.py 7.3 KB

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