Skip to content

Commit

Permalink
添加 request_interval 参数
Browse files Browse the repository at this point in the history
每次请求等待的时间,避免请求频繁触发风控
lemisky committed Jun 2, 2024
1 parent 0c7a909 commit e2b206b
Showing 3 changed files with 29 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/aligo/apis/Aligo.py
Original file line number Diff line number Diff line change
@@ -62,7 +62,8 @@ def __init__(
request_failed_delay: float = 3,
requests_timeout: float = None,
login_timeout: float = None,
re_login: bool = True
re_login: bool = True,
request_interval: int = 0,
):
"""
Aligo
@@ -78,6 +79,7 @@ def __init__(
:param requests_timeout: (可选) 应网友提议,添加 requests timeout 参数
:param login_timeout: (可选) 登录超时时间,单位:秒。
:param re_login: refresh_token 失效后是否继续登录(弹出二维码或邮件,需等待) fix #73
:param request_interval: 每次请求等待的时间,避免请求频繁触发风控
level, use_aria2, proxies, port, email 可以通过 配置文件 配置默认值,在 <用户家目录>/.aligo/config.json5 中
```json5
@@ -97,23 +99,25 @@ def __init__(
if config.exists():
config = json.loads(config.read_text(encoding='utf8'))
if level == logging.DEBUG and 'level' in config:
level = config.get('level')
level = config.get('level', level)
if not use_aria2:
use_aria2 = config.get('use_aria2')
use_aria2 = config.get('use_aria2', use_aria2)
if proxies is None:
proxies = config.get('proxies')
proxies = config.get('proxies', proxies)
if port is None:
port = config.get('port')
port = config.get('port', port)
if email is None:
email = config.get('email')
email = config.get('email', email)
if request_failed_delay == 3:
requests_timeout = config.get('request_failed_delay', 3)
if requests_timeout is None:
requests_timeout = config.get('requests_timeout')
if requests_timeout is None:
login_timeout = config.get('login_timeout')
requests_timeout = config.get('requests_timeout', requests_timeout)
if login_timeout is None:
login_timeout = config.get('login_timeout', login_timeout)
if re_login is True:
re_login = config.get('re_login')
re_login = config.get('re_login', re_login)
if request_interval is True:
request_interval = config.get('request_interval', request_interval)

super().__init__(
name,
@@ -128,4 +132,5 @@ def __init__(
requests_timeout,
login_timeout,
re_login,
request_interval,
)
10 changes: 10 additions & 0 deletions src/aligo/core/Auth.py
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ def __init__(
email: EMailConfig = None,
request_failed_delay: float = 3,
requests_timeout: float = None,
request_interval: int = 0,
):
"""扫描二维码登录"""

@@ -102,6 +103,7 @@ def __init__(
email: EMailConfig = None,
request_failed_delay: float = 3,
requests_timeout: float = None,
request_interval: int = 0,
):
"""refresh_token 登录"""

@@ -118,6 +120,7 @@ def __init__(
requests_timeout: float = None,
login_timeout: float = None,
re_login: bool = True,
request_interval: int = 0,
):
"""..."""

@@ -134,6 +137,7 @@ def __init__(
requests_timeout: float = None,
login_timeout: float = None,
re_login: bool = True,
request_interval: int = 0,
):
"""登录验证
@@ -148,6 +152,7 @@ def __init__(
:param requests_timeout: same as requests timeout
:param login_timeout: 登录超时时间,单位:秒
:param re_login: refresh_token 失效后是否继续登录(弹出二维码或邮件,需等待) fix #73
:param request_interval: 每次请求等待的时间,避免请求频繁触发风控
"""
self._name_name = name
self._name = aligo_config_folder.joinpath(f'{name}.json')
@@ -159,6 +164,7 @@ def __init__(
self._requests_timeout = requests_timeout
self._login_timeout = LoginTimeout(login_timeout)
self._re_login = re_login
self._request_interval = request_interval

fmt = f'%(asctime)s.%(msecs)03d {name}.%(levelname)s %(message)s'

@@ -374,6 +380,10 @@ def _refresh_token(self, refresh_token=None, loop_call: bool = False):
def request(self, method: str, url: str, params: Dict = None,
headers: Dict = None, data=None, body: Dict = None) -> requests.Response:
"""统一请求方法"""
#
if self._request_interval:
time.sleep(self._request_interval)

# 删除值为None的键
if body is not None:
body = {k: v for k, v in body.items() if v is not None}
5 changes: 4 additions & 1 deletion src/aligo/core/BaseAligo.py
Original file line number Diff line number Diff line change
@@ -32,7 +32,8 @@ def __init__(
request_failed_delay: float = 3,
requests_timeout: float = None,
login_timeout: float = None,
re_login: bool = True
re_login: bool = True,
request_interval: int = 0,
):
"""
BaseAligo
@@ -48,6 +49,7 @@ def __init__(
:param requests_timeout: same as requests timeout
:param login_timeout: 登录超时时间,单位:秒
:param re_login: refresh_token 失效后是否继续登录(弹出二维码或邮件,需等待) fix #73
:param request_interval: 每次请求等待的时间,避免请求频繁触发风控
"""
self._auth = Auth(
name=name,
@@ -61,6 +63,7 @@ def __init__(
requests_timeout=requests_timeout,
login_timeout=login_timeout,
re_login=re_login,
request_interval=request_interval,
)
# 因为 self._auth.session 没有被重新赋值, 所以可以这么用
self._session: requests.Session = self._auth.session

0 comments on commit e2b206b

Please sign in to comment.