forked from KlikDev/Anti-abuse-api-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (39 loc) · 1.26 KB
/
app.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
# using flask_restful
from flask import Flask, jsonify, request
from flask_restful import Resource, Api, reqparse
# creating the flask app
app = Flask(__name__)
app.url_map.strict_slashes = False
# function to get file contents
def get_content(filepath):
with open(filepath, 'r') as f:
lines = set() # using sets to avoid duplicate enteries
for line in f:
lines.add(line.strip())
return {'cuss_words': list(lines)}
# creating an API object
api = Api(app)
class Home(Resource):
def get(self):
return 'Invalid Request'
class abuseapi(Resource):
def get(self):
abuses = get_content("cuss_words.txt")
return jsonify(abuses)
class check(Resource):
def get(self):
abuses = get_content("cuss_words.txt")
parser = reqparse.RequestParser()
parser.add_argument('q', type=str)
args = parser.parse_args()
if args['q'] in abuses['cuss_words']:
return jsonify(True)
else:
return jsonify(False)
# adding the defined resources along with their corresponding urls
api.add_resource(Home, '/')
api.add_resource(abuseapi, '/api/v1')
api.add_resource(check, '/api/v1/check')
# driver function
if __name__ == '__main__':
app.run(debug=True)