Spaces:
Paused
Paused
| # src/core/utils.py | |
| import json | |
| import time | |
| import base64 | |
| import re | |
| import requests | |
| from src.core.logger import logger # 从新的位置导入 logger | |
| from src.core.auth_token_manager import AuthTokenManager # 从新的位置导入 AuthTokenManager | |
| from app import CONFIG, DEFAULT_HEADERS # 导入 CONFIG 和 DEFAULT_HEADERS | |
| class Utils: | |
| def organize_search_results(search_results): | |
| if not search_results or 'results' not in search_results: | |
| return '' | |
| results = search_results['results'] | |
| formatted_results = [] | |
| for index, result in enumerate(results): | |
| title = result.get('title', '未知标题') | |
| url = result.get('url', '#') | |
| preview = result.get('preview', '无预览内容') | |
| formatted_result = f"\r\n资料[{index}]: {title}\r\n{preview}\r\n\n[Link]({url})\r\n" | |
| formatted_results.append(formatted_result) | |
| return '\n\n'.join(formatted_results) | |
| def create_auth_headers(token_manager, model, is_return=False): | |
| # 接收 token_manager 实例作为参数 | |
| return token_manager.get_next_token_for_model(model, is_return) | |
| def get_proxy_options(): | |
| proxy = CONFIG["API"]["PROXY"] | |
| proxy_options = {} | |
| if proxy: | |
| logger.info(f"使用代理: {proxy}", "Server") | |
| if proxy.startswith("socks5://"): | |
| proxy_options["proxy"] = proxy | |
| if '@' in proxy: | |
| auth_part = proxy.split('@')[0].split('://')[1] | |
| if ':' in auth_part: | |
| username, password = auth_part.split(':') | |
| proxy_options["proxy_auth"] = (username, password) | |
| else: | |
| proxy_options["proxies"] = {"https": proxy, "http": proxy} | |
| return proxy_options |