message_coin_detail.py 6.3 KB

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