-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (75 loc) · 2.6 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
from flask import Flask, request, jsonify, escape
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import json
from flask_cors import CORS
import requests
app = Flask(__name__)
CORS(app)
limiter = Limiter(app, key_func=get_remote_address)
# Load rooms data from "rooms.json" file
with open("rooms.json") as f:
rooms = json.load(f)
# Set the rate limit for API endpoints
rate_limit = "100/hour" # Adjust as per your requirements
@app.route("/ggg")
def pring():
print("PING PONG")
return "Kwell"
@app.route("/find/<string:room>")
def find_room_url(room):
name_to_url = {item["name"]: item["api_url"] for item in rooms}
return name_to_url[room]
@app.route("/get/<int:page>/<int:results_per_page>")
@limiter.limit(rate_limit)
def get_rooms(page, results_per_page):
start_index = (page - 1) * results_per_page
end_index = start_index + results_per_page
# Return rooms based on pagination
return jsonify(rooms[start_index:end_index])
@app.route("/search/<string:search_query>")
@limiter.limit(rate_limit)
def search_rooms(search_query):
results = []
for room in rooms:
if search_query.lower() in room['name'].lower() or search_query.lower() in room['description'].lower():
results.append(room)
# Return search results
return jsonify(results)
@app.route("/new", methods=["POST"])
@limiter.limit(rate_limit)
def create_room():
name = escape(request.headers.get('name'))
description = escape(request.headers.get('description'))
api_url = escape(request.headers.get('url'))
# Check if room already exists
for room in rooms:
if room['name'] == name and room['description'] == description and room['api_url'] == api_url:
return "x" # Room already exists
# Check if the API URL is valid and accessible
try:
response = requests.get(api_url)
except:
return "n"
try:
if api_url.endswith("/"):
api_ = api_url + "verify/if/it/a/chat/room/of/just/chat"
else:
api_ = api_url + "/verify/if/it/a/chat/room/of/just/chat"
res = requests.get(api_).text
if res == name:
pass
else:
return "n"
except:
return "n"
# Create a new room
new_room = {"name": name, "description": description, "api_url": api_url}
rooms.append(new_room)
# Save the updated rooms data back to "rooms.json" file
with open("rooms.json", "w") as f:
json.dump(rooms, f)
requests.get(api_)
return "s" # Room created successfully
if __name__ == "__main__":
app.run("0.0.0.0")