-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
513 lines (447 loc) · 18.5 KB
/
Copy pathutils.py
File metadata and controls
513 lines (447 loc) · 18.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import asyncio
import re
import unicodedata
from contextlib import asynccontextmanager
from datetime import datetime, timezone, timedelta
from typing import Any, Coroutine, List, Optional, Tuple, Callable, Awaitable, Dict, AsyncIterator
import logging
import psutil
import discord
from dateparser.search import search_dates
logger = logging.getLogger(__name__)
# Assuming config is imported from config.py where it's defined
# from .config import config # If in a package
# For standalone scripts, you might need to adjust import paths or pass config
# For this refactor, we'll assume config can be imported if placed correctly.
# If main_bot.py initializes config, then other modules import it from there or config.py
from config import config
from rate_limiter import get_discord_edit_limiter
def format_article_time(dt: Optional[datetime]) -> str:
"""Format datetime for article display: 12-hour human readable, no timezone, no decimals."""
if not dt:
return ""
try:
local = dt.astimezone()
month_abbr = local.strftime("%b")
day = str(local.day)
year = local.strftime("%Y")
hour_12 = local.hour % 12 or 12
minute = local.minute
am_pm = "AM" if local.hour < 12 else "PM"
return f"{month_abbr} {day}, {year} at {hour_12}:{minute:02d} {am_pm}"
except Exception:
return ""
def chunk_text(text: str, max_length: int = config.EMBED_MAX_LENGTH) -> List[str]:
if not text: return [""]
chunks = []
current_chunk = ""
for line in text.splitlines(keepends=True):
if len(current_chunk) + len(line) > max_length:
if current_chunk: chunks.append(current_chunk)
current_chunk = line
while len(current_chunk) > max_length: # Handle very long lines
chunks.append(current_chunk[:max_length])
current_chunk = current_chunk[max_length:]
else:
current_chunk += line
if current_chunk: chunks.append(current_chunk)
return chunks if chunks else [""]
def detect_urls(message_text: str) -> List[str]:
if not message_text: return []
# Basic URL detection, can be improved for more complex cases
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
return url_pattern.findall(message_text)
RELATIVE_DATE_PATTERN = re.compile(
r'\b('
r'yesterday|today|tomorrow|tonight|'
r'this\s+(?:morning|afternoon|evening)|'
r'last\s+\w+|next\s+\w+|'
r'\d+\s+(?:seconds?|minutes?|hours?|days?|weeks?|months?|years?)\s+(?:ago|from\\s+now)|'
r'in\s+\d+\s+(?:seconds?|minutes?|hours?|days?|weeks?|months?|years?)'
r')\b',
re.IGNORECASE,
)
def append_absolute_dates(
text: str, current_time: Optional[datetime] = None
) -> str:
"""Annotate relative date phrases in ``text`` with absolute timestamps.
Parameters
----------
text:
The input text potentially containing relative date expressions.
current_time:
Reference time used to resolve relative phrases. Defaults to the
current UTC time.
Returns
-------
str
Text where each detected relative date phrase is followed by its
absolute representation, e.g. ``"tomorrow (2024-05-10 00:00 UTC)"``.
Phrases already containing explicit dates are left unmodified.
"""
if not text:
return text
current_time = current_time or datetime.now(timezone.utc)
results = search_dates(text, settings={"RELATIVE_BASE": current_time})
if not results:
return text
for phrase, dt in results:
if "(" in phrase:
continue
if not RELATIVE_DATE_PATTERN.search(phrase):
continue
if dt.tzinfo is None:
dt = dt.replace(tzinfo=current_time.tzinfo)
pattern = re.compile(
rf"({re.escape(phrase)})(?!\s*\()",
re.IGNORECASE,
)
text = pattern.sub(
f"{phrase} ({dt.astimezone().strftime('%Y-%m-%d %H:%M %Z')})",
text,
)
return text
def clean_text_for_tts(text: str) -> str:
if not text:
return ""
# Explicitly replace directional quotes and backticks first for robustness.
text = text.replace('’', "'") # Right single quote
text = text.replace('‘', "'") # Left single quote
text = text.replace('“', '"') # Left double quote
text = text.replace('”', '"') # Right double quote
text = text.replace('`', "'") # Backtick
# 1. Normalize unicode characters to their closest ASCII equivalents.
# NFKC is aggressive and handles many "compatibility" characters like smart quotes.
text = unicodedata.normalize('NFKC', text)
# 2. Manually replace any remaining common special characters.
text = text.replace("—", "--") # Em-dash
# 3. Remove URLs and <think> tags.
text = re.sub(r"http[s]?://\S+", "", text)
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL | re.IGNORECASE)
# 4. Whitelist allowed characters.
# This strips out any remaining non-standard characters after normalization.
# We allow basic letters, numbers, punctuation, and whitespace.
allowed_chars = re.compile(r"[^a-zA-Z0-9\s.,!?'\"-]")
text = allowed_chars.sub('', text)
# 5. Clean up whitespace with care for newlines.
# Collapse horizontal whitespace on each line.
text = re.sub(r'[ \t]+', ' ', text)
# Remove leading whitespace from each line, but preserve blank lines.
text = re.sub(r'^[ \t]+', '', text, flags=re.MULTILINE)
# Remove trailing whitespace from each line.
text = re.sub(r'[ \t]+$', '', text, flags=re.MULTILINE)
# Reduce more than two consecutive newlines down to two.
text = re.sub(r'\n{3,}', '\n\n', text)
# Remove any leading/trailing whitespace from the whole block.
text = text.strip()
return text
def sanitize_moltbook_text_for_tts(text: str) -> str:
"""Make Moltbook-formatted post/comment text suitable for TTS.
- Converts ISO 8601 timestamps to natural language (e.g. 'February 1, 2026 at 12:45 AM').
- Replaces 'ID: [uuid](url)' or 'ID: uuid' or 'ID: `uuid`' with 'Post' so TTS does not read raw IDs.
"""
if not text or not text.strip():
return text
# Replace ISO date-time strings with readable form
iso_pattern = re.compile(
r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?:\d{2})?(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?"
)
def _replace_iso(match: re.Match) -> str:
raw = match.group(0)
try:
dt = datetime.fromisoformat(raw.replace("Z", "+00:00"))
# 12-hour human-readable: "February 1, 2026 at 12:45 AM"
hour12 = dt.hour % 12 or 12
return (
dt.strftime("%B ")
+ str(dt.day)
+ dt.strftime(", %Y at ")
+ str(hour12)
+ dt.strftime(":%M %p")
)
except (ValueError, TypeError):
return "" # omit unparseable timestamps
text = iso_pattern.sub(_replace_iso, text)
# Collapse any double spaces left by omitted timestamps
text = re.sub(r" +", " ", text)
# Replace "ID: [uuid](url)", "ID: `uuid`", or "ID: uuid" with "Post"
text = re.sub(
r"ID:\s*\[[^\]]+\]\([^)]+\)",
"Post",
text,
)
text = re.sub(
r"ID:\s*`[^`]+`",
"Post",
text,
)
text = re.sub(
r"ID:\s*[a-fA-F0-9-]{8,}(?:-[a-fA-F0-9-]{4,})*",
"Post",
text,
)
text = re.sub(
r"ID:\s*unknown",
"Post",
text,
flags=re.IGNORECASE,
)
return text
def parse_time_string_to_delta(time_str: str) -> Tuple[Optional[timedelta], Optional[str]]:
patterns = {
'd': r'(\d+)\s*d(?:ay(?:s)?)?',
'h': r'(\d+)\s*h(?:our(?:s)?|r(?:s)?)?',
'm': r'(\d+)\s*m(?:inute(?:s)?|in(?:s)?)?',
's': r'(\d+)\s*s(?:econd(?:s)?|ec(?:s)?)?'
}
delta_args = {'days': 0, 'hours': 0, 'minutes': 0, 'seconds': 0}
original_parts = []
time_str_processed = time_str.lower() # Process a copy
for key, pattern_regex in patterns.items():
for match in re.finditer(pattern_regex, time_str_processed):
value = int(match.group(1))
unit_full = {'d': 'days', 'h': 'hours', 'm': 'minutes', 's': 'seconds'}[key]
delta_args[unit_full] += value
original_parts.append(f"{value} {unit_full.rstrip('s') if value == 1 else unit_full}")
# Remove matched parts from the string to avoid re-matching
time_str_processed = re.sub(pattern_regex, "", time_str_processed)
if not any(val > 0 for val in delta_args.values()):
return None, None # No valid time units found
time_delta = timedelta(**delta_args)
descriptive_str = ", ".join(original_parts) if original_parts else "immediately"
# Fallback descriptive string if delta_args had values but original_parts somehow didn't form
if not descriptive_str and time_delta.total_seconds() > 0 :
descriptive_str = "a duration" # Should ideally not be reached if parsing is correct
return time_delta, descriptive_str
def is_admin_user(user_id: int) -> bool:
"""Return True if ``user_id`` is configured as an admin."""
if not config.ADMIN_USER_IDS:
return False
try:
user_id_int = int(user_id)
except (TypeError, ValueError):
return False
return user_id_int in config.ADMIN_USER_IDS
def cleanup_playwright_processes() -> int:
"""Kill lingering Playwright/Chromium processes.
Returns the number of processes terminated."""
killed = 0
# Only target processes associated with this bot's Playwright profile.
# Using the more specific ".pw-" avoids killing other unrelated Playwright
# instances the user might be running elsewhere.
markers = [".pw-"]
for proc in psutil.process_iter(["pid", "cmdline"]):
try:
cmdline = " ".join(proc.info.get("cmdline") or [])
if any(m in cmdline for m in markers):
proc.kill()
killed += 1
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
return killed
async def safe_followup_send(
interaction: discord.Interaction, *, error_hint: str = "", **kwargs
) -> discord.Message:
"""Send a followup message with fallback to channel.send if the token expired."""
try:
return await interaction.followup.send(**kwargs)
except discord.HTTPException as e:
status = getattr(e, "status", None)
code = getattr(e, "code", None)
token_lost = (status == 401 and code == 50027) or (status == 404 and code == 10062)
if token_lost:
logger.warning(
"Interaction token unavailable%s; falling back to alternate delivery",
" " + error_hint if error_hint else "",
)
if kwargs.get("ephemeral"):
logger.warning(
"Skipping public fallback for ephemeral message%s to avoid leaking response.",
" " + error_hint if error_hint else "",
)
content = kwargs.get("content")
if content and interaction.user:
try:
return await interaction.user.send(content)
except Exception as dm_exc:
logger.warning("Failed to DM user after token loss: %s", dm_exc)
raise
if interaction.channel:
kwargs_fallback = dict(kwargs)
# Remove parameters not supported by channel.send
kwargs_fallback.pop("ephemeral", None)
kwargs_fallback.pop("wait", None)
return await interaction.channel.send(**kwargs_fallback)
raise
def _retry_after_from_discord_exception(e: BaseException) -> float:
"""Extract retry_after in seconds from a Discord 429/RateLimited exception."""
if hasattr(e, "retry_after") and e.retry_after is not None:
return float(e.retry_after)
if hasattr(e, "response") and e.response is not None:
resp = e.response
ra = getattr(resp, "headers", None)
if ra is not None:
retry_after = ra.get("Retry-After") or ra.get("retry-after")
if retry_after is not None:
try:
return float(retry_after)
except (TypeError, ValueError):
pass
return 5.0
async def safe_message_edit(
message: discord.Message,
channel: discord.abc.Messageable,
*,
cleanup_old: bool = True,
**kwargs: Any,
) -> discord.Message:
"""Edit a message safely.
Throttles edits per channel to avoid Discord 429s. On 429, respects
Retry-After and retries once. If the underlying webhook has expired
(HTTP 401 with code 50027), a new message is sent to ``channel`` and,
optionally, the old message is removed.
Parameters
----------
message : :class:`discord.Message`
The message to edit.
channel : :class:`discord.abc.Messageable`
Channel used to send a replacement message if editing fails.
cleanup_old : bool, optional
Whether to delete ``message`` when a new one is sent due to webhook
expiration, by default ``True``.
Returns
-------
:class:`discord.Message`
The edited message, or the newly-sent replacement.
"""
limiter = get_discord_edit_limiter()
channel_id = getattr(message.channel, "id", None) or "unknown"
key = f"discord_edit:{channel_id}"
last_exc: BaseException | None = None
for attempt in range(2):
await limiter.await_slot(key)
try:
await message.edit(**kwargs)
return message
except discord.HTTPException as e:
last_exc = e
if e.status == 401 and getattr(e, "code", None) == 50027:
logger.warning(
"Webhook token expired during edit; sending new message"
)
new_msg = await channel.send(**kwargs)
if cleanup_old:
try:
await message.delete()
except discord.HTTPException:
pass
return new_msg
if e.status == 429:
retry_after = _retry_after_from_discord_exception(e)
headers = {"retry-after": str(retry_after)}
await limiter.record_response(key, 429, headers)
logger.info(
"Discord 429 on message edit; respecting retry_after=%.2fs, retrying once.",
retry_after,
)
continue
raise
except discord.errors.RateLimited as e:
last_exc = e
retry_after = _retry_after_from_discord_exception(e)
await limiter.record_response(key, 429, {"retry-after": str(retry_after)})
logger.info(
"Discord RateLimited on message edit; respecting retry_after=%.2fs, retrying once.",
retry_after,
)
continue
if last_exc is not None:
raise last_exc
return message
def start_post_processing_task(
coro: Coroutine[Any, Any, Any],
*,
progress_message: Optional[discord.Message] = None,
) -> asyncio.Task:
"""Run ``coro`` in the background and handle progress cleanup.
Parameters
----------
coro:
The coroutine performing the post-processing work.
progress_message:
Optional message indicating progress. It will be deleted when
``coro`` finishes.
"""
async def _runner() -> None:
try:
await coro
finally:
if progress_message:
try:
await progress_message.delete()
except discord.HTTPException:
pass
return asyncio.create_task(_runner())
@asynccontextmanager
async def temporary_status_message(
*,
interaction: Optional[discord.Interaction] = None,
channel: Optional[discord.abc.Messageable] = None,
initial_text: str,
send_kwargs: Optional[Dict[str, Any]] = None,
) -> AsyncIterator[Callable[[str], Awaitable[None]]]:
"""Post a temporary status message that can be updated and cleaned up automatically."""
message: Optional[discord.Message] = None
send_available = True
send_kwargs = dict(send_kwargs or {})
if "allowed_mentions" not in send_kwargs:
send_kwargs["allowed_mentions"] = discord.AllowedMentions.none()
async def update(text: str) -> None:
nonlocal message, send_available
if not send_available:
return
if message is None:
try:
if interaction is not None:
message = await safe_followup_send(
interaction,
content=text,
**send_kwargs,
)
elif channel is not None:
message = await channel.send(content=text, **send_kwargs)
else:
send_available = False
except Exception as send_exc: # noqa: BLE001
logger.debug("Failed to send status indicator: %s", send_exc, exc_info=True)
send_available = False
else:
target_channel: Optional[discord.abc.Messageable]
if interaction is not None:
target_channel = interaction.channel
else:
target_channel = channel
if not target_channel:
return
try:
message = await safe_message_edit(
message,
target_channel,
content=text,
)
except Exception as edit_exc: # noqa: BLE001
logger.debug("Failed to update status indicator: %s", edit_exc, exc_info=True)
await update(initial_text)
if not send_available:
async def noop(_: str) -> None:
return None
yield noop
return
try:
yield update
finally:
if message:
try:
await message.delete()
except discord.HTTPException:
pass