import logging
import os
import re
from six.moves.urllib.request import urlopen
from flask import Flask
from flask_ask import Ask, request, session, question, statement
app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
# URL prefix to download history content from Wikipedia.
URL_PREFIX = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts' + \
'&format=json&explaintext=&exsectionformat=plain&redirects=&titles='
# Constant defining number of events to be read at one time.
PAGINATION_SIZE = 3
# Length of the delimiter between individual events.
DELIMITER_SIZE = 2
# Size of events from Wikipedia response.
SIZE_OF_EVENTS = 10
# Constant defining session attribute key for the event index
SESSION_INDEX = 'index'
# Constant defining session attribute key for the event text key for date of events.
SESSION_TEXT = 'text'
@ask.launch
def launch():
speech_output = 'History buff. What day do you want events for?'
reprompt_text = "With History Buff, you can get historical events for any day of the year. " + \
"For example, you could say today, or August thirtieth. " + \
"Now, which day do you want?"
return question(speech_output).reprompt(reprompt_text)
@ask.intent('GetFirstEventIntent', convert={ 'day': 'date' })
def get_first_event(day):
month_name = day.strftime('%B')
day_number = day.day
events = _get_json_events_from_wikipedia(month_name, day_number)
if not events:
speech_output = "There is a problem connecting to Wikipedia at this time. Please try again later."
return statement('
For {} {}
".format(month_name, day_number) card_output = "" for i in range(PAGINATION_SIZE): speech_output += "{}
".format(events[i]) card_output += "{}\n".format(events[i]) speech_output += " Wanna go deeper into history?" card_output += " Wanna go deeper into history?" reprompt_text = "With History Buff, you can get historical events for any day of the year. " + \ "For example, you could say today, or August thirtieth. " + \ "Now, which day do you want?" session.attributes[SESSION_INDEX] = PAGINATION_SIZE session.attributes[SESSION_TEXT] = events speech_output = '{}
".format(events[index]) card_output += "{}\n".format(events[index]) i += 1 index += 1 speech_output += " Wanna go deeper into history?" reprompt_text = "Do you want to know more about what happened on this date?" session.attributes[SESSION_INDEX] = index speech_output = '