forked from Ryuk-me/Torrent-Api-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
335 lines (306 loc) · 10.4 KB
/
main.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import asyncio
import os
import time
from typing import Optional
# import aioredis
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
# from fastapi_cache import FastAPICache
# from fastapi_cache.backends.redis import RedisBackend
# from fastapi_cache.decorator import cache
from helper.is_site_available import check_if_site_available
load_dotenv()
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# CACHE_EXPIRATION = (
# int(os.getenv("CACHE_EXPIRATION", 180))
# if os.getenv("PYTHON_ENV", "dev") == "prod"
# else 30
# )
@app.get("/api/v1/search")
# @cache(expire=CACHE_EXPIRATION)
async def call_api(
site: str, query: str, limit: Optional[int] = 0, page: Optional[int] = 1
):
site = site.lower()
query = query.lower()
all_sites = check_if_site_available(site)
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
if all_sites:
resp = await all_sites[site]["website"]().search(query, page, limit)
if resp == None:
return {"error": "website blocked change ip or domain"}
elif len(resp["data"]) > 0:
return resp
else:
return {"error": "no results found"}
return {"error": "invalid site"}
@app.get("/api/v1/search_url")
async def get_torrent_from_url(
site: str, url: str):
site = site.lower()
all_sites = check_if_site_available(site)
if all_sites:
resp = await all_sites[site]["website"]().get_torrent_by_url(url)
if resp == None:
return {"error": "website blocked change ip or domain"}
elif len(resp["data"]) > 0:
return resp
else:
return {"error": "no results found"}
return {"error": "invalid site"}
@app.get("/api/v1/trending")
# @cache(expire=CACHE_EXPIRATION)
async def get_trending(
site: str,
limit: Optional[int] = 0,
category: Optional[str] = None,
page: Optional[int] = 1,
):
site = site.lower()
all_sites = check_if_site_available(site)
category = category.lower() if category else None
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
if all_sites:
if all_sites[site]["trending_available"]:
if category != None and not all_sites[site]["trending_category"]:
return {
"error": "search by trending category not available for {}".format(
site
)
}
if category != None and category not in all_sites[site]["categories"]:
return {
"error": "selected category not available",
"available_categories": all_sites[site]["categories"],
}
resp = await all_sites[site]["website"]().trending(category, page, limit)
if not resp:
return {"error": "website blocked change ip or domain"}
elif len(resp["data"]) > 0:
return resp
else:
return {"error": "no results found"}
else:
return {"error": "trending search not availabe for {}".format(site)}
return {"error": "invalid site"}
@app.get("/api/v1/category")
# @cache(expire=CACHE_EXPIRATION)
async def get_category(
site: str,
query: str,
category: str,
limit: Optional[int] = 0,
page: Optional[int] = 1,
):
all_sites = check_if_site_available(site)
site = site.lower()
query = query.lower()
category = category.lower()
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
if all_sites:
if all_sites[site]["search_by_category"]:
if category not in all_sites[site]["categories"]:
return {
"error": "selected category not available",
"available_categories": all_sites[site]["categories"],
}
resp = await all_sites[site]["website"]().search_by_category(
query, category, page, limit
)
if resp == None:
return {"error": "website blocked change ip or domain"}
elif len(resp["data"]) > 0:
return resp
else:
return {"error": "no results found"}
else:
return {"error": "search by category not available for {}".format(site)}
return {"error": "invalid site"}
@app.get("/api/v1/recent")
# @cache(expire=CACHE_EXPIRATION)
async def get_recent(
site: str,
limit: Optional[int] = 0,
category: Optional[str] = None,
page: Optional[int] = 1,
):
all_sites = check_if_site_available(site)
site = site.lower()
category = category.lower() if category else None
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
if all_sites:
if all_sites[site]["recent_available"]:
if category != None and not all_sites[site]["recent_category_available"]:
return {
"error": "search by recent category not available for {}".format(
site
)
}
if category != None and category not in all_sites[site]["categories"]:
return {
"error": "selected category not available",
"available_categories": all_sites[site]["categories"],
}
resp = await all_sites[site]["website"]().recent(category, page, limit)
if not resp:
return {"error": "website blocked change ip or domain"}
elif len(resp["data"]) > 0:
return resp
else:
return {"error": "no results found"}
else:
return {"error": "recent search not available for {}".format(site)}
else:
return {"error": "invalid site"}
@app.get("/")
async def home():
return FileResponse("README.md")
@app.get("/api/v1/all/search")
# @cache(expire=CACHE_EXPIRATION)
async def get_search_combo(query: str, limit: Optional[int] = 0):
start_time = time.time()
query = query.lower()
# just getting all_sites dictionary
all_sites = check_if_site_available("1337x")
sites_list = list(all_sites.keys())
tasks = []
COMBO = {"data": []}
total_torrents_overall = 0
for site in sites_list:
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
tasks.append(
asyncio.create_task(
all_sites[site]["website"]().search(query, page=1, limit=limit)
)
)
results = await asyncio.gather(*tasks)
for res in results:
if res and len(res["data"]) > 0:
for torrent in res["data"]:
COMBO["data"].append(torrent)
total_torrents_overall = total_torrents_overall + res["total"]
COMBO["time"] = time.time() - start_time
COMBO["total"] = total_torrents_overall
return COMBO
@app.get("/api/v1/all/trending")
# @cache(expire=CACHE_EXPIRATION)
async def get_all_trending(limit: Optional[int] = 0):
start_time = time.time()
# just getting all_sites dictionary
all_sites = check_if_site_available("1337x")
sites_list = [
site
for site in all_sites.keys()
if all_sites[site]["trending_available"] and all_sites[site]["website"]
]
tasks = []
COMBO = {"data": []}
total_torrents_overall = 0
for site in sites_list:
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
tasks.append(
asyncio.create_task(
all_sites[site]["website"]().trending(
category=None, page=1, limit=limit
)
)
)
results = await asyncio.gather(*tasks)
for res in results:
if res and len(res["data"]) > 0:
for torrent in res["data"]:
COMBO["data"].append(torrent)
total_torrents_overall = total_torrents_overall + res["total"]
COMBO["time"] = time.time() - start_time
COMBO["total"] = total_torrents_overall
return COMBO
@app.get("/api/v1/all/recent")
# @cache(expire=CACHE_EXPIRATION)
async def get_all_recent(limit: Optional[int] = 0):
start_time = time.time()
# just getting all_sites dictionary
all_sites = check_if_site_available("1337x")
sites_list = [
site
for site in all_sites.keys()
if all_sites[site]["recent_available"] and all_sites[site]["website"]
]
tasks = []
COMBO = {"data": []}
total_torrents_overall = 0
for site in sites_list:
limit = (
all_sites[site]["limit"]
if limit == 0 or limit > all_sites[site]["limit"]
else limit
)
tasks.append(
asyncio.create_task(
all_sites[site]["website"]().recent(
category=None, page=1, limit=limit)
)
)
results = await asyncio.gather(*tasks)
for res in results:
if res and len(res["data"]) > 0:
for torrent in res["data"]:
COMBO["data"].append(torrent)
total_torrents_overall = total_torrents_overall + res["total"]
COMBO["time"] = time.time() - start_time
COMBO["total"] = total_torrents_overall
return COMBO
@app.get("/api/v1/sites")
async def get_all_supported_sites():
all_sites = check_if_site_available("1337x")
sites_list = [
site
for site in all_sites.keys()
if all_sites[site]["website"]
]
return {"supported_sites": sites_list}
# @app.on_event("startup")
# async def startup():
# PYTHON_ENV = os.getenv("PYTHON_ENV", "dev")
# if PYTHON_ENV == "prod":
# HOST = os.getenv("REDIS_URI", "redis://localhost")
# else:
# HOST = "redis://localhost"
# redis = aioredis.from_url(HOST, encoding="utf8", decode_responses=True)
# FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)