Repository navigation
Expand file tree
/
Copy pathApi.py
More file actions
executable file
·92 lines (70 loc) · 2.42 KB
/
Copy pathApi.py
File metadata and controls
executable file
·92 lines (70 loc) · 2.42 KB
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
from socket import timeout
from typing import Optional
from fastapi import FastAPI, Query
from Torrent import Search
from Manga import Mangasearch, Get_manga
from Comic import Comicsearch, Get_comic
from fastapi.middleware.cors import CORSMiddleware
# caching #######################################################################################################
import functools
import time
def timedlru_cache(maxsize=128, timeout=None):
cache = {}
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
key = (args, frozenset(kwargs.items())) # Create a hashable key
if key in cache:
timestamp, value = cache[key]
if timeout is None or time.time() - timestamp <= timeout:
return value # Return cached value if not expired
value = func(*args, **kwargs)
cache[key] = (time.time(), value) # Cache the value with current timestamp
return value
return wrapper
return decorator
##################################################################################################################
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust this according to your needs. "*" allows all origins.
allow_credentials=True,
allow_methods=[
"GET",
"POST",
"PUT",
"DELETE",
], # Adjust the allowed methods as needed.
allow_headers=["*"], # Adjust the allowed headers as needed.
)
@app.get("/")
@timedlru_cache(maxsize=10, timeout=600)
def index():
return "api is up!!"
@app.get("/manga/search")
#@timedlru_cache(maxsize=10, timeout=600)
def manga_search(term: str):
res = Mangasearch(term)
return res
@app.get("/manga/{manga_name}")
#@timedlru_cache(maxsize=10, timeout=600)
def manga(
manga_name: str, source: str = Query("mangapanda", enum=["mangapanda", "rmanga"])
):
res = Get_manga(manga_name, source)
return res
@app.get("/comic/search")
#@timedlru_cache(maxsize=10, timeout=10)
def comic_search(term: str):
res = Comicsearch(term)
return res
@app.get("/comic/{comic_name}")
#@timedlru_cache(maxsize=10, timeout=600)
def comic(comic_name: str):
res = Get_comic(comic_name)
return res
@app.get("/torr/search")
#@timedlru_cache(maxsize=10, timeout=600)
def torr_search(term: str, catagory: Optional[str] = None):
res = Search(term, catagory)
return res