# -*- coding: utf-8 -*- ''' 使用 httpx 获取 coinmarketcap 最新数字币数据 ''' import time import os import sys import httpx import re sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto')) from utils.utils_send_gotify import * def get_coinmarketcap_coin_price(): url_list = [ ['BTC', 'https://coinmarketcap.com/currencies/bitcoin/'], ['ETH', 'https://coinmarketcap.com/currencies/ethereum/'], ['SOL', 'https://coinmarketcap.com/currencies/solana/'], ['GRASS', 'https://coinmarketcap.com/currencies/grass/'], ['SUI', 'https://coinmarketcap.com/currencies/sui/'], ['DOGE', 'https://coinmarketcap.com/currencies/dogecoin/'], ['ARB', 'https://coinmarketcap.com/currencies/arbitrum/'], ['ATH', 'https://coinmarketcap.com/currencies/aethir/'] ] headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107" } result = '' for data_list in url_list: url = data_list[1] target = data_list[0] try: resp = httpx.get(url=url, headers=headers) except Exception as e: print(f'target: {target}, {e}') time.sleep(5) continue result += f'{target}\n' if resp.status_code == 301: print(resp.text) return None elif resp.status_code != 200: print(resp.status_code) return None resp.encoding = 'utf-8' page = resp.text text = re.findall('(.*?)sc-65e7f566-0', page)[0] if re.findall( '(.*?)sc-65e7f566-0', page) else 'No Data' text = re.sub(r'', '', text) text = re.sub(r'', '', text) text = re.sub(r'

n_chars else n result = '\nGAS:\n' try: data = response.json()['data'] fastest = remove_last_n_chars(data['fastest']['price']) fast = remove_last_n_chars(data['fast']['price']) standard = remove_last_n_chars(data['standard']['price']) low = remove_last_n_chars(data['low']['price']) base = remove_last_n_chars(data['base']['price']) result += f'fastest: {fastest}\nfast: {fast}\nstandard: {standard}\nlow: {low}\nbase: {base}' return result except Exception as e: print(e) return None def main(): text = '' text = get_coinmarketcap_coin_price() res = get_vix_data() if res: text += res fear_and_greed = get_crypto_fear_and_greed_index() if fear_and_greed: text += fear_and_greed gas = get_gas_value() if gas: text += gas if text: print(text) GotifyNotifier('Real-time coin price\n', text, 'AgfOJESqDKftBTQ').send_message() else: print('No Data') if __name__ == '__main__': main()