Spaces:
Sleeping
Sleeping
File size: 5,269 Bytes
ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 be1c7e2 ec9a941 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
from typing import Optional
from fastapi import FastAPI, HTTPException
import httpx
import random
import string
app = FastAPI()
# Utility function to generate random hex strings
def random_hex(length=32):
return ''.join(random.choices(string.hexdigits, k=length))
# Fetch JSON from the given URL with required headers
async def jsongen(url):
headers = {
"X-Signature-Version": "web2",
"X-Signature": random_hex(),
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
}
try:
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
response.raise_for_status()
return response.json()
except httpx.HTTPError as e:
raise HTTPException(status_code=500, detail=f"Error fetching data: {e}")
# Route to fetch trending videos
@app.get("/trending/{time}")
async def get_trending(time: str, page: Optional[int] = 0):
trending_url = f"https://hanime.tv/api/v8/browse-trending?time={time}&page={page}&order_by=views&ordering=desc"
urldata = await jsongen(trending_url)
jsondata = [
{
"id": x["id"],
"name": x["name"],
"slug": x["slug"],
"cover_url": x["cover_url"],
"views": x["views"],
"link": f"/watch/{x['slug']}",
}
for x in urldata["hentai_videos"]
]
next_page = f"/trending/{time}?{page + 1}"
return {
"creator": "EYEPATCH",
"api_version": "1.0",
"results": jsondata,
"next_page": next_page,
}
# Route to fetch video details
@app.get("/watch/{slug}")
async def get_video(slug: str):
video_api_url = f"https://hanime.tv/api/v8/video?id={slug}"
video_data = await jsongen(video_api_url)
tags = [
{"name": t["text"], "link": f"/hentai-tags/{t['text']}/0"}
for t in video_data["hentai_tags"]
]
streams = [
{"width": s["width"], "height": s["height"], "size_mbs": s["filesize_mbs"], "url": s["url"]}
for s in video_data["videos_manifest"]["servers"][0]["streams"]
]
episodes = [
{
"id": e["id"],
"name": e["name"],
"slug": e["slug"],
"cover_url": e["cover_url"],
"views": e["views"],
"link": f"/watch/{e['slug']}",
}
for e in video_data["hentai_franchise_hentai_videos"]
]
jsondata = {
"id": video_data["hentai_video"]["id"],
"name": video_data["hentai_video"]["name"],
"description": video_data["hentai_video"]["description"],
"poster_url": video_data["hentai_video"]["poster_url"],
"cover_url": video_data["hentai_video"]["cover_url"],
"views": video_data["hentai_video"]["views"],
"streams": streams,
"tags": tags,
"episodes": episodes,
}
return {
"creator": "EYEPATCH",
"api_version": "1.0",
"results": [jsondata],
}
# Route to fetch browse data
@app.get("/browse/{type}")
async def get_browse(type: str):
browse_url = "https://hanime.tv/api/v8/browse"
data = await jsongen(browse_url)
jsondata = data[type]
if type == "hentai_tags":
jsondata = [{"name": x["text"], "url": f"/hentai-tags/{x['text']}/0"} for x in jsondata]
elif type == "brands":
jsondata = [{"name": x["name"], "url": f"/brands/{x['slug']}/0"} for x in jsondata]
return {
"creator": "EYEPATCH",
"api_version": "1.0",
"results": jsondata,
}
# Route to fetch tags
@app.get("/tags")
async def get_tags():
browse_url = "https://hanime.tv/api/v8/browse"
data = await jsongen(browse_url)
jsondata = [{"name": x["text"], "url": f"/tags/{x['text']}/0"} for x in data["hentai_tags"]]
return {
"creator": "EYEPATCH",
"api_version": "1.0",
"results": jsondata,
}
# Route to fetch browse videos
@app.get("/{type}/{category}")
async def get_browse_videos(type: str, category: str, page: Optional[int] = 0):
browse_url = f"https://hanime.tv/api/v8/browse/{type}/{category}?page={page}&order_by=views&ordering=desc"
browsedata = await jsongen(browse_url)
jsondata = [
{
"id": x["id"],
"name": x["name"],
"slug": x["slug"],
"cover_url": x["cover_url"],
"views": x["views"],
"link": f"/watch/{x['slug']}",
}
for x in browsedata["hentai_videos"]
]
next_page = f"/{type}/{category}?{page + 1}"
return {
"creator": "EYEPATCH",
"api_version": "1.0",
"results": jsondata,
"next_page": next_page,
}
# Root route
@app.get("/")
async def root():
return {
"creator": "EYEPATCH",
"api_version": "1.0",
"message": "Welcome to Hanime API π",
}
# Custom error handler
@app.exception_handler(Exception)
async def custom_exception_handler(request, exc):
return JSONResponse(
status_code=500,
content={
"creator": "EYEPATCH",
"api_version": "1.0",
"error": "Something went wrong",
"details": str(exc),
},
)
|