forked from genewildish/Mainline
- Add Config dataclass with get_config()/set_config() for injection - Add Config.from_args() for CLI argument parsing (testable) - Add platform font path detection (Darwin/Linux) - Bound translate cache with @lru_cache(maxsize=500) - Add fixtures for external dependencies (network, feeds, config) - Add 15 tests for Config class, from_args, and platform detection This enables testability by: - Allowing config injection instead of global mutable state - Supporting custom argv in from_args() for testing - Providing reusable fixtures for mocking network/filesystem - Preventing unbounded memory growth in translation cache Fixes: _arg_value/_arg_int not accepting custom argv
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""
|
|
Google Translate wrapper and location→language detection.
|
|
Depends on: sources (for LOCATION_LANGS).
|
|
"""
|
|
|
|
import json
|
|
import re
|
|
import urllib.parse
|
|
import urllib.request
|
|
from functools import lru_cache
|
|
|
|
from engine.sources import LOCATION_LANGS
|
|
|
|
TRANSLATE_CACHE_SIZE = 500
|
|
|
|
|
|
@lru_cache(maxsize=TRANSLATE_CACHE_SIZE)
|
|
def _translate_cached(title: str, target_lang: str) -> str:
|
|
"""Cached translation implementation."""
|
|
try:
|
|
q = urllib.parse.quote(title)
|
|
url = (
|
|
"https://translate.googleapis.com/translate_a/single"
|
|
f"?client=gtx&sl=en&tl={target_lang}&dt=t&q={q}"
|
|
)
|
|
req = urllib.request.Request(url, headers={"User-Agent": "mainline/0.1"})
|
|
resp = urllib.request.urlopen(req, timeout=5)
|
|
data = json.loads(resp.read())
|
|
result = "".join(p[0] for p in data[0] if p[0]) or title
|
|
except Exception:
|
|
result = title
|
|
return result
|
|
|
|
|
|
def detect_location_language(title):
|
|
"""Detect if headline mentions a location, return target language."""
|
|
title_lower = title.lower()
|
|
for pattern, lang in LOCATION_LANGS.items():
|
|
if re.search(pattern, title_lower):
|
|
return lang
|
|
return None
|
|
|
|
|
|
def translate_headline(title: str, target_lang: str) -> str:
|
|
"""Translate headline via Google Translate API (zero dependencies)."""
|
|
return _translate_cached(title, target_lang)
|