"""플랫폼 교체 경계. 전송 결과 불명은 재시도 가능한 실패와 절대 섞지 않는다.""" import re import unicodedata class SocialError(RuntimeError): def __init__(self, code="SOCIAL_FAILED", *, reauth=False): super().__init__(code) self.reauth = reauth class SocialOutcomeUnknown(SocialError): pass URL = re.compile(r"https?://[^\s]+", re.I) def weighted_length(text: str, provider: int = 1) -> int: text = unicodedata.normalize("NFC", text) if provider == 2: return len(text) # 보수적으로 이모지 조합의 각 코드포인트도 센다. 공식 가중치보다 작게 세지 않는다. def weight(c): n = ord(c) return ( 1 if n <= 0x10FF or 0x2000 <= n <= 0x200D or 0x2010 <= n <= 0x201F or 0x2032 <= n <= 0x2037 else 2 ) length = 0 pos = 0 for match in URL.finditer(text): length += sum(weight(c) for c in text[pos : match.start()]) + 23 pos = match.end() return length + sum(weight(c) for c in text[pos:]) def adapter(provider: int): if provider == 2: from services.external import threads return threads raise SocialError("UNSUPPORTED_PROVIDER")