randydev commited on
Commit
44c8282
·
verified ·
1 Parent(s): e91d062

Create combo.py

Browse files
Files changed (1) hide show
  1. src/combo.py +125 -0
src/combo.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, status
2
+ from typing import Optional
3
+ from helper.is_site_available import check_if_site_available
4
+ import time
5
+ import asyncio
6
+ from helper.error_messages import error_handler
7
+
8
+ router = APIRouter(tags=["Combo Routes"])
9
+
10
+ @router.get("/combo-search")
11
+ async def get_search_combo(query: str, limit: Optional[int] = 0):
12
+ start_time = time.time()
13
+ query = query.lower()
14
+ all_sites = check_if_site_available("1337x")
15
+ sites_list = list(all_sites.keys())
16
+ tasks = []
17
+ COMBO = {"data": []}
18
+ total_torrents_overall = 0
19
+ for site in sites_list:
20
+ limit = (
21
+ all_sites[site]["limit"]
22
+ if limit == 0 or limit > all_sites[site]["limit"]
23
+ else limit
24
+ )
25
+ tasks.append(
26
+ asyncio.create_task(
27
+ all_sites[site]["website"]().search(query, page=1, limit=limit)
28
+ )
29
+ )
30
+ results = await asyncio.gather(*tasks)
31
+ for res in results:
32
+ if res is not None and len(res["data"]) > 0:
33
+ for torrent in res["data"]:
34
+ COMBO["data"].append(torrent)
35
+ total_torrents_overall = total_torrents_overall + res["total"]
36
+ COMBO["time"] = time.time() - start_time
37
+ COMBO["total"] = total_torrents_overall
38
+ if total_torrents_overall == 0:
39
+ return error_handler(
40
+ status_code=status.HTTP_404_NOT_FOUND,
41
+ json_message={"error": "Result not found."},
42
+ )
43
+ return COMBO
44
+
45
+
46
+ @router.get("/combo-trending")
47
+ async def get_all_trending(limit: Optional[int] = 0):
48
+ start_time = time.time()
49
+ # * just getting all_sites dictionary
50
+ all_sites = check_if_site_available("1337x")
51
+ sites_list = [
52
+ site
53
+ for site in all_sites.keys()
54
+ if all_sites[site]["trending_available"] and all_sites[site]["website"]
55
+ ]
56
+ tasks = []
57
+ COMBO = {"data": []}
58
+ total_torrents_overall = 0
59
+ for site in sites_list:
60
+ limit = (
61
+ all_sites[site]["limit"]
62
+ if limit == 0 or limit > all_sites[site]["limit"]
63
+ else limit
64
+ )
65
+ tasks.append(
66
+ asyncio.create_task(
67
+ all_sites[site]["website"]().trending(
68
+ category=None, page=1, limit=limit
69
+ )
70
+ )
71
+ )
72
+ results = await asyncio.gather(*tasks)
73
+ for res in results:
74
+ if res is not None and len(res["data"]) > 0:
75
+ for torrent in res["data"]:
76
+ COMBO["data"].append(torrent)
77
+ total_torrents_overall = total_torrents_overall + res["total"]
78
+ COMBO["time"] = time.time() - start_time
79
+ COMBO["total"] = total_torrents_overall
80
+ if total_torrents_overall == 0:
81
+ return error_handler(
82
+ status_code=status.HTTP_404_NOT_FOUND,
83
+ json_message={"error": "Result not found."},
84
+ )
85
+ return COMBO
86
+
87
+
88
+ @router.get("/combo-recent")
89
+ async def get_all_recent(limit: Optional[int] = 0):
90
+ start_time = time.time()
91
+ # just getting all_sites dictionary
92
+ all_sites = check_if_site_available("1337x")
93
+ sites_list = [
94
+ site
95
+ for site in all_sites.keys()
96
+ if all_sites[site]["recent_available"] and all_sites[site]["website"]
97
+ ]
98
+ tasks = []
99
+ COMBO = {"data": []}
100
+ total_torrents_overall = 0
101
+ for site in sites_list:
102
+ limit = (
103
+ all_sites[site]["limit"]
104
+ if limit == 0 or limit > all_sites[site]["limit"]
105
+ else limit
106
+ )
107
+ tasks.append(
108
+ asyncio.create_task(
109
+ all_sites[site]["website"]().recent(category=None, page=1, limit=limit)
110
+ )
111
+ )
112
+ results = await asyncio.gather(*tasks)
113
+ for res in results:
114
+ if res is not None and len(res["data"]) > 0:
115
+ for torrent in res["data"]:
116
+ COMBO["data"].append(torrent)
117
+ total_torrents_overall = total_torrents_overall + res["total"]
118
+ COMBO["time"] = time.time() - start_time
119
+ COMBO["total"] = total_torrents_overall
120
+ if total_torrents_overall == 0:
121
+ return error_handler(
122
+ status_code=status.HTTP_404_NOT_FOUND,
123
+ json_message={"error": "Result not found."},
124
+ )
125
+ return COMBO