message_coin_detail.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 使用 httpx 获取 coinmarketcap 最新数字币数据
  4. '''
  5. import time
  6. import os
  7. import sys
  8. import httpx
  9. import re
  10. from playwright.sync_api import sync_playwright
  11. sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
  12. from utils.utils_send_gotify import *
  13. def get_chainalert():
  14. coin_follow_list = ['BTC', 'ETH', 'SOL', 'GRASS', 'SUI', 'DOGE', 'ARB', 'ATH', 'MOVE']
  15. text = ''
  16. with sync_playwright() as playwright:
  17. browser = playwright.chromium.launch(headless=True)
  18. context = browser.new_context()
  19. page = context.new_page()
  20. page.goto("https://chainalert.me/data/marketprice")
  21. for coin in coin_follow_list:
  22. page.fill("#marketprice_search_input", coin)
  23. page.click("#marketprice_search_btn")
  24. page.wait_for_timeout(1500)
  25. elements = page.query_selector_all("#listdata_header > li:nth-child(2) > div")
  26. text_content = [element.text_content() for element in elements]
  27. temp_data = [s.replace('↑', '').replace('↓', '').split() for s in text_content]
  28. print(temp_data[0])
  29. text += f'Ranking: {temp_data[0][0]}\n'
  30. text += f'Name: {temp_data[0][1]}\n'
  31. text += f'Price: {temp_data[0][2]}\n'
  32. text += f'24H Transaction Volume: {temp_data[0][3]}\n'
  33. text += f'24H Price Change: {temp_data[0][4]}\n'
  34. text += f'Market Capitalization: {temp_data[0][5]}\n'
  35. text += f'Diluted Market Value: {temp_data[0][6]}\n'
  36. text += '\n\n'
  37. time.sleep(1)
  38. page.goto("https://chainalert.me/data/")
  39. page.wait_for_timeout(1500)
  40. elements = page.query_selector_all(
  41. '#greedy-galay-div > div.greedy_index-svg-value-div > div.greedy-svg-value > strong')
  42. text_content = [element.text_content() for element in elements]
  43. print(f'VIX: {text_content[0] if text_content else "No Data"}')
  44. if text_content:
  45. text += f'VIX: {text_content[0]}'
  46. else:
  47. text += 'VIX: No Data'
  48. context.close()
  49. browser.close()
  50. return text
  51. def get_gas_value():
  52. url = "https://a5.maiziqianbao.net/api/v1/chains/EVM/1/gas_price"
  53. headers = {
  54. "Host": "a5.maiziqianbao.net",
  55. "Connection": "keep-alive",
  56. "x-req-token": "MDbO4FsaSUPdjCdvTUs2zY4V3rnvvYatvYyjz7SfY+aCJ8r+RFm06X2dGR8eEDK7Gc5g1TLEQySEhGerRXbDT/NS+e5QAWRU68yD8m4y/aKK+TBkIv90VwvxmvYId2BVoDPDHQCGG4o3EqRWkS93eV0twYQ7w7qvNUj2e3tpDcUZYuplPyLozgYVTegFPnDk",
  57. "Accept": "*/*",
  58. "x-app-type": "iOS-5",
  59. "x-app-ver": "1.0.1",
  60. "x-app-udid": "419815AD-3015-4B5A-92CA-3BCBED24ACEC",
  61. "x-app-locale": "en",
  62. "Accept-Language": "zh-Hans-CN;q=1.0, en-CN;q=0.9",
  63. "Accept-Encoding": "br;q=1.0, gzip;q=0.9, deflate;q=0.8",
  64. "User-Agent": "MathGas/1.0.1 (MathWallet.MathGas; build:3; macOS 13.5.0) Alamofire/5.4.4"
  65. }
  66. response = httpx.get(url, headers=headers)
  67. if response.status_code != 200:
  68. print("Error:", response.status_code)
  69. return None
  70. if not response.json():
  71. print("Not Find GAS Data. Error: No response")
  72. return None
  73. remove_last_n_chars = lambda n, n_chars=9: int(str(n)[:-n_chars]) if len(str(n)) > n_chars else n
  74. result = '\nGAS:\n'
  75. try:
  76. data = response.json()['data']
  77. fastest = remove_last_n_chars(data['fastest']['price'])
  78. fast = remove_last_n_chars(data['fast']['price'])
  79. standard = remove_last_n_chars(data['standard']['price'])
  80. low = remove_last_n_chars(data['low']['price'])
  81. base = remove_last_n_chars(data['base']['price'])
  82. result += f'fastest: {fastest}\nfast: {fast}\nstandard: {standard}\nlow: {low}\nbase: {base}'
  83. return result
  84. except Exception as e:
  85. print(e)
  86. return None
  87. def main():
  88. retry = 5
  89. text = ''
  90. for i in range(retry):
  91. try:
  92. text = get_chainalert()
  93. gas = get_gas_value()
  94. if gas:
  95. text += gas
  96. if text:
  97. GotifyNotifier('Real-time coin price\n', text, 'AgfOJESqDKftBTQ').send_message()
  98. else:
  99. print('No Data')
  100. break
  101. except Exception as e:
  102. print(e)
  103. time.sleep(5)
  104. if __name__ == '__main__':
  105. main()