Clean code

This commit is contained in:
yuanmengqi
2025-07-23 16:05:39 +00:00
parent 73de48af75
commit 5d219e7a5b
3 changed files with 29 additions and 29 deletions

View File

@@ -33,7 +33,7 @@ class ProxyPool:
self.load_proxies_from_file(config_file)
def load_proxies_from_file(self, config_file: str):
"""从配置文件加载代理列表"""
"""Load proxy list from config file"""
try:
with open(config_file, 'r') as f:
proxy_configs = json.load(f)
@@ -54,7 +54,7 @@ class ProxyPool:
def add_proxy(self, host: str, port: int, username: str = None,
password: str = None, protocol: str = "http"):
"""添加代理到池中"""
"""Add proxy to pool"""
proxy = ProxyInfo(host=host, port=port, username=username,
password=password, protocol=protocol)
with self.lock:
@@ -62,19 +62,19 @@ class ProxyPool:
logger.info(f"Added proxy {host}:{port}")
def get_next_proxy(self) -> Optional[ProxyInfo]:
"""获取下一个可用的代理"""
"""Get next available proxy"""
with self.lock:
if not self.proxies:
return None
# 过滤掉失败次数过多的代理
# Filter out proxies with too many failures
active_proxies = [p for p in self.proxies if self._is_proxy_available(p)]
if not active_proxies:
logger.warning("No active proxies available")
return None
# 轮询选择代理
# Round-robin selection of proxy
proxy = active_proxies[self.current_index % len(active_proxies)]
self.current_index += 1
proxy.last_used = time.time()
@@ -82,22 +82,22 @@ class ProxyPool:
return proxy
def _is_proxy_available(self, proxy: ProxyInfo) -> bool:
"""检查代理是否可用"""
"""Check if proxy is available"""
if not proxy.is_active:
return False
if proxy.failed_count >= self.max_failures:
# 检查是否过了冷却时间
# Check if cooldown time has passed
if time.time() - proxy.last_used < self.cooldown_time:
return False
else:
# 重置失败计数
# Reset failure count
proxy.failed_count = 0
return True
def mark_proxy_failed(self, proxy: ProxyInfo):
"""标记代理失败"""
"""Mark proxy as failed"""
with self.lock:
proxy.failed_count += 1
if proxy.failed_count >= self.max_failures:
@@ -105,13 +105,13 @@ class ProxyPool:
f"(failures: {proxy.failed_count})")
def mark_proxy_success(self, proxy: ProxyInfo):
"""标记代理成功"""
"""Mark proxy as successful"""
with self.lock:
proxy.failed_count = 0
def test_proxy(self, proxy: ProxyInfo, test_url: str = "http://httpbin.org/ip",
timeout: int = 10) -> bool:
"""测试代理是否正常工作"""
"""Test if proxy is working"""
try:
proxy_url = self._format_proxy_url(proxy)
proxies = {
@@ -133,14 +133,14 @@ class ProxyPool:
return False
def _format_proxy_url(self, proxy: ProxyInfo) -> str:
"""格式化代理URL"""
"""Format proxy URL"""
if proxy.username and proxy.password:
return f"{proxy.protocol}://{proxy.username}:{proxy.password}@{proxy.host}:{proxy.port}"
else:
return f"{proxy.protocol}://{proxy.host}:{proxy.port}"
def get_proxy_dict(self, proxy: ProxyInfo) -> Dict[str, str]:
"""获取requests库使用的代理字典"""
"""Get proxy dictionary for requests library"""
proxy_url = self._format_proxy_url(proxy)
return {
'http': proxy_url,
@@ -148,7 +148,7 @@ class ProxyPool:
}
def test_all_proxies(self, test_url: str = "http://httpbin.org/ip"):
"""测试所有代理"""
"""Test all proxies"""
logger.info("Testing all proxies...")
working_count = 0
@@ -163,7 +163,7 @@ class ProxyPool:
return working_count
def get_stats(self) -> Dict:
"""获取代理池统计信息"""
"""Get proxy pool statistics"""
with self.lock:
total = len(self.proxies)
active = len([p for p in self.proxies if self._is_proxy_available(p)])
@@ -176,18 +176,18 @@ class ProxyPool:
'success_rate': active / total if total > 0 else 0
}
# 全局代理池实例
# Global proxy pool instance
_proxy_pool = None
def get_global_proxy_pool() -> ProxyPool:
"""获取全局代理池实例"""
"""Get global proxy pool instance"""
global _proxy_pool
if _proxy_pool is None:
_proxy_pool = ProxyPool()
return _proxy_pool
def init_proxy_pool(config_file: str = None):
"""初始化全局代理池"""
"""Initialize global proxy pool"""
global _proxy_pool
_proxy_pool = ProxyPool(config_file)
return _proxy_pool