-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (63 loc) · 1.99 KB
/
app.py
File metadata and controls
73 lines (63 loc) · 1.99 KB
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
from flask import Flask, jsonify, request
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
from flask_cors import CORS
import wptools
import re
import json
cleanr = re.compile('\[[^\]]*\||\[\[|\]\]')
# removing_pipes = re.compile('\[[^\]]*\||')
app = Flask(__name__)
CORS(app)
cors = CORS(app, resource={
r"/*":{
"origins":"*"
}
})
with open('fields.txt') as f:
fields_list = [x.rstrip() for x in f] # remove line breaks
def clean_info_string(string):
cleantext = re.sub(cleanr, '', string)
# cleantext = re.sub(removing_pipes, ' ', cleantext)
if("{{" in cleantext):
return ""
return cleantext
def process_info_box(infobox):
returned_infobox = {}
for key in infobox.keys():
if key in fields_list:
clean_text = clean_info_string(infobox[key])
if (len(clean_text) > 0):
returned_infobox[key] = clean_text
return returned_infobox
def responsify(data, status, error):
return {'data': data, 'status': status, 'error': error}
@app.route('/')
def index():
return "Hello, World!"
@app.route('/info')
def info():
try:
title = request.args.get('search')
page = wptools.page(title)
page.get_query()
return_obj = {}
extext = ""
aliases = []
images = page.images(['url'])
if 'extext' in page.data:
extext = page.data['extext']
if 'aliases' in page.data:
aliases = page.data['aliases']
return_obj['images'] = [img['url'] for img in images]
return_obj['extext'] = extext
return_obj['aliases'] = aliases
wikidata = wptools.page(title).get_parse()
infobox = wikidata.data['infobox']
return_obj['infobox'] = process_info_box(infobox)
return jsonify(responsify(return_obj, 200, ""))
except Exception as e:
print("Incorrect")
return jsonify(responsify({}, 400, str(e)))
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=8889)