import os, logging, json
from flask import Flask, request, render_template, jsonify
from flask_restplus import Api
from flask_ask import Ask, statement
import requests

from public import input

app = Flask(__name__)

ROOT_URL = os.getenv('ROOT_URL', 'localhost')
VERSION_NO = os.getenv('VERSION_NO', '1.0')
APP_NAME = os.getenv('APP_NAME', "Devil's Advocate")
DEBUG = os.getenv('DEBUG', False)
SENTIMENT_URL = os.getenv("SENTIMENT_URL", "https://ns3-sentiment.herokuapp.com/")
SENTIMENT_ENDPOINT = os.getenv("SENTIMENT_ENDPOINT", 'api/v1/parse')
ACQUIRE_URL = os.getenv("SENTIMENT_URL", "https://ns3-acquire.herokuapp.com/")
ACQUIRE_ENDPOINT = os.getenv("ACQUIRE_ENDPOINT", 'Public/articles/')
print APP_NAME, VERSION_NO
# api = Api(app, version=VERSION_NO, title=APP_NAME)

ask = Ask(app, '/alexa')
logging.getLogger('flask_ask').setLevel(logging.DEBUG)


@ask.intent('Hello')
def startup():
    text = render_template('start')
    return statement(text)

def create_coherent_list(claim_arr):
    converted = [claim.encode('ascii', 'ignore') for claim in claim_arr]
    if len(converted) == 0:
        return "nothing"
    elif len(converted) == 1:
        return converted[0]
    to_say = ', '.join(converted[:-1])
    to_say += 'and ' + converted[-1]
    return to_say

MAX_ARTICLES = 3

@ask.intent("GetNews")
def get_news(topic):
    articles = requests.get(ACQUIRE_URL + ACQUIRE_ENDPOINT + topic)
    print topic, articles
    response = articles.json()
    to_say = []

    for index, article in enumerate(response):
        if index > MAX_ARTICLES:
            break
        claim_params = {
            'article': article
        }
        claim = requests.post(SENTIMENT_URL + SENTIMENT_ENDPOINT, data=json.dumps(claim_params))
        to_say.append(claim.json()[0])
    sentence = create_coherent_list(to_say)
    return statement(render_template('news', topic=topic, opinion=sentence))

if __name__ == '__main__':
    app.run()
    
@@ -1337,32 +1337,14 @@
     
-articles = requests.get(
+url = 
 ACQU
@@ -1377,16 +1377,48 @@
  + topic
+%0A    articles = requests.get(url
 )%0A    pr
@@ -1436,16 +1436,21 @@
 articles
+, url
 %0A    res

debug url