refactored
This commit is contained in:
parent
22be23224f
commit
d01e6f10eb
Binary file not shown.
Binary file not shown.
103
reader/main.py
103
reader/main.py
|
|
@ -5,102 +5,113 @@ from flask_ask import Ask, request, session, question, statement
|
||||||
import random
|
import random
|
||||||
import yaml
|
import yaml
|
||||||
import siteobj as site2
|
import siteobj as site2
|
||||||
|
import util
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
ask = Ask(app, "/")
|
ask = Ask(app, "/")
|
||||||
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
|
logging.getLogger('flask_ask').setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
### ###
|
||||||
|
# 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 #
|
||||||
|
### ###
|
||||||
|
|
||||||
@ask.intent('searchon', mapping={'site': 'Site'}, default={'site': 'golem'})
|
@ask.intent('searchon', mapping={'site': 'Site'}, default={'site': 'golem'})
|
||||||
def search_on(site):
|
def search_on(site):
|
||||||
try:
|
session.attributes["siteName"] = site
|
||||||
session.attributes["siteName"] = site
|
|
||||||
print(session.attributes["siteName"])
|
|
||||||
except:
|
|
||||||
print("error")
|
|
||||||
|
|
||||||
if "searchTerm" in session.attributes and session.attributes["searchTerm"] is not None and "lastCall" in session.attributes and session.attributes["lastCall"] == "searchfor":
|
if not util.session_value_is(session.attributes, "searchTerm", None) and util.session_value_is(session.attributes, "lastCall", "searchfor"):
|
||||||
searchTerm = session.attributes["searchTerm"]
|
searchTerm = session.attributes["searchTerm"]
|
||||||
session.attributes["searchTerm"] = None
|
|
||||||
return search_for(searchTerm)
|
return search_for(searchTerm)
|
||||||
if "lastCall" in session.attributes and session.attributes["lastCall"] == "news":
|
|
||||||
|
if util.session_value_is(session.attributes, "lastCall", "news"):
|
||||||
return news(site)
|
return news(site)
|
||||||
|
|
||||||
session.attributes["lastCall"] = "searchon"
|
session.attributes["lastCall"] = "searchon"
|
||||||
return question("Wonach?")
|
return question("Wonach?")
|
||||||
|
|
||||||
@ask.intent('searchfor', mapping={'searchTerm':'Topic'}, default={'searchTerm':''})
|
@ask.intent('searchfor', mapping={'searchTerm':'Topic'}, default={'searchTerm':''})
|
||||||
def search_for(searchTerm):
|
def search_for(searchTerm):
|
||||||
try:
|
site = util.get_session_value(session.attributes, "siteName")
|
||||||
site = session.attributes["siteName"]
|
|
||||||
except:
|
|
||||||
site = None
|
|
||||||
|
|
||||||
if site == "golem":
|
if site is not None:
|
||||||
obj = site2.Golem()
|
obj = get_site_obj(site)
|
||||||
elif site.lower() == "spiegel":
|
else:
|
||||||
obj = site2.Spiegel()
|
|
||||||
elif site is None:
|
|
||||||
session.attributes["searchTerm"] = searchTerm
|
session.attributes["searchTerm"] = searchTerm
|
||||||
session.attributes["lastCall"] = "searchfor"
|
session.attributes["lastCall"] = "searchfor"
|
||||||
return question("Auf welcher Seite wollen Sie hiernach Suchen?")
|
return question("Auf welcher Seite wollen Sie hiernach Suchen?")
|
||||||
else:
|
|
||||||
return statement("error")
|
if obj is None: # should never be called
|
||||||
|
return question("Error. Auf welcher Seite wollen Sie hiernach suchen?")
|
||||||
|
|
||||||
articles, links = obj.search_article(searchTerm)
|
articles, links = obj.search_article(searchTerm)
|
||||||
session.attributes["lastSearch"] = links
|
|
||||||
response = "Für welchen der folgenden Artikel interessieren Sie sich?"
|
|
||||||
|
|
||||||
|
session.attributes["lastSearch"] = links
|
||||||
|
session.attributes["lastCall"] = "searchfor"
|
||||||
|
|
||||||
|
response = "Für welchen der folgenden Artikel interessieren Sie sich?"
|
||||||
if len(articles) > 0:
|
if len(articles) > 0:
|
||||||
for i in range(0, min(5, len(articles))):
|
for i in range(0, min(5, len(articles))):
|
||||||
response += articles[i]
|
response += articles[i]
|
||||||
else:
|
else:
|
||||||
return question("Dazu konnte nichts gefunden werden. Möchten Sie nach etwas anderem Suchen?")
|
return question("Dazu konnte nichts gefunden werden. Möchten Sie nach etwas anderem Suchen?")
|
||||||
|
|
||||||
session.attributes["lastCall"] = "searchfor"
|
|
||||||
|
|
||||||
return question(response + "noch etwas?")
|
return question(response + "noch etwas?")
|
||||||
|
|
||||||
@ask.intent('News', mapping={'site': 'Site'}, default={'site': ''})
|
@ask.intent('News', mapping={'site': 'Site'}, default={'site': ''})
|
||||||
def news(site):
|
def news(site):
|
||||||
try:
|
site = util.get_session_value(session.attributes, "siteName")
|
||||||
session.attributes["siteName"] = site
|
|
||||||
except:
|
if site is not None:
|
||||||
print("error")
|
obj = get_site_obj(site)
|
||||||
print(site)
|
else:
|
||||||
if site == "golem":
|
|
||||||
obj = site2.Golem()
|
|
||||||
elif site.lower() == "spiegel":
|
|
||||||
obj = site2.Spiegel()
|
|
||||||
elif site == '':
|
|
||||||
session.attributes["lastCall"] = "news"
|
session.attributes["lastCall"] = "news"
|
||||||
return question("Auf welcher Seite wollen Sie hiernach Suchen?")
|
return question("Auf welcher Seite wollen Sie hiernach Suchen?")
|
||||||
else:
|
|
||||||
|
if obj is None:
|
||||||
return statement("error")
|
return statement("error")
|
||||||
|
|
||||||
news, links = obj.get_news()
|
news, links = obj.get_news()
|
||||||
print(news)
|
|
||||||
session.attributes["lastSearch"] = links
|
|
||||||
|
|
||||||
|
session.attributes["lastSearch"] = links
|
||||||
|
session.attributes["lastCall"] = "news"
|
||||||
|
|
||||||
response = ""
|
response = ""
|
||||||
for i in range(0, min(5, len(news))):
|
for i in range(0, min(5, len(news))):
|
||||||
response += news[i] + ". "
|
response += news[i] + ". "
|
||||||
|
|
||||||
session.attributes["lastCall"] = "news"
|
|
||||||
return question(response)
|
return question(response)
|
||||||
|
|
||||||
@ask.intent('SearchTwo', mapping={'number': 'Nummer'}, default={'number': 1})
|
@ask.intent('SearchTwo', mapping={'number': 'Nummer'}, default={'number': 1})
|
||||||
def search_answer(number):
|
def search_answer(number):
|
||||||
try:
|
site = util.get_session_value(session.attributes, "siteName")
|
||||||
site = session.attributes["siteName"]
|
|
||||||
except:
|
|
||||||
site = None
|
|
||||||
print(number)
|
|
||||||
if site == "golem":
|
|
||||||
obj = site2.Golem()
|
|
||||||
elif site.lower() == "spiegel":
|
|
||||||
obj = site2.Spiegel()
|
|
||||||
|
|
||||||
links = session.attributes["lastSearch"]
|
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")
|
||||||
|
|
||||||
|
# if the site uses relative links, make absolute ones
|
||||||
if "http" not in str(links):
|
if "http" not in str(links):
|
||||||
newLinks = []
|
newLinks = []
|
||||||
for link in links:
|
for link in links:
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,12 @@ class Site:
|
||||||
title += tree.xpath(self.xPath["readHeadlineText"])
|
title += tree.xpath(self.xPath["readHeadlineText"])
|
||||||
return title
|
return title
|
||||||
|
|
||||||
|
# not used, who wants to listen to alexa for 10 minutes?
|
||||||
def read_article(self, url):
|
def read_article(self, url):
|
||||||
site = requests.get(url)
|
site = requests.get(url)
|
||||||
tree = html.fromstring(site.content)
|
tree = html.fromstring(site.content)
|
||||||
|
|
||||||
|
# may need to be reworked
|
||||||
title = self.read_headlines(url)
|
title = self.read_headlines(url)
|
||||||
title += tree.xpath(self.xPath["readArticleText"])
|
title += tree.xpath(self.xPath["readArticleText"])
|
||||||
return title
|
return title
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
|
||||||
|
|
||||||
|
def check_session_for(sessionAttributes, key):
|
||||||
|
if key in sessionAttributes:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# returns value to key in session if set
|
||||||
|
# returns None if not
|
||||||
|
def session_value_is(sessionAttributes, key, value):
|
||||||
|
if key in sessionAttributes and sessionAttributes[key] == value:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_session_value(sessionAttributes, key):
|
||||||
|
if key in sessionAttributes:
|
||||||
|
return sessionAttributes[key]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
Loading…
Reference in New Issue