-
Notifications
You must be signed in to change notification settings - Fork 0
/
siri2.py
134 lines (113 loc) · 4.73 KB
/
siri2.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
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
import time
import imaplib
import email
import os
import pkgutil
##########################################
# Add your gmail username and password here
username = "audifonos.son.la.compania@gmail.com"
password = "blackmetalhouse3"
##########################################
class ControlException(Exception):
pass
class Control():
def __init__(self, username, password):
print("------------------------------------------------------")
print("- SIRI CONTROL -")
print("- Created by Sanjeet Chatterjee -")
print("- Website: https://medium.com/@thesanjeetc -")
print("------------------------------------------------------")
try:
self.last_checked = -1
self.mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
self.mail.login(username, password)
self.mail.list()
self.mail.select("Notes")
# Gets last Note id to stop last command from executing
result, uidlist = self.mail.search(None, "ALL")
try:
self.last_checked = uidlist[0].split()[-1]
except IndexError:
pass
self.load()
self.handle()
except imaplib.IMAP4.error:
print("Your username and password is incorrect")
print("Or IMAP is not enabled.")
def load(self):
"""Try to load all modules found in the modules folder"""
print("\n")
print("Loading modules...")
self.modules = []
path = os.path.join(os.path.dirname(__file__), "modules")
directory = pkgutil.iter_modules(path=[path])
for finder, name, ispkg in directory:
try:
loader = finder.find_module(name)
module = loader.load_module(name)
if hasattr(module, "commandWords") \
and hasattr(module, "moduleName") \
and hasattr(module, "execute"):
self.modules.append(module)
print("The module '{0}' has been loaded, "
"successfully.".format(name))
else:
print("[ERROR] The module '{0}' is not in the "
"correct format.".format(name))
except:
print("[ERROR] The module '" + name + "' has some errors.")
print("\n")
def fetch_command(self):
"""Retrieve the last Note created if new id found"""
self.mail.list()
self.mail.select("Notes")
result, uidlist = self.mail.search(None, "ALL")
try:
latest_email_id = uidlist[0].split()[-1]
except IndexError:
return
if latest_email_id == self.last_checked:
return
self.last_checked = latest_email_id
result, data = self.mail.fetch(latest_email_id, "(RFC822)")
voice_command = email.message_from_string(data[0][1].decode('utf-8'))
return str(voice_command.get_payload()).lower().strip()
def handle(self):
"""Handle new commands
Poll continuously every second and check for new commands.
"""
print("Fetching commands...")
print("\n")
while True:
try:
command = self.fetch_command()
if not command:
raise ControlException("No command found.")
print("The word(s) '" + command + "' have been said")
if command == "abre youtube y pon metallica":
print("buena la banda")
for module in self.modules:
foundWords = []
for word in module.commandWords:
if str(word) in command:
foundWords.append(str(word))
if len(foundWords) == len(module.commandWords):
try:
module.execute(command)
print("The module {0} has been executed "
"successfully.".format(module.moduleName))
except:
print("[ERROR] There has been an error "
"when running the {0} module".format(
module.moduleName))
else:
print("\n")
except (TypeError, ControlException):
pass
except Exception as exc:
print("Received an exception while running: {exc}".format(
**locals()))
print("Restarting...")
time.sleep(1)
if __name__ == '__main__':
Control(username, password)