-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
55 lines (39 loc) · 1.49 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
45
46
47
48
49
50
51
52
53
54
55
import os
from flask import Flask, request, jsonify
from services.monkeylearn_service import MonkeyLearnService
from services.watson_service import WatsonService
from services.spotify_service import SpotifyService
app = Flask(__name__)
app.config.from_object(os.environ.get('APP_SETTINGS'))
@app.route('/')
def hello():
return 'Guten-Reader API'
@app.route('/api/v1/watson')
def watson():
text = request.json['text']
service = WatsonService(text)
sentiment = service.get_sentiment_value()
return jsonify(sentiment)
@app.route('/api/v1/monkeylearn')
def monkeylearn():
text = request.json['text']
service = MonkeyLearnService(text)
sentiment = service.text_sentiment()
return jsonify(sentiment)
@app.route('/api/v1/recommendation', methods=["POST"])
def recommendation():
body = {} if request.get_data() == b'' else request.get_json()
required_params = {'text', 'current_mood', 'access_token', 'genre'}
missing_params = list(required_params - set(body.keys()))
if missing_params:
return jsonify({ 'error': { 'missing_params': missing_params }}), 400
watson_service = WatsonService(body['text'])
new_mood = watson_service.get_sentiment_value()
if body['current_mood'] != new_mood:
spotify_service = SpotifyService(body['access_token'], new_mood, body['genre'])
result = spotify_service.recommend()
return jsonify(result), result['status_code']
else:
return '', 204
if __name__ == '__main__':
app.run()