Skip to content

Instantly share code, notes, and snippets.

@myles
Created April 15, 2010 13:47
Show Gist options
  • Save myles/367104 to your computer and use it in GitHub Desktop.
Save myles/367104 to your computer and use it in GitHub Desktop.
Get the number of thread unread in Gmail.
#!/usr/bin/env python
# encoding: utf-8
# Copyright (c) 2009 Greg Newman
# Copyright (c) 2010 Myles Braithwaite
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import sys
import httplib2
from xml.dom.minidom import parseString
def gmail_inbox_count(email, password):
h = httplib2.Http()
h.add_credentials(email, password)
if email.endswith('@gmail.com'):
url = 'https://mail.google.com/mail/feed/atom'
else:
url = 'https://mail.google.com/a/%s/feed/atom' % email.split('@')[1]
resp, content = h.request(url)
dom = parseString(content)
fullcount = dom.getElementsByTagName('fullcount')[0].childNodes[0].data
return fullcount
def main(argv=None):
personal = gmail_inbox_count('example@gmail.com', 'example')
work = gmail_inbox_count('example@example.org', 'example')
print 'Personal: %s' % personal
print 'Work: %s' % work
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment