forked from genewildish/Mainline
feat: Introduce translate module for Google Translate integration and location-based language detection, and add a new filter module.
This commit is contained in:
41
engine/translate.py
Normal file
41
engine/translate.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""
|
||||
Google Translate wrapper and location→language detection.
|
||||
Depends on: sources (for LOCATION_LANGS).
|
||||
"""
|
||||
|
||||
import re
|
||||
import json
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
|
||||
from engine.sources import LOCATION_LANGS
|
||||
|
||||
_TRANSLATE_CACHE = {}
|
||||
|
||||
|
||||
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, target_lang):
|
||||
"""Translate headline via Google Translate API (zero dependencies)."""
|
||||
key = (title, target_lang)
|
||||
if key in _TRANSLATE_CACHE:
|
||||
return _TRANSLATE_CACHE[key]
|
||||
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
|
||||
_TRANSLATE_CACHE[key] = result
|
||||
return result
|
||||
Reference in New Issue
Block a user