-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
44 lines (36 loc) · 1.17 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
from flask import Flask
from flask import render_template
from flask import request
import json
from flask import jsonify
import requests
oxd_base_url = "https://od-api.oxforddictionaries.com/api/v2"
with open("credentials.json", "r") as credentials_file:
credentials_json = json.load(credentials_file)
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html")
@app.route("/search", methods=["GET"])
def search():
search_text = request.args.get("text")
# Use a variable at the moment to potentially allow for multi-language support later
source_language = "en"
params = {
"q": search_text,
"prefix": True,
"limit": 30
}
search_request = requests.get(oxd_base_url + "/search/thesaurus/" + source_language,
params=params, headers=credentials_json)
search_json = search_request.json()
words_list = []
for word_data in search_json["results"]:
words_list.append(str.title(word_data["word"]))
return_dict = {
"original": search_text,
"words": words_list
}
return jsonify(return_dict)
if __name__ == '__main__':
app.run()