Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
"""Eksterne tjenester: geoip, static-assets-version, random-proxy status, Matomo."""
|
|
import httpx
|
|
|
|
from config import (ASSETS_VERSION_URL, GEOIP_DB, MATOMO_BASE, MATOMO_SITE,
|
|
MATOMO_TOKEN, RP_API_KEY, RP_BASE_URL)
|
|
|
|
_geoip_reader = None
|
|
|
|
|
|
def geoip_country(ip):
|
|
global _geoip_reader
|
|
try:
|
|
if _geoip_reader is None:
|
|
import maxminddb
|
|
_geoip_reader = maxminddb.open_database(GEOIP_DB)
|
|
rec = _geoip_reader.get(ip) or {}
|
|
c = rec.get("country") or {}
|
|
iso = c.get("iso_code") or ""
|
|
name = (c.get("names") or {}).get("en") or ""
|
|
flag = "".join(chr(0x1F1E6 + ord(ch) - 65) for ch in iso) if len(iso) == 2 and iso.isalpha() else ""
|
|
return {"iso": iso, "name": name, "flag": flag}
|
|
except Exception:
|
|
return {"iso": "", "name": "", "flag": ""}
|
|
|
|
|
|
async def assets_version():
|
|
try:
|
|
async with httpx.AsyncClient(timeout=6) as c:
|
|
r = await c.get(ASSETS_VERSION_URL)
|
|
return r.text.strip()[:40]
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
async def rp_status():
|
|
if not RP_API_KEY:
|
|
return {"error": "ingen RP_API_KEY konfigureret"}
|
|
try:
|
|
async with httpx.AsyncClient(timeout=6) as c:
|
|
r = await c.get(RP_BASE_URL + "/status", headers={"X-WEIRCON-RANDOM-IP": RP_API_KEY})
|
|
d = r.json()
|
|
for p in d.get("proxies", []):
|
|
p["geo"] = geoip_country(p.get("egress_ip") or "")
|
|
rot = d.get("rotation") or {}
|
|
if rot.get("next_egress_ip"):
|
|
rot["geo"] = geoip_country(rot["next_egress_ip"])
|
|
d["inflight_total"] = sum(int(p.get("inflight") or 0) for p in d.get("proxies", []))
|
|
return d
|
|
except Exception as exc:
|
|
return {"error": str(exc)[:200]}
|
|
|
|
|
|
async def matomo_summary():
|
|
if not MATOMO_TOKEN:
|
|
return None
|
|
params = {"module": "API", "method": "VisitsSummary.get", "idSite": MATOMO_SITE,
|
|
"period": "day", "date": "last7", "format": "JSON", "token_auth": MATOMO_TOKEN}
|
|
try:
|
|
async with httpx.AsyncClient(timeout=10) as c:
|
|
r = await c.get(f"{MATOMO_BASE}/index.php", params=params)
|
|
data = r.json()
|
|
days = list(data.values()) if isinstance(data, dict) else []
|
|
return {"visits_7d": sum(int(d.get("nb_visits", 0) or 0) for d in days if isinstance(d, dict)),
|
|
"actions_7d": sum(int(d.get("nb_actions", 0) or 0) for d in days if isinstance(d, dict))}
|
|
except Exception:
|
|
return None
|