coin_detail.py 7.0 KB

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