message_coin_detail.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
  5. import httpx
  6. from datetime import datetime
  7. from utils.utils_send_gotify import *
  8. retry_count = 5
  9. def fetch_coin_data(target):
  10. url = "https://api.chainalert.me/"
  11. headers = {
  12. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  13. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
  14. }
  15. payload = {
  16. "method": "listData",
  17. "params": [datetime.now().strftime("%Y-%m-%d"), "MARKETPRICE", '', 0, 9999.0, target]
  18. }
  19. with httpx.Client() as client:
  20. try:
  21. response = client.post(url, headers=headers, data=payload, timeout=3)
  22. except Exception as e:
  23. # print(f"Target: {target} failed to fetch data. error: {str(e)}")
  24. client.close()
  25. return False
  26. if response.status_code != 200:
  27. client.close()
  28. # print(f"{target} failed to fetch data. status code: {response.status_code}")
  29. return False
  30. else:
  31. text = ''
  32. data = response.json()
  33. target_data = eval(data['result'][0]['data'])
  34. target_data = target_data[0]
  35. print(target_data)
  36. # 获取数据值
  37. name = target_data['name']
  38. rank = target_data['rank']
  39. price = target_data['item1']
  40. volume = target_data['item2']
  41. change = target_data['item3']
  42. market_cap = target_data['item4']
  43. dilute = target_data['item5']
  44. logoUrl = target_data['logoUrl']
  45. # 拼接到 text 中
  46. text += f'Name: {name}\n'
  47. text += f'Ranking: {rank}\n'
  48. text += f'Price: {price}\n'
  49. text += f'24H Transaction Volume: {volume}\n'
  50. text += f'24H Price Change: {change}\n'
  51. text += f'Market Capitalization: {market_cap}\n'
  52. text += f'Diluted Market Value: {dilute}\n'
  53. text += f'Logo: {logoUrl}\n'
  54. return text
  55. def fetch_vix_data():
  56. url = "https://api.chainalert.me/"
  57. headers = {
  58. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  59. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
  60. }
  61. payload = {
  62. "method": "listData",
  63. "params": ['', "GREEDY_INDEX", 1, 0, 1, '']
  64. }
  65. with httpx.Client() as client:
  66. try:
  67. response = client.post(url, headers=headers, data=payload, timeout=3)
  68. except Exception as e:
  69. # print(f"failed to fetch VIX data. error: {str(e)}")
  70. client.close()
  71. return False
  72. if response.status_code != 200:
  73. client.close()
  74. # print(f"Failed to fetch VIX data. status code: {response.status_code}")
  75. return False
  76. else:
  77. data = response.json()
  78. vix_data = eval(data['result'][0]['data'])
  79. vix_data = vix_data[0]
  80. print(vix_data)
  81. greedy = vix_data['greedy']
  82. level = vix_data['level']
  83. text = f'VIX data: {greedy}\nLevel: {level}'
  84. return text
  85. def fetch_gas_data():
  86. url = "https://a5.maiziqianbao.net/api/v1/chains/EVM/1/gas_price"
  87. headers = {
  88. "Host": "a5.maiziqianbao.net",
  89. "Connection": "keep-alive",
  90. "x-req-token": "MDbO4FsaSUPdjCdvTUs2zY4V3rnvvYatvYyjz7SfY+aCJ8r+RFm06X2dGR8eEDK7Gc5g1TLEQySEhGerRXbDT/NS+e5QAWRU68yD8m4y/aKK+TBkIv90VwvxmvYId2BVoDPDHQCGG4o3EqRWkS93eV0twYQ7w7qvNUj2e3tpDcUZYuplPyLozgYVTegFPnDk",
  91. "Accept": "*/*",
  92. "x-app-type": "iOS-5",
  93. "x-app-ver": "1.0.1",
  94. "x-app-udid": "419815AD-3015-4B5A-92CA-3BCBED24ACEC",
  95. "x-app-locale": "en",
  96. "Accept-Language": "zh-Hans-CN;q=1.0, en-CN;q=0.9",
  97. "Accept-Encoding": "br;q=1.0, gzip;q=0.9, deflate;q=0.8",
  98. "User-Agent": "MathGas/1.0.1 (MathWallet.MathGas; build:3; macOS 13.5.0) Alamofire/5.4.4"
  99. }
  100. with httpx.Client() as client:
  101. response = client.get(url, headers=headers)
  102. if response.status_code != 200:
  103. client.close()
  104. print("Error:", response.status_code)
  105. return False
  106. if not response.json():
  107. client.close()
  108. print("Not Find GAS Data. Error: No response")
  109. return False
  110. remove_last_n_chars = lambda n, n_chars=9: int(str(n)[:-n_chars]) if len(str(n)) > n_chars else n
  111. result = '\nGAS:\n'
  112. try:
  113. data = response.json()['data']
  114. fastest = remove_last_n_chars(data['fastest']['price'])
  115. fast = remove_last_n_chars(data['fast']['price'])
  116. standard = remove_last_n_chars(data['standard']['price'])
  117. low = remove_last_n_chars(data['low']['price'])
  118. base = remove_last_n_chars(data['base']['price'])
  119. print(f'fastest: {fastest} - fast: {fast} - standard: {standard} - low: {low} - base: {base}')
  120. result += f'fastest: {fastest}\nfast: {fast}\nstandard: {standard}\nlow: {low}\nbase: {base}'
  121. return result
  122. except Exception as e:
  123. print(e)
  124. return False
  125. def main():
  126. text = ''
  127. # 获取币币实时价格
  128. target_list = ['btc', 'eth', 'sol', 'grass', 'sui', 'doge', 'arb', 'ath', 'move', 'pepe']
  129. for target in target_list:
  130. for retry in range(1, retry_count + 1):
  131. result = fetch_coin_data(target)
  132. if result:
  133. text += result + '\n\n'
  134. break
  135. else:
  136. print(f"{target} Failed to fetch data. retry: {retry}")
  137. if retry == retry_count:
  138. text += f"{target} Failed to fetch data. retry count: {retry}"
  139. # 获取恐慌指数
  140. for retry in range(1, retry_count + 1):
  141. result = fetch_vix_data()
  142. if result:
  143. text += result + '\n\n'
  144. break
  145. else:
  146. print(f"Failed to fetch VIX data. retry: {retry}")
  147. if retry == retry_count:
  148. text += f"Failed to fetch VIX data. retry count: {retry}"
  149. # 获取gas
  150. for retry in range(1, retry_count + 1):
  151. result = fetch_gas_data()
  152. if result:
  153. text += result + '\n\n'
  154. break
  155. else:
  156. print(f"Failed to fetch Gas data. retry: {retry}")
  157. if retry == retry_count:
  158. text += f"Failed to fetch Gas data. retry count: {retry}"
  159. if text:
  160. GotifyNotifier('Real-time coin price\n', text, 'AgfOJESqDKftBTQ').send_message()
  161. else:
  162. print('No Data')
  163. if __name__ == "__main__":
  164. main()