2019-04-24 16:29:24 +00:00
|
|
|
import logging
|
2019-05-15 13:58:28 +00:00
|
|
|
from OpenSSL import SSL
|
2019-04-24 16:29:24 +00:00
|
|
|
import os
|
|
|
|
|
from flask import Flask
|
|
|
|
|
from flask_ask import Ask, request, session, question, statement
|
|
|
|
|
import random
|
|
|
|
|
import yaml
|
2019-04-26 20:53:04 +00:00
|
|
|
import siteobj as site2
|
2019-05-13 09:53:13 +00:00
|
|
|
import util
|
2019-04-24 16:29:24 +00:00
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
ask = Ask(app, "/")
|
|
|
|
|
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
|
|
|
|
|
|
2019-05-13 09:53:13 +00:00
|
|
|
### ###
|
|
|
|
|
# Helper #
|
|
|
|
|
### ###
|
|
|
|
|
|
|
|
|
|
def get_site_obj(site):
|
|
|
|
|
if site == "golem":
|
|
|
|
|
obj = site2.Golem()
|
|
|
|
|
elif site.lower() == "spiegel":
|
|
|
|
|
obj = site2.Spiegel()
|
|
|
|
|
else:
|
|
|
|
|
obj = None
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
### ###
|
|
|
|
|
# CONTROLLER #
|
|
|
|
|
### ###
|
2019-04-24 16:29:24 +00:00
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
@ask.intent('searchon', mapping={'site': 'Site'}, default={'site': 'golem'})
|
|
|
|
|
def search_on(site):
|
2019-05-13 09:53:13 +00:00
|
|
|
session.attributes["siteName"] = site
|
2019-04-24 16:29:24 +00:00
|
|
|
|
2019-05-13 09:53:13 +00:00
|
|
|
if not util.session_value_is(session.attributes, "searchTerm", None) and util.session_value_is(session.attributes, "lastCall", "searchfor"):
|
2019-05-02 18:04:10 +00:00
|
|
|
searchTerm = session.attributes["searchTerm"]
|
|
|
|
|
return search_for(searchTerm)
|
2019-05-13 09:53:13 +00:00
|
|
|
|
|
|
|
|
if util.session_value_is(session.attributes, "lastCall", "news"):
|
2019-05-02 18:04:10 +00:00
|
|
|
return news(site)
|
2019-05-13 09:53:13 +00:00
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
session.attributes["lastCall"] = "searchon"
|
|
|
|
|
return question("Wonach?")
|
2019-04-24 16:29:24 +00:00
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
@ask.intent('searchfor', mapping={'searchTerm':'Topic'}, default={'searchTerm':''})
|
|
|
|
|
def search_for(searchTerm):
|
2019-05-13 09:53:13 +00:00
|
|
|
site = util.get_session_value(session.attributes, "siteName")
|
2019-05-02 18:04:10 +00:00
|
|
|
|
2019-05-13 09:53:13 +00:00
|
|
|
if site is not None:
|
|
|
|
|
obj = get_site_obj(site)
|
|
|
|
|
else:
|
2019-05-02 18:04:10 +00:00
|
|
|
session.attributes["searchTerm"] = searchTerm
|
|
|
|
|
session.attributes["lastCall"] = "searchfor"
|
|
|
|
|
return question("Auf welcher Seite wollen Sie hiernach Suchen?")
|
2019-05-13 09:53:13 +00:00
|
|
|
|
|
|
|
|
if obj is None: # should never be called
|
|
|
|
|
return question("Error. Auf welcher Seite wollen Sie hiernach suchen?")
|
2019-04-24 16:29:24 +00:00
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
articles, links = obj.search_article(searchTerm)
|
2019-05-13 09:53:13 +00:00
|
|
|
|
2019-04-26 20:53:04 +00:00
|
|
|
session.attributes["lastSearch"] = links
|
2019-05-13 09:53:13 +00:00
|
|
|
session.attributes["lastCall"] = "searchfor"
|
2019-04-26 20:53:04 +00:00
|
|
|
|
2019-05-13 09:53:13 +00:00
|
|
|
response = "Für welchen der folgenden Artikel interessieren Sie sich?"
|
2019-05-02 18:04:10 +00:00
|
|
|
if len(articles) > 0:
|
2019-05-07 18:10:28 +00:00
|
|
|
for i in range(0, min(5, len(articles))):
|
2019-05-02 18:04:10 +00:00
|
|
|
response += articles[i]
|
|
|
|
|
else:
|
|
|
|
|
return question("Dazu konnte nichts gefunden werden. Möchten Sie nach etwas anderem Suchen?")
|
2019-04-26 20:53:04 +00:00
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
|
|
|
|
|
return question(response + "noch etwas?")
|
|
|
|
|
|
|
|
|
|
@ask.intent('News', mapping={'site': 'Site'}, default={'site': ''})
|
2019-04-26 20:53:04 +00:00
|
|
|
def news(site):
|
2019-05-13 09:53:13 +00:00
|
|
|
site = util.get_session_value(session.attributes, "siteName")
|
|
|
|
|
|
|
|
|
|
if site is not None:
|
|
|
|
|
obj = get_site_obj(site)
|
|
|
|
|
else:
|
2019-05-02 18:04:10 +00:00
|
|
|
session.attributes["lastCall"] = "news"
|
|
|
|
|
return question("Auf welcher Seite wollen Sie hiernach Suchen?")
|
2019-05-13 09:53:13 +00:00
|
|
|
|
|
|
|
|
if obj is None:
|
2019-05-02 18:04:10 +00:00
|
|
|
return statement("error")
|
|
|
|
|
|
2019-05-07 18:10:28 +00:00
|
|
|
news, links = obj.get_news()
|
2019-05-02 18:04:10 +00:00
|
|
|
|
2019-05-13 09:53:13 +00:00
|
|
|
session.attributes["lastSearch"] = links
|
|
|
|
|
session.attributes["lastCall"] = "news"
|
|
|
|
|
|
2019-04-24 16:29:24 +00:00
|
|
|
response = ""
|
2019-05-07 18:10:28 +00:00
|
|
|
for i in range(0, min(5, len(news))):
|
2019-05-02 18:04:10 +00:00
|
|
|
response += news[i] + ". "
|
|
|
|
|
|
2019-05-07 18:10:28 +00:00
|
|
|
return question(response)
|
2019-05-02 18:04:10 +00:00
|
|
|
|
|
|
|
|
@ask.intent('SearchTwo', mapping={'number': 'Nummer'}, default={'number': 1})
|
|
|
|
|
def search_answer(number):
|
2019-05-13 09:53:13 +00:00
|
|
|
site = util.get_session_value(session.attributes, "siteName")
|
|
|
|
|
|
|
|
|
|
if site is not None:
|
|
|
|
|
obj = get_site_obj(site)
|
|
|
|
|
else:
|
|
|
|
|
session.attributes["lastCall"] = "search_answer"
|
|
|
|
|
return question("Wonach wollen Sie suchen?")
|
|
|
|
|
|
|
|
|
|
if obj is None: # should never be called
|
|
|
|
|
return question("Error. Wonach wollen Sie suchen?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
links = util.get_session_value(session.attributes, "lastSearch")
|
2019-05-07 18:10:28 +00:00
|
|
|
|
2019-05-13 09:53:13 +00:00
|
|
|
# if the site uses relative links, make absolute ones
|
2019-05-08 14:48:42 +00:00
|
|
|
if "http" not in str(links):
|
|
|
|
|
newLinks = []
|
|
|
|
|
for link in links:
|
|
|
|
|
if "http" not in link:
|
|
|
|
|
newLinks.append(obj.baseURL + link)
|
|
|
|
|
links = newLinks
|
2019-05-07 18:10:28 +00:00
|
|
|
|
|
|
|
|
art = obj.read_headlines(links[int(number)-1])
|
2019-05-02 18:04:10 +00:00
|
|
|
response = ""
|
|
|
|
|
for element in art:
|
2019-05-08 14:48:42 +00:00
|
|
|
response += element
|
|
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
session.attributes["lastCall"] = "search2"
|
2019-04-24 16:29:24 +00:00
|
|
|
return statement(response)
|
|
|
|
|
|
|
|
|
|
@ask.intent('AMAZON.HelpIntent')
|
|
|
|
|
def help():
|
|
|
|
|
speech_text = 'Dieser Skill erlaubt es Ihnen einige Nachrichten Websites zu nutzen'
|
|
|
|
|
return statement(speech_text)
|
|
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
@ask.intent('AMAZON.FallbackIntent')
|
|
|
|
|
def fallback():
|
|
|
|
|
return statement("ein fehler ist aufgetreten")
|
|
|
|
|
|
2019-04-24 16:29:24 +00:00
|
|
|
@ask.launch
|
|
|
|
|
def launch():
|
2019-05-02 18:04:10 +00:00
|
|
|
return question("Was möchten Sie tun?")
|
2019-04-24 16:29:24 +00:00
|
|
|
|
|
|
|
|
@ask.session_ended
|
|
|
|
|
def session_ended():
|
|
|
|
|
return "{}", 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if 'ASK_VERIFY_REQUESTS' in os.environ:
|
|
|
|
|
verify = str(os.environ.get('ASK_VERIFY_REQUESTS', '')).lower()
|
|
|
|
|
if verify == 'false':
|
|
|
|
|
app.config['ASK_VERIFY_REQUESTS'] = False
|
2019-05-15 13:58:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
context = SSL.Context(SSL.TLSv1_2_METHOD)
|
|
|
|
|
cer = os.path.join(os.path.dirname(__file__), 'certificate.pem')
|
|
|
|
|
key = os.path.join(os.path.dirname(__file__), 'privkey.pem')
|
|
|
|
|
context = (cer, key)
|
|
|
|
|
|
|
|
|
|
app.run(host='127.0.0.1',port=443,ssl_context=context)
|