This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (104 loc) · 3.27 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
import sanic
from sanic import json
from sanic_ext import openapi, Extend
from pymongo import MongoClient
import requests
from dotenv import load_dotenv
from os import environ as env
load_dotenv()
db = MongoClient()["MeowerBots"]
app = sanic.Sanic("MeowerBots")
Extend(app)
resp = requests.post("https://meltauth.meltland.dev/login", json={
"username": env["MEOWER_USERNAME"],
"pswd": env["MEOWER_PASSWORD"]
})
if resp.status_code != 200:
raise Exception("Failed to login to Meower")
token = resp.json()["payload"]["payload"]["token"]
def get_max_pages():
return (db["bots"].count_documents({}) // 5) + 1
@app.get("/")
async def get_bots(request: sanic.request.Request):
try:
page = int(request.args.get("page", 1))
except ValueError:
page = 1
return json({
"bots": list(db["bots"].find({}, skip=(page-1)*5, limit=5)),
"page": page,
"max_pages": get_max_pages()
})
# noinspection PyShadowingNames
def check_key(name, key):
resp = requests.get(f"https://api.meower.org/users/{name}/dm", headers={
"token": token
})
if resp.status_code != 200:
print(resp.text, resp.status_code)
return False
resp = resp.json()
post = requests.get(f"https://api.meower.org/posts/{resp['_id']}?autoget=1&page=1", headers={
"token": token
}).json()["autoget"][0]
return post["p"] == key
# noinspection PyTestUnpassedFixture
@app.put("/bot/<name:str>")
@openapi.body({
"body": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"library": {
"type": "string"
}
},
"required": ["key"]
}
})
async def add_bot(request: sanic.request.Request, name: str):
if db["bots"].find_one({"name": name}):
return json({"error": "Bot already added"}, status=400)
data = request.json
if not check_key(name, data["key"]):
return json({"error": "Verification failed"}, status=400)
db["bots"].insert_one({
"_id": name,
"owner": None,
"verified": False,
"library": data.get("library", "Unknown"),
})
return json({"message": "Bot added"})
@app.delete("/bot/<name:str>")
async def remove_bot(request: sanic.request.Request, name: str):
if not check_key(name, request.headers["key"]):
return json({"error": "Verification failed"}, status=400)
db["bots"].delete_one({
"_id": name,
})
return json({"message": "Bot removed"})
@app.get("/bot/<name:str>")
async def get_bot(request: sanic.request.Request, name: str):
bot = db["bots"].find_one({"_id": name})
if not bot:
return json({"error": "Bot not found"}, status=404)
return json({"bot": bot})
# noinspection PyTestUnpassedFixture
@app.patch("/bot/<name:str>")
async def update_bot(request: sanic.request.Request, name: str):
if not check_key(name, request.headers["key"]):
return json({"error": "Verification failed"}, status=400)
return 501
data = request.json
db["bots"].update_one({
"_id": name,
}, {
"$set": data,
"_id": name,
"verified": False,
})
return json({"message": "Bot updated"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8100)