-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubjectDetection.py
64 lines (52 loc) · 1.61 KB
/
SubjectDetection.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
from flask import Flask, request
from flask_limiter import Limiter
from flask_cors import CORS, cross_origin
from random import randint
import re
app = Flask(__name__)
CORS(app, support_credentials=True)
limiter = Limiter(
app,
key_func=lambda: request.headers.get("X-Real-Ip"),
)
url_regex = re.compile(
r"^(?:http|ftp)s?://" # http:// or https://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain...
r"localhost|" # localhost...
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
r"(?::\d+)?" # optional port
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)
def detect(url: str):
identifier = randint(0, 1)
if identifier == 0:
return "False", 200
else:
return "True", 200
@app.route("/vampire", methods=["GET"])
@cross_origin(support_credentials=True)
@limiter.limit("30 per month")
def vampire():
url = request.args.get("url")
if re.match(url_regex, url) is None:
return "Bad Request", 400
return "False", 200 # Impossible to photograph vampires
@app.route("/dog", methods=["GET"])
@cross_origin(support_credentials=True)
@limiter.limit("30 per month")
def dog():
url = request.args.get("url")
if re.match(url_regex, url) is None:
return "Bad Request", 400
return detect(url)
@app.route("/cat", methods=["GET"])
@cross_origin(support_credentials=True)
@limiter.limit("30 per month")
def cat():
url = request.args.get("url")
if re.match(url_regex, url) is None:
return "Bad Request", 400
return detect(url)
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0")