This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (51 loc) · 1.66 KB
/
app.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
from sanic import Sanic
from sanic.response import json
from imageboard.danbooru import Danbooru
app = Sanic("ihaboard-scrapper")
app.config.FORWARDED_HOST = "temporary_string"
def to_real_bool(string):
bool_map = {
"0": False,
"1": True,
"true": True,
"false": False,
"y": True,
"n": False,
"yes": True,
"no": False,
0: False,
1: True,
True: True,
False: False,
}
if isinstance(string, str):
string = string.lower()
return bool_map.get(string, False)
@app.get("/danbooru")
async def danbooru_requests(request):
params = request.args
tags = params.get("search", "")
do_random_search = to_real_bool(params.get("random", "0"))
tags = [t for t in tags.split("+")]
dbi = Danbooru(False)
if do_random_search:
results = await dbi.random_search(tags)
else:
results = await dbi.search(tags)
await dbi.shutoff()
return json(results, ensure_ascii=False, encode_html_chars=True, escape_forward_slashes=False, indent=4)
@app.get("/safebooru")
async def safebooru_request(request):
params = request.args
tags = params.get("search", "")
do_random_search = to_real_bool(params.get("random", "0"))
tags = [t for t in tags.split("+")]
dbi = Danbooru(True)
if do_random_search:
results = await dbi.random_search(tags)
else:
results = await dbi.search(tags)
await dbi.shutoff()
return json(results, ensure_ascii=False, encode_html_chars=True, escape_forward_slashes=False, indent=4)
if __name__ == "__main__":
app.run("127.0.0.1", 6969, debug=True, access_log=True, auto_reload=True)