30 lines
723 B
Python
30 lines
723 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import re
|
||
|
|
|
||
|
|
_URL_PATTERN = re.compile(r"https?://[^\s<>\"')\]]+")
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_timestamp(ts: int | float | str) -> float:
|
||
|
|
if isinstance(ts, (int, float)):
|
||
|
|
return float(ts) if ts > 1e12 else float(ts)
|
||
|
|
try:
|
||
|
|
return float(ts)
|
||
|
|
except (ValueError, TypeError):
|
||
|
|
return 0.0
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_user_id(raw_id: str | int) -> str:
|
||
|
|
return str(raw_id).strip()
|
||
|
|
|
||
|
|
|
||
|
|
def extract_mentions(text: str, bot_names: list[str] | None = None) -> list[str]:
|
||
|
|
mentions = re.findall(r"@(\S+)", text)
|
||
|
|
if bot_names:
|
||
|
|
return [m for m in mentions if m in bot_names]
|
||
|
|
return mentions
|
||
|
|
|
||
|
|
|
||
|
|
def extract_urls(text: str) -> list[str]:
|
||
|
|
return _URL_PATTERN.findall(text)
|