Create catergory.py
Browse files- src/catergory.py +64 -0
src/catergory.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter
|
2 |
+
from fastapi import status
|
3 |
+
from typing import Optional
|
4 |
+
from helper.is_site_available import check_if_site_available
|
5 |
+
from helper.error_messages import error_handler
|
6 |
+
|
7 |
+
router = APIRouter(tags=["Category Torrents Route"])
|
8 |
+
|
9 |
+
@router.get("/get_category")
|
10 |
+
async def get_category(
|
11 |
+
site: str,
|
12 |
+
query: str,
|
13 |
+
category: str,
|
14 |
+
limit: Optional[int] = 0,
|
15 |
+
page: Optional[int] = 1,
|
16 |
+
):
|
17 |
+
all_sites = check_if_site_available(site)
|
18 |
+
site = site.lower()
|
19 |
+
query = query.lower()
|
20 |
+
category = category.lower()
|
21 |
+
if all_sites:
|
22 |
+
limit = (
|
23 |
+
all_sites[site]["limit"]
|
24 |
+
if limit == 0 or limit > all_sites[site]["limit"]
|
25 |
+
else limit
|
26 |
+
)
|
27 |
+
|
28 |
+
if all_sites[site]["search_by_category"]:
|
29 |
+
if category not in all_sites[site]["categories"]:
|
30 |
+
return error_handler(
|
31 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
32 |
+
json_message={
|
33 |
+
"error": "Selected category not available.",
|
34 |
+
"available_categories": all_sites[site]["categories"],
|
35 |
+
},
|
36 |
+
)
|
37 |
+
resp = await all_sites[site]["website"]().search_by_category(
|
38 |
+
query, category, page, limit
|
39 |
+
)
|
40 |
+
if resp is None:
|
41 |
+
return error_handler(
|
42 |
+
status_code=status.HTTP_403_FORBIDDEN,
|
43 |
+
json_message={
|
44 |
+
"error": "Website Blocked Change IP or Website Domain."
|
45 |
+
},
|
46 |
+
)
|
47 |
+
elif len(resp["data"]) > 0:
|
48 |
+
return resp
|
49 |
+
else:
|
50 |
+
return error_handler(
|
51 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
52 |
+
json_message={"error": "Result not found."},
|
53 |
+
)
|
54 |
+
else:
|
55 |
+
return error_handler(
|
56 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
57 |
+
json_message={
|
58 |
+
"error": "Category search not availabe for {}.".format(site)
|
59 |
+
},
|
60 |
+
)
|
61 |
+
return error_handler(
|
62 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
63 |
+
json_message={"error": "Selected Site Not Available"},
|
64 |
+
)
|