qrpa 1.0.39__py3-none-any.whl → 1.0.41__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of qrpa might be problematic. Click here for more details.
- qrpa/feishu_client.py +76 -2
- qrpa/fun_web.py +3 -6
- {qrpa-1.0.39.dist-info → qrpa-1.0.41.dist-info}/METADATA +1 -1
- {qrpa-1.0.39.dist-info → qrpa-1.0.41.dist-info}/RECORD +6 -6
- {qrpa-1.0.39.dist-info → qrpa-1.0.41.dist-info}/WHEEL +0 -0
- {qrpa-1.0.39.dist-info → qrpa-1.0.41.dist-info}/top_level.txt +0 -0
qrpa/feishu_client.py
CHANGED
|
@@ -3,19 +3,29 @@ import requests
|
|
|
3
3
|
import json
|
|
4
4
|
import time
|
|
5
5
|
import base64, os
|
|
6
|
+
import threading
|
|
6
7
|
from urllib.parse import urlparse
|
|
7
8
|
from typing import Optional, Dict, List, Any
|
|
9
|
+
from collections import deque
|
|
8
10
|
|
|
9
11
|
from .fun_base import log
|
|
10
12
|
|
|
11
13
|
class FeishuClient:
|
|
12
|
-
def __init__(self, app_id: str, app_secret: str):
|
|
14
|
+
def __init__(self, app_id: str, app_secret: str, max_requests_per_second: int = 90, skip_token_refresh: bool = False):
|
|
13
15
|
self.app_id = app_id
|
|
14
16
|
self.app_secret = app_secret
|
|
15
17
|
self.base_url = "https://open.feishu.cn/open-apis"
|
|
16
18
|
self.tenant_access_token = None
|
|
17
19
|
self.token_expire_time = 0
|
|
18
|
-
|
|
20
|
+
|
|
21
|
+
# 速率限制相关参数
|
|
22
|
+
self.max_requests_per_second = max_requests_per_second
|
|
23
|
+
self.request_times = deque() # 存储请求时间戳
|
|
24
|
+
self.rate_limit_lock = threading.Lock() # 保证线程安全
|
|
25
|
+
|
|
26
|
+
# 只有在不跳过token刷新时才获取token
|
|
27
|
+
if not skip_token_refresh:
|
|
28
|
+
self._refresh_token()
|
|
19
29
|
|
|
20
30
|
def _refresh_token(self):
|
|
21
31
|
url = f"{self.base_url}/auth/v3/tenant_access_token/internal"
|
|
@@ -41,8 +51,72 @@ class FeishuClient:
|
|
|
41
51
|
def _ensure_valid_token(self):
|
|
42
52
|
if not self.tenant_access_token or time.time() > self.token_expire_time:
|
|
43
53
|
self._refresh_token()
|
|
54
|
+
|
|
55
|
+
def _wait_for_rate_limit(self):
|
|
56
|
+
"""
|
|
57
|
+
实现请求频率限制,确保不超过90次/秒
|
|
58
|
+
使用滑动窗口算法控制请求频率
|
|
59
|
+
"""
|
|
60
|
+
with self.rate_limit_lock:
|
|
61
|
+
current_time = time.time()
|
|
62
|
+
|
|
63
|
+
# 清理1秒前的请求记录
|
|
64
|
+
while self.request_times and current_time - self.request_times[0] > 1.0:
|
|
65
|
+
self.request_times.popleft()
|
|
66
|
+
|
|
67
|
+
# 如果当前秒内请求数已达到限制,则等待
|
|
68
|
+
if len(self.request_times) >= self.max_requests_per_second:
|
|
69
|
+
# 计算需要等待的时间
|
|
70
|
+
oldest_request_time = self.request_times[0]
|
|
71
|
+
wait_time = 1.0 - (current_time - oldest_request_time)
|
|
72
|
+
if wait_time > 0:
|
|
73
|
+
log(f"请求频率限制: 等待 {wait_time:.3f} 秒")
|
|
74
|
+
time.sleep(wait_time)
|
|
75
|
+
# 重新获取当前时间并清理过期记录
|
|
76
|
+
current_time = time.time()
|
|
77
|
+
while self.request_times and current_time - self.request_times[0] > 1.0:
|
|
78
|
+
self.request_times.popleft()
|
|
79
|
+
|
|
80
|
+
# 记录当前请求时间
|
|
81
|
+
self.request_times.append(current_time)
|
|
82
|
+
|
|
83
|
+
def get_current_request_rate(self):
|
|
84
|
+
"""
|
|
85
|
+
获取当前的请求频率 (最近1秒内的请求数)
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
int: 最近1秒内的请求数
|
|
89
|
+
"""
|
|
90
|
+
with self.rate_limit_lock:
|
|
91
|
+
current_time = time.time()
|
|
92
|
+
# 清理1秒前的请求记录
|
|
93
|
+
while self.request_times and current_time - self.request_times[0] > 1.0:
|
|
94
|
+
self.request_times.popleft()
|
|
95
|
+
return len(self.request_times)
|
|
96
|
+
|
|
97
|
+
def set_rate_limit(self, max_requests_per_second: int):
|
|
98
|
+
"""
|
|
99
|
+
动态设置请求频率限制
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
max_requests_per_second: 每秒最大请求数
|
|
103
|
+
"""
|
|
104
|
+
with self.rate_limit_lock:
|
|
105
|
+
self.max_requests_per_second = max_requests_per_second
|
|
106
|
+
log(f"请求频率限制已更新为: {max_requests_per_second} 次/秒")
|
|
107
|
+
|
|
108
|
+
def reset_rate_limit_counter(self):
|
|
109
|
+
"""
|
|
110
|
+
重置频率限制计数器
|
|
111
|
+
"""
|
|
112
|
+
with self.rate_limit_lock:
|
|
113
|
+
self.request_times.clear()
|
|
114
|
+
log("频率限制计数器已重置")
|
|
44
115
|
|
|
45
116
|
def _make_request(self, method: str, url: str, **kwargs):
|
|
117
|
+
# 在发送请求前进行速率限制检查
|
|
118
|
+
self._wait_for_rate_limit()
|
|
119
|
+
|
|
46
120
|
self._ensure_valid_token()
|
|
47
121
|
|
|
48
122
|
headers = kwargs.get('headers', {})
|
qrpa/fun_web.py
CHANGED
|
@@ -5,7 +5,6 @@ from playwright.sync_api import Page
|
|
|
5
5
|
from .fun_base import log, send_exception
|
|
6
6
|
from .time_utils import get_current_datetime
|
|
7
7
|
|
|
8
|
-
|
|
9
8
|
def fetch(page: Page, url: str, params: Optional[Union[dict, list, str]] = None, headers: Optional[dict] = None, config:
|
|
10
9
|
Optional[dict] = None) -> dict:
|
|
11
10
|
"""
|
|
@@ -23,6 +22,7 @@ Optional[dict] = None) -> dict:
|
|
|
23
22
|
raise ValueError("headers 参数必须是 dict 或 None")
|
|
24
23
|
|
|
25
24
|
try:
|
|
25
|
+
page.wait_for_load_state('load')
|
|
26
26
|
response = page.evaluate("""
|
|
27
27
|
async ({ url, params, extraHeaders }) => {
|
|
28
28
|
try {
|
|
@@ -64,7 +64,6 @@ Optional[dict] = None) -> dict:
|
|
|
64
64
|
raise send_exception()
|
|
65
65
|
# return {"error": "fetch error", "message": str(e)}
|
|
66
66
|
|
|
67
|
-
|
|
68
67
|
def fetch_via_iframe(page: Page, target_domain: str, url: str, params: Optional[Union[dict, list, str]] = None, config:
|
|
69
68
|
Optional[dict] = None) -> dict:
|
|
70
69
|
"""
|
|
@@ -126,9 +125,8 @@ Optional[dict] = None) -> dict:
|
|
|
126
125
|
|
|
127
126
|
return response
|
|
128
127
|
except Exception as e:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
128
|
+
raise send_exception()
|
|
129
|
+
# return {"error": "iframe_exception", "message": str(e)}
|
|
132
130
|
|
|
133
131
|
# 找到一个页面里面所有的iframe
|
|
134
132
|
def find_all_iframe(page: Page):
|
|
@@ -137,7 +135,6 @@ def find_all_iframe(page: Page):
|
|
|
137
135
|
log("找到 iframe:", frame.url)
|
|
138
136
|
return [frame.url for frame in frames]
|
|
139
137
|
|
|
140
|
-
|
|
141
138
|
# 全屏幕截图
|
|
142
139
|
def full_screen_shot(web_page: Page, config):
|
|
143
140
|
# 设置页面的视口大小为一个较大的值,确保截图高清
|
|
@@ -2,12 +2,12 @@ qrpa/RateLimitedSender.py,sha256=hqvb1qspDFaW4RsLuVufylOrefkMgixANKeBaGEqYb4,142
|
|
|
2
2
|
qrpa/__init__.py,sha256=E6f2Fju31RxpBLG8_e5dMDgYxhqSgv8LR0Zq9ceJJJM,1109
|
|
3
3
|
qrpa/db_migrator.py,sha256=2VmhzcMsU0MKpl-mNCwKyV8tLTqyEysSpP27-S_rQZ8,21862
|
|
4
4
|
qrpa/feishu_bot_app.py,sha256=6r2YqCAMUN7y3F7onoABRmHC7S-UPOLHwbhsQnQ3IRc,9452
|
|
5
|
-
qrpa/feishu_client.py,sha256=
|
|
5
|
+
qrpa/feishu_client.py,sha256=gXvyhf7r-IqeDhPjM01SfsGf17t8g1ZwUAkhymBkBeg,17544
|
|
6
6
|
qrpa/feishu_logic.py,sha256=yuwb-LeZiHKGlz-W8JobinorHonVa8L-5h12WxnU7_Q,67508
|
|
7
7
|
qrpa/fun_base.py,sha256=qg6SvR-GEj2TclB1OL9eLu711jV-bysXJ5Eh2gW9pE8,10600
|
|
8
8
|
qrpa/fun_excel.py,sha256=kXR3fU0XqsdBuSEToqA3TzYWSP5Cgh1aZ1QPgaZ0y08,114758
|
|
9
9
|
qrpa/fun_file.py,sha256=yzjDV16WL5vRys7J4uQcNzIFkX4D5MAlSCwxcD-mwQo,11966
|
|
10
|
-
qrpa/fun_web.py,sha256=
|
|
10
|
+
qrpa/fun_web.py,sha256=F6cxwwOxB8twg2XK73oAvKla0FqpwDYJiM37CWmFwXo,6322
|
|
11
11
|
qrpa/fun_win.py,sha256=-LnTeocdTt72NVH6VgLdpAT9_C5oV9okeudXG6CftMA,8034
|
|
12
12
|
qrpa/shein_daily_report_model.py,sha256=H8oZmIN5Pyqe306W1_xuz87lOqLQ_LI5RjXbaxDkIzE,12589
|
|
13
13
|
qrpa/shein_excel.py,sha256=Nz9mdcdh8nZ9A1eDfGxS1r3E3H9aAv5m5HtHcphDjDU,114996
|
|
@@ -20,7 +20,7 @@ qrpa/temu_lib.py,sha256=hYB59zsLS3m3NTic_duTwPMOTSxlHyQVa8OhHnHm-1g,7199
|
|
|
20
20
|
qrpa/time_utils.py,sha256=ef0hhbN_6b-gcnz5ETIVOoxemIMvcxGVGGIRnHnGaBo,29564
|
|
21
21
|
qrpa/time_utils_example.py,sha256=shHOXKKF3QSzb0SHsNc34M61wEkkLuM30U9X1THKNS8,8053
|
|
22
22
|
qrpa/wxwork.py,sha256=gIytG19DZ5g7Tsl0-W3EbjfSnpIqZw-ua24gcB78YEg,11264
|
|
23
|
-
qrpa-1.0.
|
|
24
|
-
qrpa-1.0.
|
|
25
|
-
qrpa-1.0.
|
|
26
|
-
qrpa-1.0.
|
|
23
|
+
qrpa-1.0.41.dist-info/METADATA,sha256=vHy4gY4jkPqHunulu-KOo1qmjHk7sXBw19crJKL2O1Q,231
|
|
24
|
+
qrpa-1.0.41.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
25
|
+
qrpa-1.0.41.dist-info/top_level.txt,sha256=F6T5igi0fhXDucPPUbmmSC0qFCDEsH5eVijfVF48OFU,5
|
|
26
|
+
qrpa-1.0.41.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|