/usr/lib/gmail-notify/gmailatom.py is in gmail-notify 1.6.1.1-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #! /usr/bin/python
# -*- coding: utf-8 -*-
# gmailatom 0.0.1
#
# HOW TO USE:
# 1) Create an instance of 'GmailAtom' class. The two arguments
# its constructor take are the username (including '@gmail.com')
# and the password.
# 2) To retrieve the account status call 'refreshInfo()'.
# 3) To get the unread messages count call 'getUnreadMsgCount()'.
# You MUST call 'refreshInfo()' at least one time before using
# this method or it will return zero.
# 4) To get specific information about an unread email you must
# call the corresponding getter method passing to it the number
# of the message. The number zero represents the newest one.
# You MUST call 'refreshInfo()' at least one time before using any
# getter method or they will return an empty string.
# The getter methods are:
# getMsgTitle(index)
# getMsgSummary(index)
# getMsgAuthorName(index)
# getMsgAuthorEmail(index)
#
# by Juan Grande
# juan.grande@gmail.com
from xml.sax.handler import ContentHandler
from xml import sax
import urllib2
# Auxiliar structure
class Mail:
title=""
summary=""
author_name=""
author_addr=""
# Sax XML Handler
class MailHandler(ContentHandler):
# Tags
TAG_FEED = "feed"
TAG_FULLCOUNT = "fullcount"
TAG_ENTRY = "entry"
TAG_TITLE = "title"
TAG_SUMMARY = "summary"
TAG_AUTHOR = "author"
TAG_NAME = "name"
TAG_EMAIL = "email"
# Path the information
PATH_FULLCOUNT = [ TAG_FEED, TAG_FULLCOUNT ]
PATH_TITLE = [ TAG_FEED, TAG_ENTRY, TAG_TITLE ]
PATH_SUMMARY = [ TAG_FEED, TAG_ENTRY, TAG_SUMMARY ]
PATH_AUTHOR_NAME = [ TAG_FEED, TAG_ENTRY, TAG_AUTHOR, TAG_NAME ]
PATH_AUTHOR_EMAIL = [ TAG_FEED, TAG_ENTRY, TAG_AUTHOR, TAG_EMAIL ]
def __init__(self):
self.startDocument()
def startDocument(self):
self.entries=list()
self.actual=list()
self.mail_count="0"
def startElement( self, name, attrs):
# update actual path
self.actual.append(name)
# add a new email to the list
if name=="entry":
m = Mail()
self.entries.append(m)
def endElement( self, name):
# update actual path
self.actual.pop()
def characters( self, content):
# New messages count
if (self.actual==self.PATH_FULLCOUNT):
self.mail_count = self.mail_count+content
# Message title
if (self.actual==self.PATH_TITLE):
temp_mail=self.entries.pop()
temp_mail.title=temp_mail.title+content
self.entries.append(temp_mail)
# Message summary
if (self.actual==self.PATH_SUMMARY):
temp_mail=self.entries.pop()
temp_mail.summary=temp_mail.summary+content
self.entries.append(temp_mail)
# Message author name
if (self.actual==self.PATH_AUTHOR_NAME):
temp_mail=self.entries.pop()
temp_mail.author_name=temp_mail.author_name+content
self.entries.append(temp_mail)
# Message author email
if (self.actual==self.PATH_AUTHOR_EMAIL):
temp_mail=self.entries.pop()
temp_mail.author_addr=temp_mail.author_addr+content
self.entries.append(temp_mail)
def getUnreadMsgCount(self):
return int(self.mail_count)
# The mail class
class GmailAtom:
realm = "New mail feed"
host = "https://mail.google.com"
url = host + "/mail/feed/atom"
def __init__(self, user, pswd, proxy=None):
self.m = MailHandler()
# initialize authorization handler
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password( self.realm, self.host, user, pswd)
# manage proxy
if proxy:
proxy_handler = urllib2.ProxyHandler({'http': proxy})
opener = urllib2.build_opener(proxy_handler, auth_handler)
else:
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
def sendRequest(self):
return urllib2.urlopen(self.url)
def refreshInfo(self):
# get the page and parse it
p = sax.parseString( self.sendRequest().read(), self.m)
def getUnreadMsgCount(self):
return self.m.getUnreadMsgCount()
def getMsgTitle(self, index):
return self.m.entries[index].title
def getMsgSummary(self, index):
return self.m.entries[index].summary
def getMsgAuthorName(self, index):
return self.m.entries[index].author_name
def getMsgAuthorEmail(self, index):
return self.m.entries[index].author_email
|