-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlexaSkill.py
More file actions
153 lines (130 loc) · 5.87 KB
/
AlexaSkill.py
File metadata and controls
153 lines (130 loc) · 5.87 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from flask import Flask
from flask_ask import Ask, statement, convert_errors
import logging
import requests
from bs4 import BeautifulSoup
# Some initial setup for an Alexa skill using Flask-Ask
app = Flask(__name__)
ask = Ask(app, '/')
# To determine the correct grammar
# #SorryForUsingGlobalVariables
first = True
# No idea what this does
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
# A function to turn the lists into strings
def string_from_lists(hourlist, minutelist, am_or_pm, pill_name):
# Yes I am using a global variable
global first
# initialize empty string
pills = ''
# Some loops to throw everything into speech format
for x in range(len(hourlist)):
if hourlist[x] > 12:
# You don't say 15 o'clock
hourlist[x] %= 12
am_or_pm.append(' PM ')
else:
am_or_pm.append(' AM ')
if first:
# Basically this is necessary so Alexa doesn't say "You have and ..."
pills = str(hourlist[x]) + ' ' + str(minutelist[x]) + am_or_pm[x]
first = False
else:
# Now you use "and ..."
pills += ' and ' + str(hourlist[x]) + ' ' + str(minutelist[x]) + am_or_pm[x]
# Tell them what pill and when"
return pill_name + ' at ' + pills + '. '
# The intent name goes into the quotations
@ask.intent('GPIOControlIntent', mapping={})
def gpio_control():
# Create the output speech string, the lists to store the scrapings, and a list for am or pm
global first
pills = ''
pill1_hour_list = []
pill1_minute_list = []
pill2_hour_list = []
pill2_minute_list = []
pill3_hour_list = []
pill3_minute_list = []
pill4_hour_list = []
pill4_minute_list = []
pill5_hour_list = []
pill5_minute_list = []
pill6_hour_list = []
pill6_minute_list = []
am_or_pm = []
# Open the local host port 80 for scraping
r = requests.get("http://0.0.0.0:80")
# Grab and transcribe the raw html using Beautiful Soup
data = r.content
page_soup = BeautifulSoup(data, "html.parser")
# Attempt to scrape everything
try:
pill1 = page_soup.findAll("td", {"class": "pill1Time"})
pill2 = page_soup.findAll("td", {"class": "pill2Time"})
pill3 = page_soup.findAll("td", {"class": "pill3Time"})
pill4 = page_soup.findAll("td", {"class": "pill4Time"})
pill5 = page_soup.findAll("td", {"class": "pill5Time"})
pill6 = page_soup.findAll("td", {"class": "pill6Time"})
pill1name = page_soup.find("p", {"id": "pill1name"}).text
pill2name = page_soup.find("p", {"id": "pill2name"}).text
pill3name = page_soup.find("p", {"id": "pill3name"}).text
pill4name = page_soup.find("p", {"id": "pill4name"}).text
pill5name = page_soup.find("p", {"id": "pill5name"}).text
pill6name = page_soup.find("p", {"id": "pill6name"}).text
# Loop through all elements in the list of pill times and store them in lists
for element in pill1:
this_time = element.text.split()
pill1_hour_list.append(int(this_time[0]))
pill1_minute_list.append(int(this_time[2]))
for element in pill2:
this_time = element.text.split()
pill2_hour_list.append(int(this_time[0]))
pill2_minute_list.append(int(this_time[2]))
for element in pill3:
this_time = element.text.split()
pill3_hour_list.append(int(this_time[0]))
pill3_minute_list.append(int(this_time[2]))
for element in pill4:
this_time = element.text.split()
pill4_hour_list.append(int(this_time[0]))
pill4_minute_list.append(int(this_time[2]))
for element in pill5:
this_time = element.text.split()
pill5_hour_list.append(int(this_time[0]))
pill5_minute_list.append(int(this_time[2]))
for element in pill6:
this_time = element.text.split()
pill6_hour_list.append(int(this_time[0]))
pill6_minute_list.append(int(this_time[2]))
# If there are no pills when asked tell them that
if len(pill1_hour_list) == 0 and len(pill2_hour_list) == 0 and len(pill3_hour_list) == 0 and \
len(pill4_hour_list) == 0 and len(pill5_hour_list) == 0 and len(pill6_hour_list) == 0:
return statement('You have no pills')
# If any of the pills have times to dispense go to the function and determine the speech output
if len(pill1_hour_list) > 0:
first = True
pills += string_from_lists(pill1_hour_list, pill1_minute_list, am_or_pm, pill1name)
if len(pill2_hour_list) > 0:
first = True
pills += string_from_lists(pill2_hour_list, pill2_minute_list, am_or_pm, pill2name)
if len(pill3_hour_list) > 0:
first = True
pills += string_from_lists(pill3_hour_list, pill3_minute_list, am_or_pm, pill3name)
if len(pill4_hour_list) > 0:
first = True
pills += string_from_lists(pill4_hour_list, pill4_minute_list, am_or_pm, pill4name)
if len(pill5_hour_list) > 0:
first = True
pills += string_from_lists(pill5_hour_list, pill5_minute_list, am_or_pm, pill5name)
if len(pill6_hour_list) > 0:
first = True
pills += string_from_lists(pill6_hour_list, pill6_minute_list, am_or_pm, pill6name)
# Return the statement of the pills formatted with the correct speech
return statement('You have {}'.format(pills))
# If it doesn't work say we have an error
except Exception as e:
return statement('There was an error')
if __name__ == '__main__':
# We will host this one on port 5000
app.run(host='0.0.0.0', port=5000)