-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail_parser.py
executable file
·82 lines (64 loc) · 2.57 KB
/
gmail_parser.py
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
## check-gmail.py -- A command line util to check GMail -*- Python -*-
## modified to display mailbox summary for conky
# ======================================================================
# Copyright (C) 2006 Baishampayan Ghose <[email protected]>
# Modified 2008 Hunter Loftis <[email protected]>
# Time-stamp: Mon Jul 31, 2006 20:45+0530
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# ======================================================================
import sys
import os.path
import datetime
import urllib # For BasicHTTPAuthentication
import feedparser # For parsing the feed
_URL = "https://mail.google.com/gmail/feed/atom"
maxlen = 1
def auth():
'''The method to do HTTPBasicAuthentication'''
if len(sys.argv) < 3:
print "Usage: python " + sys.argv[0] + " username password"
exit()
uname = sys.argv[1]
password = sys.argv[2]
urllib.FancyURLopener.prompt_user_passwd = lambda self, host, realm: (uname, password)
opener = urllib.FancyURLopener()
f = opener.open(_URL)
feed = f.read()
return feed
def readmail(feed, maxlen):
'''Parse the Atom feed and print a summary'''
atom = feedparser.parse(feed)
yesterday = refresh_count(atom.feed.fullcount)
print "${color1} %s[%s] unread email(s)" % (atom.feed.fullcount, yesterday)
# for i in range(min(len(atom.entries), maxlen)):
# print ' ${color2}%s' % atom.entries[i].title.encode("utf-")
#uncomment the following line if you want to show the name of the sender
# print ' ${color2}%s' % atom.entries[i].author
# if len(atom.entries) > maxlen:
# print ' ${color}more...'
def refresh_count(newcount = None):
path = os.path.abspath(os.path.dirname(sys.argv[0]))
filename = path + "/gmail_parser.log"
today = datetime.date.today()
new_line = str(today) + " " + str(newcount)
if not os.path.exists(filename):
f = open(filename, "w")
f.writelines(new_line)
f.close()
return
f = open(filename, "r+")
[d, c] = f.read(128).split(" ")
f.close()
date = datetime.datetime.strptime(d, "%Y-%m-%d").date()
count = int(c)
trend = int(newcount) - count
if (today - date) > datetime.timedelta(days = 1):
f = open(filename, "w")
f.write(new_line)
f.close()
return trend
if __name__ == "__main__":
f = auth() # Do auth and then get the feed
readmail(f, int(maxlen)) # Let the feed be chewed by feedparser