nico 8 éve
szülő
commit
8b51a46ce9

+ 1 - 0
Docs/dev_env_install.md

@@ -27,6 +27,7 @@ pip install python2-pythondialog
 pip install jinja
 pip install python-crontab
 pip install cffi
+pip install pygmail
 ```
 
 ### Test your env

+ 9 - 1
brain.yml

@@ -111,4 +111,12 @@
           message: "Aurevoir"
       - kill_switch
     signals:
-      - order: "ferme-toi"
+      - order: "ferme-toi"
+
+  - name: "check email"
+    neurons:
+      - gmail_checker:
+          username: "me@gmail.com"
+          password: "my_password"
+    signals:
+      - order: "est-ce que j'ai des emails"

+ 2 - 1
core/OrderListener.py

@@ -22,8 +22,9 @@ class OrderListener:
         """
         # this is a trick to ignore ALSA output error
         # see http://stackoverflow.com/questions/7088672/pyaudio-working-but-spits-out-error-messages-each-time
-        self._ignore_stderr()
         self.stt = stt
+        self._ignore_stderr()
+        self.stt_module_name = stt
         self.callback = callback
         self.settings = SettingLoader.get_settings()
 

+ 1 - 0
neurons/__init__.py

@@ -5,4 +5,5 @@ from say import Say
 from script import Script
 from sleep import Sleep
 from systemdate import Systemdate
+from gmail_checker import Gmail_checker
 

+ 1 - 0
neurons/gmail_checker/__init__.py

@@ -0,0 +1 @@
+from gmail_checker import Gmail_checker

+ 68 - 0
neurons/gmail_checker/gmail_checker.py

@@ -0,0 +1,68 @@
+# coding: utf8
+import logging
+
+from gmail import Gmail
+from email.header import decode_header
+from core.NeuronModule import NeuronModule
+
+logging.basicConfig()
+logger = logging.getLogger("jarvis")
+
+
+class MissingParameterException(Exception):
+    pass
+
+
+class Gmail_checker(NeuronModule):
+    def __init__(self, **kwargs):
+        super(Gmail_checker, self).__init__(**kwargs)
+
+        # check if parameters have been provided
+        username = kwargs.get('username', None)
+        password = kwargs.get('password', None)
+
+        if username is None:
+            raise MissingParameterException("Username parameter required")
+
+        if password is None:
+            raise MissingParameterException("Password parameter required")
+
+        # prepare a returned dict
+        returned_dict = dict()
+
+        g = Gmail()
+        g.login(username, password)
+
+        # check if login succeed
+        logging.debug("Gmail loggin ok: %s" % g.logged_in)  # Should be True, AuthenticationError if login fails
+
+        # get unread mail
+        unread = g.inbox().mail(unread=True)
+
+        returned_dict["unread"] = len(unread)
+
+        if len(unread) > 0:
+            # add a list of subject
+            subject_list = list()
+            for email in unread:
+                email.fetch()
+                encoded_subject = email.subject
+                subject = self._parse_subject(encoded_subject)
+                subject_list.append(subject)
+
+            returned_dict["subjects"] = subject_list
+
+        print returned_dict
+        # logout of gmail
+        g.logout()
+
+    @staticmethod
+    def _parse_subject(encoded_subject):
+        dh = decode_header(encoded_subject)
+        # TODO decode that shit
+        print str(encoded_subject.decode("ascii").encode("utf8"))
+        default_charset = 'ASCII'
+        string = ''.join([unicode(t[0], t[1] or default_charset) for t in dh])
+        print type(string)
+        return string.encode('utf8')
+

+ 5 - 5
test.py

@@ -12,12 +12,12 @@ from core.TriggerLauncher import TriggerLauncher
 logging.basicConfig()
 logger = logging.getLogger("jarvis")
 
+order = "est-ce que j'ai des emails"
+
+oa = OrderAnalyser(order=order)
+
+oa.start()
 
-# oa = OrderAnalyser("wake up", brain_file="/home/nico/Documents/jarvis/test.yml")
-#
-# oa = OrderAnalyser("test", brain_file="brain_examples/fr/fr_systemdate.yml")
-#
-# oa.start()