-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
51 lines (35 loc) · 1.46 KB
/
api.py
File metadata and controls
51 lines (35 loc) · 1.46 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
import websockets
import asyncio
import json
from websockets.asyncio.server import serve
import concurrent.futures
from test_main import MainPipelineHelper
import db
database = db.DB("dd.db")
main_pipeline = MainPipelineHelper()
async def handler(websocket):
try:
async for message in websocket:
data = json.loads(message)
print("DATA", data)
if data["type"] == "request_scan":
url = data["url"]
loop = asyncio.get_running_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
score = await loop.run_in_executor(pool, main_pipeline.run, url)
response_data = {"type": "scan_results", "url": url , "score": score}
elif data["type"] == "request_cache":
url = data["url"]
res = database.fetch_website(url)
if res == None:
response_data = {"type": "cache_not_found", "url": url}
else:
score, ts = res
response_data = {"type": "cache_found", "url": url, "score": score, "ts": ts.isoformat()}
await websocket.send(json.dumps(response_data))
except websockets.ConnectionClosed as e:
print(f"Connection closed: {e}")
async def main():
async with serve(handler, "0.0.0.0", 6969):
await asyncio.get_running_loop().create_future() # run forever
asyncio.run(main())