2016-12-26 14:19:12 +08:00
|
|
|
import os
|
2016-12-02 22:24:19 +08:00
|
|
|
import importlib
|
|
|
|
|
|
|
|
from flask import Flask, request
|
|
|
|
|
2016-12-26 16:36:35 +08:00
|
|
|
from little_shit import SkipException, get_filters_dir
|
2016-12-08 21:58:49 +08:00
|
|
|
from filter import apply_filters
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=['POST'])
|
|
|
|
def _index():
|
|
|
|
ctx_msg = request.json
|
|
|
|
try:
|
|
|
|
if ctx_msg.get('msg_class') != 'recv':
|
|
|
|
raise SkipException
|
2016-12-08 21:58:49 +08:00
|
|
|
if not apply_filters(ctx_msg):
|
|
|
|
raise SkipException
|
2016-12-02 22:24:19 +08:00
|
|
|
except SkipException:
|
|
|
|
# Skip this message
|
|
|
|
pass
|
2016-12-26 14:19:12 +08:00
|
|
|
|
2016-12-02 22:24:19 +08:00
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
2016-12-08 21:58:49 +08:00
|
|
|
def _load_filters():
|
|
|
|
filter_mod_files = filter(
|
|
|
|
lambda filename: filename.endswith('.py') and not filename.startswith('_'),
|
|
|
|
os.listdir(get_filters_dir())
|
|
|
|
)
|
|
|
|
command_mods = [os.path.splitext(file)[0] for file in filter_mod_files]
|
|
|
|
for mod_name in command_mods:
|
|
|
|
importlib.import_module('filters.' + mod_name)
|
|
|
|
|
|
|
|
|
2016-12-02 22:24:19 +08:00
|
|
|
if __name__ == '__main__':
|
2016-12-08 21:58:49 +08:00
|
|
|
_load_filters()
|
|
|
|
app.run(host=os.environ.get('HOST', '0.0.0.0'), port=os.environ.get('PORT', '8080'))
|