""" controllers/tenor.py — Tenor GIF API proxy. """ import json import urllib.request import urllib.parse from bottle import route, request, response _api_key: str = "" def init(tenor_cfg): global _api_key _api_key = tenor_cfg.api_key def register_routes(app): @app.route("/api/gifs/trending", method="GET") def gifs_trending(): if not _api_key: response.status = 400 return {"success": False, "error": "Tenor API key not configured"} try: limit = request.query.get("limit", "20") url = f"https://tenor.googleapis.com/v2/trending?key={_api_key}&limit={limit}&media_filter=tinygif" with urllib.request.urlopen(url) as resp: data = json.loads(resp.read()) return {"success": True, "results": data.get("results", [])} except Exception as e: response.status = 502 return {"success": False, "error": str(e)} @app.route("/api/gifs/search", method="GET") def gifs_search(): if not _api_key: response.status = 400 return {"success": False, "error": "Tenor API key not configured"} try: q = request.query.get("q", "") if not q: response.status = 400 return {"success": False, "error": "Missing query"} limit = request.query.get("limit", "20") url = f"https://tenor.googleapis.com/v2/search?key={_api_key}&q={urllib.parse.quote(q)}&limit={limit}&media_filter=tinygif" with urllib.request.urlopen(url) as resp: data = json.loads(resp.read()) return {"success": True, "results": data.get("results", [])} except Exception as e: response.status = 502 return {"success": False, "error": str(e)}