xyz69 commited on
Commit
be1c7e2
β€’
1 Parent(s): 6eaf9a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -38
app.py CHANGED
@@ -1,10 +1,8 @@
1
  from typing import Optional
2
- from fastapi import FastAPI, HTTPException, Request
3
- from fastapi.responses import JSONResponse
4
  import httpx
5
  import random
6
  import string
7
- import json
8
 
9
  app = FastAPI()
10
 
@@ -27,31 +25,8 @@ async def jsongen(url):
27
  except httpx.HTTPError as e:
28
  raise HTTPException(status_code=500, detail=f"Error fetching data: {e}")
29
 
30
- # Middleware to add custom fields globally
31
- @app.middleware("http")
32
- async def add_custom_fields(request: Request, call_next):
33
- response = await call_next(request)
34
-
35
- # Check if the response is JSON and has a body
36
- if response.status_code == 200 and isinstance(response, JSONResponse):
37
- original_body = await response.body()
38
- try:
39
- response_data = json.loads(original_body.decode("utf-8"))
40
- except Exception:
41
- return response # If the body is not JSON, return the original response
42
-
43
- # Add custom fields to the response
44
- modified_data = {
45
- "creator": "my name",
46
- "api_version": "1.0",
47
- **response_data,
48
- }
49
- return JSONResponse(modified_data)
50
-
51
- return response
52
-
53
  # Route to fetch trending videos
54
- @app.get("/trending/{time}/{page}")
55
  async def get_trending(time: str, page: Optional[int] = 0):
56
  trending_url = f"https://hanime.tv/api/v8/browse-trending?time={time}&page={page}&order_by=views&ordering=desc"
57
  urldata = await jsongen(trending_url)
@@ -66,8 +41,13 @@ async def get_trending(time: str, page: Optional[int] = 0):
66
  }
67
  for x in urldata["hentai_videos"]
68
  ]
69
- next_page = f"/trending/{time}/{page + 1}"
70
- return {"results": jsondata, "next_page": next_page}
 
 
 
 
 
71
 
72
  # Route to fetch video details
73
  @app.get("/watch/{slug}")
@@ -104,7 +84,11 @@ async def get_video(slug: str):
104
  "tags": tags,
105
  "episodes": episodes,
106
  }
107
- return {"results": [jsondata]}
 
 
 
 
108
 
109
  # Route to fetch browse data
110
  @app.get("/browse/{type}")
@@ -116,7 +100,11 @@ async def get_browse(type: str):
116
  jsondata = [{"name": x["text"], "url": f"/hentai-tags/{x['text']}/0"} for x in jsondata]
117
  elif type == "brands":
118
  jsondata = [{"name": x["name"], "url": f"/brands/{x['slug']}/0"} for x in jsondata]
119
- return {"results": jsondata}
 
 
 
 
120
 
121
  # Route to fetch tags
122
  @app.get("/tags")
@@ -124,10 +112,14 @@ async def get_tags():
124
  browse_url = "https://hanime.tv/api/v8/browse"
125
  data = await jsongen(browse_url)
126
  jsondata = [{"name": x["text"], "url": f"/tags/{x['text']}/0"} for x in data["hentai_tags"]]
127
- return {"results": jsondata}
 
 
 
 
128
 
129
  # Route to fetch browse videos
130
- @app.get("/{type}/{category}/{page}")
131
  async def get_browse_videos(type: str, category: str, page: Optional[int] = 0):
132
  browse_url = f"https://hanime.tv/api/v8/browse/{type}/{category}?page={page}&order_by=views&ordering=desc"
133
  browsedata = await jsongen(browse_url)
@@ -142,19 +134,33 @@ async def get_browse_videos(type: str, category: str, page: Optional[int] = 0):
142
  }
143
  for x in browsedata["hentai_videos"]
144
  ]
145
- next_page = f"/{type}/{category}/{page + 1}"
146
- return {"results": jsondata, "next_page": next_page}
 
 
 
 
 
147
 
148
  # Root route
149
  @app.get("/")
150
  async def root():
151
- return {"message": "Welcome to Hanime API πŸ‘€"}
 
 
 
 
152
 
153
  # Custom error handler
154
  @app.exception_handler(Exception)
155
- async def custom_exception_handler(request: Request, exc: Exception):
156
  return JSONResponse(
157
  status_code=500,
158
- content={"error": "Something went wrong", "details": str(exc)},
 
 
 
 
 
159
  )
160
 
 
1
  from typing import Optional
2
+ from fastapi import FastAPI, HTTPException
 
3
  import httpx
4
  import random
5
  import string
 
6
 
7
  app = FastAPI()
8
 
 
25
  except httpx.HTTPError as e:
26
  raise HTTPException(status_code=500, detail=f"Error fetching data: {e}")
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Route to fetch trending videos
29
+ @app.get("/trending/{time}")
30
  async def get_trending(time: str, page: Optional[int] = 0):
31
  trending_url = f"https://hanime.tv/api/v8/browse-trending?time={time}&page={page}&order_by=views&ordering=desc"
32
  urldata = await jsongen(trending_url)
 
41
  }
42
  for x in urldata["hentai_videos"]
43
  ]
44
+ next_page = f"/trending/{time}?{page + 1}"
45
+ return {
46
+ "creator": "EYEPATCH",
47
+ "api_version": "1.0",
48
+ "results": jsondata,
49
+ "next_page": next_page,
50
+ }
51
 
52
  # Route to fetch video details
53
  @app.get("/watch/{slug}")
 
84
  "tags": tags,
85
  "episodes": episodes,
86
  }
87
+ return {
88
+ "creator": "EYEPATCH",
89
+ "api_version": "1.0",
90
+ "results": [jsondata],
91
+ }
92
 
93
  # Route to fetch browse data
94
  @app.get("/browse/{type}")
 
100
  jsondata = [{"name": x["text"], "url": f"/hentai-tags/{x['text']}/0"} for x in jsondata]
101
  elif type == "brands":
102
  jsondata = [{"name": x["name"], "url": f"/brands/{x['slug']}/0"} for x in jsondata]
103
+ return {
104
+ "creator": "EYEPATCH",
105
+ "api_version": "1.0",
106
+ "results": jsondata,
107
+ }
108
 
109
  # Route to fetch tags
110
  @app.get("/tags")
 
112
  browse_url = "https://hanime.tv/api/v8/browse"
113
  data = await jsongen(browse_url)
114
  jsondata = [{"name": x["text"], "url": f"/tags/{x['text']}/0"} for x in data["hentai_tags"]]
115
+ return {
116
+ "creator": "EYEPATCH",
117
+ "api_version": "1.0",
118
+ "results": jsondata,
119
+ }
120
 
121
  # Route to fetch browse videos
122
+ @app.get("/{type}/{category}")
123
  async def get_browse_videos(type: str, category: str, page: Optional[int] = 0):
124
  browse_url = f"https://hanime.tv/api/v8/browse/{type}/{category}?page={page}&order_by=views&ordering=desc"
125
  browsedata = await jsongen(browse_url)
 
134
  }
135
  for x in browsedata["hentai_videos"]
136
  ]
137
+ next_page = f"/{type}/{category}?{page + 1}"
138
+ return {
139
+ "creator": "EYEPATCH",
140
+ "api_version": "1.0",
141
+ "results": jsondata,
142
+ "next_page": next_page,
143
+ }
144
 
145
  # Root route
146
  @app.get("/")
147
  async def root():
148
+ return {
149
+ "creator": "EYEPATCH",
150
+ "api_version": "1.0",
151
+ "message": "Welcome to Hanime API πŸ‘€",
152
+ }
153
 
154
  # Custom error handler
155
  @app.exception_handler(Exception)
156
+ async def custom_exception_handler(request, exc):
157
  return JSONResponse(
158
  status_code=500,
159
+ content={
160
+ "creator": "EYEPATCH",
161
+ "api_version": "1.0",
162
+ "error": "Something went wrong",
163
+ "details": str(exc),
164
+ },
165
  )
166