findFilms/tmdb-proxy/tmdb_proxy.py

91 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
TMDB API Proxy Service
Прокси-сервис для TMDB API, который работает на хосте без VPN
"""
import os
import httpx
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="TMDB API Proxy", version="1.0.0")
# Настройка CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# API ключ TMDB (прописываем прямо здесь)
TMDB_API_KEY = os.getenv("TMDB_API_KEY", "6d58225585fb77af5945a964de41849f")
TMDB_BASE_URL = "https://api.themoviedb.org/3"
@app.get("/search/movie")
async def search_movies(
query: str = Query(..., description="Поисковый запрос"),
language: str = Query("ru-RU", description="Язык"),
include_adult: bool = Query(False, description="Включать взрослый контент"),
page: int = Query(1, description="Номер страницы")
):
"""Прокси для поиска фильмов через TMDB API"""
async with httpx.AsyncClient(timeout=30.0) as client:
try:
response = await client.get(
f"{TMDB_BASE_URL}/search/movie",
params={
"api_key": TMDB_API_KEY,
"query": query,
"language": language,
"include_adult": include_adult,
"page": page
}
)
response.raise_for_status()
return response.json()
except httpx.HTTPError as e:
raise HTTPException(status_code=500, detail=f"TMDB API error: {str(e)}")
@app.get("/movie/{movie_id}")
async def get_movie_details(
movie_id: int,
language: str = Query("ru-RU", description="Язык"),
append_to_response: str = Query(None, description="Дополнительные данные")
):
"""Прокси для получения детальной информации о фильме из TMDB"""
async with httpx.AsyncClient(timeout=30.0) as client:
try:
params = {
"api_key": TMDB_API_KEY,
"language": language
}
if append_to_response:
params["append_to_response"] = append_to_response
response = await client.get(
f"{TMDB_BASE_URL}/movie/{movie_id}",
params=params
)
response.raise_for_status()
return response.json()
except httpx.HTTPError as e:
raise HTTPException(status_code=500, detail=f"TMDB API error: {str(e)}")
@app.get("/health")
async def health_check():
"""Проверка работоспособности сервиса"""
return {"status": "ok", "service": "tmdb-proxy"}
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "8001"))
print(f"Starting TMDB Proxy on 0.0.0.0:{port}")
uvicorn.run(app, host="0.0.0.0", port=port, log_level="info")