-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
63 lines (53 loc) · 1.77 KB
/
run.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
63
from flask import Flask, request, jsonify, abort
from config import wordnikApiKey
from os import environ
from flask_cors import CORS
from wordnik import *
apiUrl = 'http://api.wordnik.com/v4'
client = swagger.ApiClient(wordnikApiKey, apiUrl)
wordApi = WordApi.WordApi(client)
environ["FLASK_APP"] = __name__
environ["FLASK_DEBUG"] = "1"
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route('/')
def home():
return "<p> /api/word/define?word='{word}' <br> /api/word/related?word='{word}' <br> /api/word/sentences?word='{word}' </p>"
# Example : /api/word/define?word='{word}'
@app.route('/api/word/define')
def define():
try:
limit = 1
word = request.args['word']
print('started')
definition = wordApi.getDefinitions(word=word, limit=limit)[0].text
return jsonify({'data':definition})
except Exception as e:
jsonify({'error':e})
abort(404)
# Example : /api/word/related?word='{word}'
@app.route('/api/word/related')
def related():
try:
limit = 4
word = request.args['word']
related = wordApi.getRelatedWords(word=word, limit=limit)[0].words
print(dir(related))
return jsonify({'data':related})
except Exception as e:
jsonify({'error':e})
abort(404)
# Example : /api/word/sentences?word='{word}'
@app.route('/api/word/sentences')
def sentences():
try:
limit = 3
word = request.args['word']
examples_list = [example.text for example in wordApi.getExamples(word=word, limit=limit).examples]
print(f'examples_list: {examples_list}')
return jsonify({'data':examples_list})
except Exception as e:
jsonify({'error':e})
abort(404)
if __name__ == "__main__":
app.run()