Don't like ads? PRO users don't see any ads ;-)

gmail_pynotifier

By: crabman on Jul 9th, 2012  |  syntax: Python  |  size: 2.41 KB  |  hits: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # encoding: UTF-8
  3.  
  4. #### notification gmail
  5. #### Crabman juillet 2012
  6. #### minimal script pour conky par exemple.
  7. #### python 2.
  8.  
  9.  
  10. import os,sys
  11. import urllib2
  12. import base64
  13. import ConfigParser
  14. from xml.dom.minidom import parse
  15.  
  16. home = os.environ.get("HOME")
  17. fichier_ini = "%s/.mailnotifier/mailnotifier.ini" % (home)
  18.  
  19. ### syntaxe du fichier "/home/user/.mailnotifier/mailnotifier.ini"
  20. """
  21.  
  22. [gmail]
  23. USER:addresse@gmail.com
  24. PASS:mot_de_passe
  25.  
  26.  
  27. """
  28.  
  29. try:
  30.         config = ConfigParser.ConfigParser()
  31.         config.read(fichier_ini)
  32.         section = dict(config.items("gmail"))
  33.         user = section["user"]
  34.         password = section["pass"]
  35. except:
  36.         print ("Impossible d'accèder au fichier qui contient les infos de connection\nle fichier: %s" % (fichier_ini))
  37.         sys.exit(1)
  38.  
  39. def gmail_unread_count(user, password):
  40.         try:
  41.                 # Build the authentication string
  42.                 b64auth = base64.encodestring("%s:%s" % (user, password))
  43.                 auth = "Basic " + b64auth
  44.                 req = urllib2.Request("https://mail.google.com/mail/feed/atom/")
  45.                 req.add_header("Authorization", auth)
  46.                 handle = urllib2.urlopen(req)
  47.         except:
  48.                 return ("impossible de ce connecter au compte")
  49.         try:
  50.                 dom = parse(handle)
  51.                 handle.close()
  52.                 # Get the "fullcount" xml object
  53.                 count_obj = dom.getElementsByTagName("fullcount")[0]
  54.                 # get its text and convert it to an integer
  55.                 nb = int(count_obj.firstChild.wholeText)
  56.         except:
  57.                 return ("impossible de parser le résultat")
  58.  
  59.         ##commenter si décommente pour afficher les info
  60.         return (nb)
  61.  
  62. #               #### pour afficher les info sur les messages#####
  63. #               #################################################
  64. #       if nb == 0:
  65. #               return ("Vous n'avez pas de messages")
  66. #       elif nb > 0:
  67. #               try:
  68. #                       entry = dom.getElementsByTagName("entry")      
  69. #               except:
  70. #                       return ("problème pour identifier les informations.")
  71. #
  72. #               info = ("Vous avez %d nouveaux messages" % (nb))
  73. #               i = 1
  74. #               for message in entry:
  75. #                       nom = message.getElementsByTagName("name")[0].firstChild.wholeText
  76. #                       mail =  message.getElementsByTagName("email")[0].firstChild.wholeText
  77. #                       title = message.getElementsByTagName("title")[0].firstChild.wholeText
  78. #                       letout = ("message de %s <%s>\ntitre \"%s\"" %(nom, mail, title))
  79. #                       info += '\n\n' + letout
  80. #                       i += 1
  81. #                       if i > 4:
  82. #                               break
  83. #               return (info)
  84. #               ###############################################
  85. #               ###############################################
  86.  
  87. print(gmail_unread_count(user, password))