-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_gmail.py
executable file
·64 lines (47 loc) · 1.97 KB
/
full_gmail.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
#!/usr/bin/env python
import sys
from urllib.request import FancyURLopener
import xml.etree.ElementTree as ET
import argparse
parser = argparse.ArgumentParser(description = 'Read Gmail Account messages per RSS')
parser.add_argument('username', metavar='Username', action='store', help='GMail username (@gmail.com can be omitted)')
parser.add_argument('password', metavar='Password',action='store', help='GMail password or the device password')
parser.add_argument('--conky', dest='conky', action='store_true', help='conky-friendly output')
args = parser.parse_args()
email = args.username
password = args.password
ns = '{http://purl.org/atom/ns#}'
url = 'https://%s:%s@mail.google.com/mail/feed/atom' % (email, password)
opener = FancyURLopener()
page = opener.open(url)
contents = page.read().decode('utf-8')
#auth_handler = urllib.request.HTTPBasicAuthHandler()
#auth_handler.add_password(realm='https://mail.google.com/',
# uri='https://mail.google.com/',
# user=email,
# password=password)
#opener = urllib.request.build_opener(auth_handler)
#urllib.request.install_opener(opener)
ifrom = contents.index('<fullcount>') + 11
ito = contents.index('</fullcount>')
fullcount = contents[ifrom:ito]
root = ET.fromstring(contents)
if int(fullcount) == 1:
msg = '1 new message in ' + '${color #ff6600}' + email + '${color}\n'
else:
msg = fullcount + ' new messages in ' + '${color #ff6600}' + email + '${color}\n'
print(msg)
for entry in root.findall(ns + 'entry'):
title = entry.findtext(ns + 'title')[:50]
author = entry.find(ns + 'author')
author_name = author.findtext(ns + 'name')[:20]
author_email = author.findtext(ns + 'email')[:25]
if args.conky:
line = title
line += '${goto 350}'
line += author_name
line += '${goto 500}'
line += author_email
else:
line = '\'' + title + '\' from ' + author_name + ' (' + author_email + ')'
print(line)