-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-searchDuckDuckGo.py
70 lines (58 loc) · 2.73 KB
/
action-searchDuckDuckGo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import configparser
from hermes_python.hermes import Hermes
from hermes_python.ffi.utils import MqttOptions
from hermes_python.ontology import *
import io
import requests
import json
CONFIGURATION_ENCODING_FORMAT = "utf-8"
CONFIG_INI = "config.ini"
searchurl = "https://api.duckduckgo.com"
lang = "de"
class SnipsConfigParser(configparser.SafeConfigParser):
def to_dict(self):
return {section : {option_name : option for option_name, option in self.items(section)} for section in self.sections()}
def read_configuration_file(configuration_file):
try:
with io.open(configuration_file, encoding=CONFIGURATION_ENCODING_FORMAT) as f:
conf_parser = SnipsConfigParser()
conf_parser.readfp(f)
return conf_parser.to_dict()
except (IOError, configparser.Error) as e:
return dict()
#def subscribe_intent_callback(hermes, intentMessage):
# conf = read_configuration_file(CONFIG_INI)
# action_wrapper(hermes, intentMessage, conf)
def subscribe_intent_callback(hermes, intentMessage):
conf = read_configuration_file(CONFIG_INI)
hermes.publish_continue_session(intentMessage.session_id, u"Ok,",["ryanrudak:searchDuckDuckGo"])
action_wrapper(hermes, intentMessage, conf)
def action_wrapper(hermes, intentMessage, conf):
# wenn der Indikator ein entsprechendes Wort enthält
if len(intentMessage.slots.article_indicator) > 0:
# Schlagwort, nachdem gesucht werden soll in Variable 'article' speichern
article = intentMessage.slots.article_indicator.first().value
# Schlagwort an der Konsole (zu debug-Zwecken) ausgeben
print("Artikel: "+article)
try:
# ?format=json&pretty=1&lang=de&q=
query_url = "{}/?q={}&format=json&pretty=1&lang={}".format(searchurl, article, lang)
print("Query_URL: "+str(query_url))
headers = {"Accept-Language": "de"}
results = requests.get(query_url, headers=headers)
jsonresponse = results.json()
print("Ergebns: "+str(jsonresponse["AbstractText"]))
summary = jsonresponse["AbstractText"]
hermes.publish_end_session(intentMessage.session_id, summary)
except:
print("Leider ist ein Fehler aufgetreten:"+str(sys.exc_info()[0]))
hermes.publish_end_session(intentMessage.session_id, "Leider ist ein Fehler aufgetreten")
else:
hermes.publish_end_session(intentMessage.session_id, "Ein Fehler beim Indikator ist aufgetreten")
if __name__ == "__main__":
mqtt_opts = MqttOptions()
with Hermes(mqtt_options=mqtt_opts) as h:
h.subscribe_intent("ryanrudak:searchDuckDuckGo", subscribe_intent_callback)\
.start()