# -*- coding: utf-8 -*-
import json

from flask import request
from flask_restful import Resource, abort
from textblob import TextBlob

input_error_409 = 'Input must be a list of JSON objects. JSON objects must contain contain key text.'

class Offline_Analysis(Resource):
	def get(self):
		return "POST a list of JSON objects as content-type: application/json. JSON objects must contain key 'text'.", 405
		
	def post(self):
		if not request.json or not isinstance(request.json, (list,)):
			return abort(409, message=input_error_409)
		texts = request.json
		value = 0
		for text in texts:
			if 'text' not in text:
				return abort(409, message=input_error_409)
			analysis = TextBlob(text['text'])
			value += (analysis.sentiment.polarity/2) + 0.5
		sentiment = value/len(texts)
		data = {'sentiment': sentiment}
		return data
