Skip to content

Example: Check Gmail

Arvydas Juskevicius edited this page Aug 1, 2013 · 1 revision

This example shows how to blink BlinkStick when there is new mail in GMail account.

Change username and password variables below to your account details. When you run this script and there is new mail in your inbox, BlinkStick will blink once in red color.

import urllib             # For BasicHTTPAuthentication
import feedparser         # For parsing the feed
from blinkstick import blinkstick

username = "username"
password = 'password'

#No need to edit anything below this line

_URL = "https://mail.google.com/gmail/feed/atom"

bstick = blinkstick.find_first()

#This overriden class will automatically supply username and password for atom URL
class my_opener (urllib.FancyURLopener):
    # Override
    def get_user_passwd(self, host, realm, clear_cache=0):
        return (username, password)

def auth():
    '''The method to do HTTPBasicAuthentication'''
    opener = my_opener()
    f = opener.open(_URL)
    feed = f.read()
    return feed

def readmail(feed):
    '''Parse the Atom feed and print a summary'''
    atom = feedparser.parse(feed)
    print ""
    print atom.feed.title
    print "You have %s new mails" % len(atom.entries)

    if bstick is not None and len(atom.entries) > 0:
        bstick.blink(name="red")

if __name__ == "__main__":
    f = auth()  # Do auth and then get the feed
    readmail(f) # Let the feed be chewed by feedparser