檢查 GMail 的未讀信件
gmail.py
import urllib2 import base64 from xml.dom.minidom import parse def gmail_unread_count(user, password): """ Takes a Gmail user name and password and returns the unread messages count as an integer. """ # Build the authentication string b64auth = base64.encodestring("%s:%s" % (user, password)) auth = "Basic " + b64auth # Build the request req = urllib2.Request("https://mail.google.com/mail/feed/atom/") req.add_header("Authorization", auth) handle = urllib2.urlopen(req) # Build an XML dom tree of the feed dom = parse(handle) handle.close() # Get the "fullcount" xml object count_obj = dom.getElementsByTagName("fullcount")[0] # get its text and convert it to an integer return int(count_obj.firstChild.wholeText) unreadMail = gmail_unread_count("gmail_user", "gmail_pass") print unreadMail
TIPs:
gmail_user, gmail_pass gmail 帳號密碼
注意: 如果 Google 帳戶有啟用 2-steps authentication,上述的密碼必須使用 Google APP 專用密碼
Images 0 | ||
---|---|---|
No images to display in the gallery. |