message_coin_detail.py 6.4 KB

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