/
__init__.py
102 lines (85 loc) · 3.71 KB
/
__init__.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
from os.path import dirname
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill
from mycroft.util.log import getLogger
__author__ = 'hschmidt'
LOGGER = getLogger(__name__)
class ShopListSkill(MycroftSkill):
sId = ""
sGuid = ""
sWebService = ""
sManageItem = ""
def __init__(self):
super(ShopListSkill, self).__init__(name="ShopListSkill")
def initialize(self):
self.load_data_files(dirname(__file__))
shopping_list_intent = IntentBuilder("ShopIntent").\
require("ShopVerb").\
require("ShopItem").\
require("ShopLocation").\
build()
self.register_intent(shopping_list_intent,
self.handle_lets_shop_intent)
self.loadConf()
def loadConf(self):
import shopconf
self.sId = shopconf.auth['wsid']
self.sGuid = shopconf.auth['secret']
self.sWebService = shopconf.auth['webservice']
self.sManageItem = shopconf.auth['manageitem']
def handle_lets_shop_intent(self, message):
#self.speak_dialog("item.added", data={"shopitem": message.metadata.get("ShopItem"),\
# "shoplocation": message.metadata.get("ShopLocation")})
qString = self.prepare_querystring(message.metadata.get("ShopVerb"), message.metadata.get("ShopItem"), message.metadata.get("ShopLocation"))
# send the querystring to the web service
print(("****QUERYSTRING DEBUG MESSAGE ******\n"))
print(("Full Querystring: " + str(qString) + "\n"))
print(("Location variable from metadata: " + str(message.metadata.get("ShopLocation")).encode("utf-8")))
print(("**************************************\n"))
self.speak("See the querystring debug message in the skills terminal")
def prepare_querystring(self, shopVerb, shopItem, shopLocation):
import base64
import urllib
from hashlib import md5
# prepare the data for the querystring
sitm = { 'itm' : base64.encodestring(shopItem.encode("utf-8")) }
sItem = urllib.urlencode(sitm)
sloc = { 'loc' : shopLocation.encode("utf-8") }
sLocation = urllib.urlencode(sloc)
sDate = self.getTodayDate()
# create the MD5 hash
stringtoHash = str(self.sGuid) + str(sItem) + str(sLocation) + str(sDate)
sHash = md5(stringtoHash)
# Prep the querystring
# example: ?wsid=1&action=add&itm=b25pb25z%0A&loc=shopping-list&hsh=373e8c57c6da8577f038f194e3d25df3
sQueryString = "?wsid=" + str(self.sId) + "&action=" + \
str(shopVerb) + "&" + str(sItem) + "&" + \
str(sLocation) + "&hsh=" + str(sHash.hexdigest())
return(sQueryString)
def getTodayDate(self):
import os, time
os.environ['TZ'] = "Europe/London"
time.tzset()
localtime = time.localtime()
timeString = time.strftime('%Y%m%d')
return(timeString)
def stop(self):
pass
def create_skill():
return ShopListSkill()