2626import copy
2727import json
2828import re
29- from typing import Callable , Dict , List , Optional , Union
29+ from typing import TYPE_CHECKING , Callable , Dict , List , Optional , Union
3030from urllib .parse import parse_qs , unquote , urlencode
3131
3232import httpx
3535from tenacity import retry , stop_after_attempt , wait_fixed
3636
3737import config
38+ from proxy .proxy_mixin import ProxyRefreshMixin
3839from tools import utils
3940
41+ if TYPE_CHECKING :
42+ from proxy .proxy_ip_pool import ProxyIpPool
43+
4044from .exception import DataFetchError
4145from .field import SearchType
4246
4347
44- class WeiboClient :
48+ class WeiboClient ( ProxyRefreshMixin ) :
4549
4650 def __init__ (
4751 self ,
@@ -51,6 +55,7 @@ def __init__(
5155 headers : Dict [str , str ],
5256 playwright_page : Page ,
5357 cookie_dict : Dict [str , str ],
58+ proxy_ip_pool : Optional ["ProxyIpPool" ] = None ,
5459 ):
5560 self .proxy = proxy
5661 self .timeout = timeout
@@ -59,9 +64,14 @@ def __init__(
5964 self .playwright_page = playwright_page
6065 self .cookie_dict = cookie_dict
6166 self ._image_agent_host = "https://i1.wp.com/"
67+ # 初始化代理池(来自 ProxyRefreshMixin)
68+ self .init_proxy_pool (proxy_ip_pool )
6269
6370 @retry (stop = stop_after_attempt (5 ), wait = wait_fixed (3 ))
6471 async def request (self , method , url , ** kwargs ) -> Union [Response , Dict ]:
72+ # 每次请求前检测代理是否过期
73+ await self ._refresh_proxy_if_expired ()
74+
6575 enable_return_response = kwargs .pop ("return_response" , False )
6676 async with httpx .AsyncClient (proxy = self .proxy ) as client :
6777 response = await client .request (method , url , timeout = self .timeout , ** kwargs )
0 commit comments