Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
anime-kun32 authored Jan 1, 2025
0 parents commit c973795
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# API Base URL
# Please replace with aniwatch your API URL .
# For the AniWatch API, visit: https://github.com/ghoshRitesh12/aniwatch-api
# example url : https://api-aniwatch.onrender.com

API_BASE_URL=https://aniwatch-api-net.vercel.app

# API Origin Header
# Set the origin header to the domain where your frontend is hosted to avoid CORS issues.
# For example, if you're running the frontend on 'https://your-frontend.com', set:
# API_ORIGIN_HEADER=https://your-frontend.com
API_ORIGIN_HEADER=https://goog-three.vercel.app

# Allowed Origins
# List of domains that are allowed to access your API.
# Add any domains where your frontend app is hosted. For example:
# ALLOWED_ORIGINS=["https://your-frontend.com", "https://example2.com"]
ALLOWED_ORIGINS=["*"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# goog
An api that feches hianime spotlight from aniwathc API and then returns with anilist id
79 changes: 79 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates
import httpx
import os
from dotenv import load_dotenv
import json

# Load environment variables
load_dotenv()

app = FastAPI()

# Fetch allowed origins from the .env file
ALLOWED_ORIGINS = json.loads(os.getenv("ALLOWED_ORIGINS", "[]"))

# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_ORIGINS, # Dynamically load origins from .env
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

# Jinja2 Templates
templates = Jinja2Templates(directory="templates")

# Fetch configurations from environment variables
API_BASE_URL = os.getenv("API_BASE_URL")
API_ORIGIN_HEADER = os.getenv("API_ORIGIN_HEADER")

@app.get("/api/spotlight")
async def get_spotlight(request: Request):
try:
async with httpx.AsyncClient() as client:
# Fetch Spotlight Data
spotlight_response = await client.get(
f"{API_BASE_URL}/api/v2/hianime/home",
headers={"Origin": API_ORIGIN_HEADER}
)
spotlight_data = spotlight_response.json()

if not spotlight_data.get("success"):
return {"error": "Failed to fetch spotlight data"}

updated_spotlight = []

for anime in spotlight_data["data"]["spotlightAnimes"]:
anime_id = anime.get("id")
if anime_id:
details_url = f"{API_BASE_URL}/api/v2/hianime/anime/{anime_id}"
details_response = await client.get(
details_url,
headers={"Origin": API_ORIGIN_HEADER}
)
details_data = details_response.json()

if details_data.get("success"):
anilist_id = details_data["data"]["anime"]["info"].get("anilistId")
if anilist_id:
anime["anilistId"] = anilist_id

updated_spotlight.append(anime)

return {"success": True, "data": {"spotlightAnimes": updated_spotlight}}
except Exception as e:
return {"error": "Internal Server Error", "details": str(e)}

# Custom 404 Error Handler
@app.exception_handler(404)
async def not_found_handler(request: Request, exc):
return templates.TemplateResponse("404.html", {"request": request}, status_code=404)

# Custom 500 Error Handler
@app.exception_handler(500)
async def internal_server_error_handler(request: Request, exc):
return templates.TemplateResponse("500.html", {"request": request}, status_code=500)
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fastapi
httpx
uvicorn
python-dotenv
jinja2==3.1.5
10 changes: 10 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"builds": [
{ "src": "main.py", "use": "@vercel/python" }
],
"routes": [
{ "src": "/api/.*", "dest": "main.py" },
{ "handle": "filesystem" },
{ "src": "/.*", "dest": "/404.html" }
]
}

0 comments on commit c973795

Please sign in to comment.