-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
90 lines (74 loc) · 2.5 KB
/
Copy pathutils.py
File metadata and controls
90 lines (74 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import asyncio
import random
import logging
logger = logging.getLogger(__name__)
CAPTCHA_SIGNALS = [
"captcha",
"капча",
"robot",
"i am not a robot",
"я не робот",
"blocked",
"access denied",
"403 forbidden",
"cloudflare",
"ddos-guard",
"please verify",
"проверьте, что вы",
]
def random_delay(min_sec: float, max_sec: float):
import time
time.sleep(random.uniform(min_sec, max_sec))
async def async_random_delay(min_sec: float, max_sec: float):
await asyncio.sleep(random.uniform(min_sec, max_sec))
def is_captcha_page(content: str) -> bool:
lower = content.lower()
return any(signal in lower for signal in CAPTCHA_SIGNALS)
async def retry_async(
coro_fn,
max_attempts: int = 3,
backoff_factor: float = 2.0,
label: str = "",
):
"""
Call coro_fn() up to max_attempts times, with exponential backoff.
Raises the last exception if all attempts fail.
"""
last_exc = None
for attempt in range(1, max_attempts + 1):
try:
return await coro_fn()
except Exception as exc:
last_exc = exc
wait = backoff_factor ** (attempt - 1) * 2
logger.warning(
f"[retry {attempt}/{max_attempts}] {label}: "
f"{type(exc).__name__}: {exc} — ждём {wait:.1f}с"
)
await asyncio.sleep(wait)
raise last_exc
def find_pain_points(texts: list[str], keywords: list[str]) -> tuple[bool, str]:
"""
Scan review texts for pain-point keywords.
Returns (found: bool, quote: str) where quote is the best matching
sentence (up to 300 chars) prefixed with the triggering keyword.
Matching is case-insensitive and checks whole-word presence to reduce
false positives (e.g. "записаться" should not fire on "записи книги").
"""
import re
kw_lower = [k.lower() for k in keywords]
best_quote = ""
for text in texts:
text_lower = text.lower()
for kw in kw_lower:
if kw in text_lower:
# split into sentences on ., !, ?, \n
sentences = re.split(r"[.!?\n]+", text)
for s in sentences:
if kw in s.lower():
quote = s.strip()
if len(quote) > len(best_quote):
best_quote = quote[:300]
if best_quote:
return True, f'[{kw}] "{best_quote}"'
return False, ""