-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c973795
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=["*"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fastapi | ||
httpx | ||
uvicorn | ||
python-dotenv | ||
jinja2==3.1.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] | ||
} |