-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
83 lines (59 loc) · 2.35 KB
/
server.py
File metadata and controls
83 lines (59 loc) · 2.35 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
import asyncio
import json
import os
import time
from pathlib import Path
import websockets
from app import loader, services
from app.core import logger, settings
from app.utils import types
async def send(websocket, type, message):
await websocket.send(json.dumps({"type": type, "message": json.dumps(message)}))
async def handler(websocket):
async for message in websocket:
# todo: implement handlers
recv = json.loads(message)
logger.info(recv)
if "command" not in recv:
raise Exception("command not in payload")
# todo: add enums for each command
if recv["command"] == "init":
await send(websocket, "subreddits", settings.subreddits)
await send(websocket, "categories", settings.categories)
await send(websocket, "global_post_limit", settings.global_post_limit)
elif recv["command"] == "get_content":
images = []
format = None
if "format" in recv["params"]:
format = recv["params"]["format"]
for path in Path("assets").rglob("**/*"):
if len(path.name.split(".")) == 1:
continue
image_id, image_format = path.name.split(".")
if format and format != image_format:
continue
images.append(
{
"id": image_id,
"url": str(path.absolute()),
"format": image_format,
"modified_at": time.ctime(os.path.getmtime(path)),
}
)
if "limit" in recv["params"]:
images = images[: recv["params"]["limit"]]
await send(websocket, "submissions", images)
elif recv["command"] == "run":
section = types.NEW
if "section" in recv.get("params", {}):
section = recv["params"]["section"]
limit = settings.global_post_limit
if "limit" in recv.get("params", {}):
limit = recv["params"]["limit"]
await services.sync.sync_subs(section, limit)
async def main():
await loader.load_settings()
async with websockets.serve(handler, "", 8001):
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())