Parcourir la source

gmail neuron ok

nico il y a 8 ans
Parent
commit
33246b6e2b
4 fichiers modifiés avec 29 ajouts et 11 suppressions
  1. 1 0
      brain.yml
  2. 18 10
      neurons/gmail_checker/gmail_checker.py
  3. 9 0
      templates/fr_gmail.j2
  4. 1 1
      test.py

+ 1 - 0
brain.yml

@@ -118,5 +118,6 @@
       - gmail_checker:
           username: "me@gmail.com"
           password: "my_password"
+          file_template: fr_gmail.j2
     signals:
       - order: "est-ce que j'ai des emails"

+ 18 - 10
neurons/gmail_checker/gmail_checker.py

@@ -1,4 +1,4 @@
-# coding: utf8
+# -*- coding: utf-8 -*-
 import logging
 
 from gmail import Gmail
@@ -52,17 +52,25 @@ class Gmail_checker(NeuronModule):
 
             returned_dict["subjects"] = subject_list
 
-        print returned_dict
+        logger.debug("gmail neuron returned dict: %s" % str(returned_dict))
         # logout of gmail
         g.logout()
+        self.say(returned_dict)
 
-    @staticmethod
-    def _parse_subject(encoded_subject):
+    def _parse_subject(self, 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')
 
+        return ''.join([self.try_parse(t[0], t[1]) for t in dh])
+
+    @staticmethod
+    def try_parse(header, encoding):
+
+        if encoding is None:
+            encoding = 'ASCII'
+        try:
+            return unicode(header, encoding)
+        except UnicodeDecodeError:
+            try:
+                return unicode(header, 'ISO-8859-1')
+            except UnicodeDecodeError:
+                return unicode(header, 'UTF-8')

+ 9 - 0
templates/fr_gmail.j2

@@ -0,0 +1,9 @@
+Vous avez {{ unread }} i-maile
+
+{% set count = 1 %}
+{% if unread > 0 %}
+    {% for subject in subjects %}
+     i-maile {{ count }}. {{ subject }}
+     {% set count = count + 1 %}
+    {% endfor %}
+{% endif %}

+ 1 - 1
test.py

@@ -1,4 +1,4 @@
-# coding=utf-8
+# -*- coding: utf-8 -*-
 import os
 
 from core.ConfigurationManager import SettingLoader