-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
62 lines (47 loc) · 1.72 KB
/
server.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
from flask import Flask, request, abort, jsonify
from os import environ
import subprocess
import json
app = Flask(__name__)
def parse_payload(req):
payload = req.get_data()
# payload = unquote_plus(payload)
# payload = re.sub('payload=', '', payload)
if len(payload) == 0:
return False
payload = json.loads(payload)
return payload
def check_payload(payload):
try:
print('Name: ', payload['name'])
print('Slug: ', payload['slug'])
print('Build id: ', payload['build']['id'])
print('Build commit: ', payload['build']['commit'])
print('Build state: ', payload['build']['state'])
print('Build succeded: ', payload['build']['success'])
print('Build date: ', payload['build']['date'])
return True
except KeyError:
return False
@app.route('/', methods=['GET'])
def index():
return ('Welcome to docsearch scraper', 200, None)
@app.route('/run-docsearch-scraper', methods=['POST'])
def run_docsearch_index():
payload = parse_payload(request)
if payload == False:
abort(400)
payload_valid = check_payload(payload)
if payload_valid == False:
abort(400)
state = payload['build']['state']
status = payload['build']['success']
if state == 'finished' and status == True:
subprocess.Popen(["./docsearch", "run", "./docsearch.config.json"])
return jsonify({'message': 'Scraper is running...'})
else:
return jsonify({'message': 'No need to run scraper'})
if __name__ == '__main__':
isDevMode = environ['FLASK_ENV'] == 'development'
host = '127.0.0.1' if isDevMode else '0.0.0.0'
app.run(debug=isDevMode, use_reloader=isDevMode, port=environ['PORT'], host=host)