fixed gif api
This commit is contained in:
parent
57257f6799
commit
75380bb5ed
|
|
@ -1,3 +0,0 @@
|
|||
# Anime Channel
|
||||
|
||||
This directory is dedicated to discussions and content about anime.
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
# Games Channel
|
||||
|
||||
This directory is dedicated to discussions and content about games.
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
# Politics Channel
|
||||
|
||||
This directory is dedicated to discussions and content about politics.
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -5,48 +5,188 @@ controllers/tenor.py — Tenor GIF API proxy.
|
|||
import json
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import logging
|
||||
from bottle import route, request, response
|
||||
|
||||
# Настройка логирования
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_api_key: str = ""
|
||||
_api_key: str = "aw3wqJXDc2LXcyL0zRg0v7LvXQCh69hciOxRMk23V1mWYxfql4NhKglzArCEHwBs"
|
||||
|
||||
|
||||
def init(tenor_cfg):
|
||||
global _api_key
|
||||
if hasattr(tenor_cfg, 'api_key') and tenor_cfg.api_key:
|
||||
_api_key = tenor_cfg.api_key
|
||||
logger.info(f"Tenor API key initialized: {_api_key[:10]}...")
|
||||
else:
|
||||
logger.warning("No Tenor API key provided, using default key")
|
||||
|
||||
|
||||
def register_routes(app):
|
||||
@app.route("/api/gifs/trending", method="GET")
|
||||
def gifs_trending():
|
||||
logger.debug(f"Trending request received with params: {dict(request.query)}")
|
||||
|
||||
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:
|
||||
# Get query parameters with defaults
|
||||
page = request.query.get("page", "1")
|
||||
per_page = request.query.get("limit", "24")
|
||||
customer_id = request.query.get("customer_id", "")
|
||||
locale = request.query.get("locale", "us") # Default to US
|
||||
content_filter = request.query.get("content_filter", "off")
|
||||
format_filter = request.query.get("format_filter", "")
|
||||
|
||||
# Build URL according to KLIPY API spec
|
||||
base_url = f"https://api.klipy.com/api/v1/{_api_key}/gifs/trending"
|
||||
params = {
|
||||
"page": page,
|
||||
"per_page": per_page,
|
||||
"content_filter": content_filter,
|
||||
"locale": locale # Always include locale
|
||||
}
|
||||
|
||||
# Add optional parameters if provided
|
||||
if customer_id:
|
||||
params["customer_id"] = customer_id
|
||||
if format_filter:
|
||||
params["format_filter"] = format_filter
|
||||
|
||||
url = f"{base_url}?{urllib.parse.urlencode(params)}"
|
||||
logger.debug(f"Requesting URL: {url}")
|
||||
|
||||
# Create request with headers
|
||||
req = urllib.request.Request(url, headers={
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': 'KLIPY-Proxy/1.0'
|
||||
})
|
||||
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
data = json.loads(resp.read())
|
||||
return {"success": True, "results": data.get("results", [])}
|
||||
except Exception as e:
|
||||
logger.debug(f"KLIPY response: {json.dumps(data)[:200]}...")
|
||||
|
||||
# Transform KLIPY response to match expected format
|
||||
response_data = {
|
||||
"success": data.get("result", False),
|
||||
"results": data.get("data", {}).get("data", []),
|
||||
"pagination": {
|
||||
"current_page": data.get("data", {}).get("current_page", 1),
|
||||
"per_page": data.get("data", {}).get("per_page", 24),
|
||||
"has_next": data.get("data", {}).get("has_next", False)
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(f"Returning {len(response_data['results'])} results")
|
||||
return response_data
|
||||
|
||||
except urllib.error.HTTPError as e:
|
||||
error_msg = f"HTTP Error {e.code}: {e.reason}"
|
||||
logger.error(error_msg)
|
||||
try:
|
||||
error_body = e.read().decode('utf-8')
|
||||
logger.error(f"Error body: {error_body}")
|
||||
except:
|
||||
pass
|
||||
response.status = 502
|
||||
return {"success": False, "error": str(e)}
|
||||
return {"success": False, "error": error_msg}
|
||||
except urllib.error.URLError as e:
|
||||
error_msg = f"Connection error: {str(e)}"
|
||||
logger.error(error_msg)
|
||||
response.status = 502
|
||||
return {"success": False, "error": error_msg}
|
||||
except Exception as e:
|
||||
error_msg = f"Unexpected error: {str(e)}"
|
||||
logger.error(error_msg)
|
||||
response.status = 502
|
||||
return {"success": False, "error": error_msg}
|
||||
|
||||
@app.route("/api/gifs/search", method="GET")
|
||||
def gifs_search():
|
||||
logger.debug(f"Search request received with params: {dict(request.query)}")
|
||||
|
||||
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:
|
||||
|
||||
# Get query parameters with defaults
|
||||
page = request.query.get("page", "1")
|
||||
per_page = request.query.get("limit", "24")
|
||||
customer_id = request.query.get("customer_id", "")
|
||||
locale = request.query.get("locale", "us") # Default to US
|
||||
content_filter = request.query.get("content_filter", "off")
|
||||
format_filter = request.query.get("format_filter", "")
|
||||
|
||||
# Build URL according to KLIPY API spec
|
||||
base_url = f"https://api.klipy.com/api/v1/{_api_key}/gifs/search"
|
||||
params = {
|
||||
"q": q,
|
||||
"page": page,
|
||||
"per_page": per_page,
|
||||
"content_filter": content_filter,
|
||||
"locale": locale # Always include locale
|
||||
}
|
||||
|
||||
# Add optional parameters if provided
|
||||
if customer_id:
|
||||
params["customer_id"] = customer_id
|
||||
if format_filter:
|
||||
params["format_filter"] = format_filter
|
||||
|
||||
url = f"{base_url}?{urllib.parse.urlencode(params)}"
|
||||
logger.debug(f"Requesting URL: {url}")
|
||||
|
||||
# Create request with headers
|
||||
req = urllib.request.Request(url, headers={
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': 'KLIPY-Proxy/1.0'
|
||||
})
|
||||
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
data = json.loads(resp.read())
|
||||
return {"success": True, "results": data.get("results", [])}
|
||||
except Exception as e:
|
||||
logger.debug(f"KLIPY response: {json.dumps(data)[:200]}...")
|
||||
|
||||
# Transform KLIPY response to match expected format
|
||||
response_data = {
|
||||
"success": data.get("result", False),
|
||||
"results": data.get("data", {}).get("data", []),
|
||||
"pagination": {
|
||||
"current_page": data.get("data", {}).get("current_page", 1),
|
||||
"per_page": data.get("data", {}).get("per_page", 24),
|
||||
"has_next": data.get("data", {}).get("has_next", False)
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(f"Returning {len(response_data['results'])} results")
|
||||
return response_data
|
||||
|
||||
except urllib.error.HTTPError as e:
|
||||
error_msg = f"HTTP Error {e.code}: {e.reason}"
|
||||
logger.error(error_msg)
|
||||
try:
|
||||
error_body = e.read().decode('utf-8')
|
||||
logger.error(f"Error body: {error_body}")
|
||||
except:
|
||||
pass
|
||||
response.status = 502
|
||||
return {"success": False, "error": str(e)}
|
||||
return {"success": False, "error": error_msg}
|
||||
except urllib.error.URLError as e:
|
||||
error_msg = f"Connection error: {str(e)}"
|
||||
logger.error(error_msg)
|
||||
response.status = 502
|
||||
return {"success": False, "error": error_msg}
|
||||
except Exception as e:
|
||||
error_msg = f"Unexpected error: {str(e)}"
|
||||
logger.error(error_msg)
|
||||
response.status = 502
|
||||
return {"success": False, "error": error_msg}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue