message_coin_detail.py 6.9 KB

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