You are not logged in.

#1 2015-01-27 23:51:13

heyitstallchris
Member
Registered: 2013-10-01
Posts: 23

[SOLVED] Adapting conky "# of new emails" script to a message preview

The Conky wiki has a section to display the number of new emails (Gmail) with a python script. It appears that script uses urllib for credentials and then xml.etree to navigate the xml.

I'm trying to adapt the script to display the sender and title, since they exist in the xml.

I've been reading the documentation and after looking at the xml I'm pretty sure I need to use indexing, can't get it to work though.

Here's the script from the wiki with my comments on what I'm trying to add in.

#! /usr/bin/env python

import urllib.request
from xml.etree import ElementTree as etree

# Enter your username and password below within quotes below, in place of ****.
# Set up authentication for gmail
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='New mail feed',
                          uri='https://mail.google.com/',
                          user= '****',
                          passwd= '****')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)

gmail = 'https://mail.google.com/gmail/feed/atom'
NS = '{http://purl.org/atom/ns#}'
with urllib.request.urlopen(gmail) as source:
    tree = etree.parse(source)

fullcount = tree.find(NS + 'fullcount').text

# attempt to navigate to sender of email (feed/entry/author/name)
firstemailsender = tree[0][5][5][0].text
secondemailsender = tree[0][6][5][0].text 

# attempt to navigate to title of email (feed/entry/title)
firstemailtitle = tree[0][5][0].text
firstemailtitle = tree[0][5][0].text

print(fullcount + ' new')

# added print that doesn't work because of other things not working
print (firstemailsender + ' - ' + firstemailtitle)
print (secondemailsender + ' - ' + secondemailtitle)

 

Last edited by heyitstallchris (2015-02-06 14:55:07)

Offline

#2 2015-01-28 12:50:26

spupy
Member
Registered: 2009-08-12
Posts: 218

Re: [SOLVED] Adapting conky "# of new emails" script to a message preview

Hey,

I've re-written the code to use XPath instead of indexing, IMO it's more robust and compact. findall() returns a list of all Elements that match the given XPath. Make sure to include the namespace before each element name in the path (you can see that below when I use findall() to get all "author/name" elements.
The code saves all new mails in an array (mails) full of dicts, each with a 'sender' and 'title' keys. The script will blow up if you have zero or one new emails, but you can trivially check the length of the mails array before printing at the end.
If you have any questions about the code just ask.

#! /usr/bin/env python

import urllib.request
from xml.etree import ElementTree as etree

# Enter your username and password below within quotes below, in place of ****.
# Set up authentication for gmail
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='New mail feed',
                          uri='https://mail.google.com/',
                          user= '*****',
                          passwd= '*****')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)

gmail = 'https://mail.google.com/gmail/feed/atom'
NS = '{http://purl.org/atom/ns#}'
with urllib.request.urlopen(gmail) as source:
    tree = etree.parse(source)
    root = tree.getroot()

mails = []
# only iterate over new mails, which have a tag "entry"
for entry in root.findall(NS+'entry'):
	message = {}
	# locate sender and title info using XPath (?) again
	message['sender'] = entry.findall(NS+'author/'+NS+'name')[0].text
	message['title'] = entry.findall(NS+'title')[0].text
	mails.append(message)

print(str(len(mails)) + ' new')

print (mails[0]['sender'] + ' - ' + mails[0]['title'])
print (mails[1]['sender'] + ' - ' + mails[1]['title'])

Last edited by spupy (2015-01-28 14:00:56)


There are two types of people in this world - those who can count to 10 by using their fingers, and those who can count to 1023.

Offline

#3 2015-02-06 14:54:10

heyitstallchris
Member
Registered: 2013-10-01
Posts: 23

Re: [SOLVED] Adapting conky "# of new emails" script to a message preview

This works great! I added the bit about the if else to check for length as mentioned to replace the bottom part

if len(mails) == 0:
	print('email@gmail.com')
	print('No new emails to display')
elif len(mails) == 1:
	print(str(len(mails)) + ' new' + ' - email@gmail.com')
	print (mails[0]['sender'] + ' - ' + mails[0]['title'])
elif len(mails) == 2:
	print(str(len(mails)) + ' new' + ' - email@gmail.com')
	print (mails[0]['sender'] + ' - ' + mails[0]['title'])
	print (mails[1]['sender'] + ' - ' + mails[1]['title'])
elif len(mails) == 3:
	print(str(len(mails)) + ' new' + ' - email@gmail.com')
	print (mails[0]['sender'] + ' - ' + mails[0]['title'])
	print (mails[1]['sender'] + ' - ' + mails[1]['title'])
	print (mails[2]['sender'] + ' - ' + mails[2]['title'])
elif len(mails) == 4:
	print(str(len(mails)) + ' new' + ' - email@gmail.com')
	print (mails[0]['sender'] + ' - ' + mails[0]['title'])
	print (mails[1]['sender'] + ' - ' + mails[1]['title'])
	print (mails[2]['sender'] + ' - ' + mails[2]['title'])
	print (mails[3]['sender'] + ' - ' + mails[3]['title'])
else:
	print(str(len(mails)) + ' new' + ' - email@gmail.com')
	print (mails[0]['sender'] + ' - ' + mails[0]['title'])
	print (mails[1]['sender'] + ' - ' + mails[1]['title'])
	print (mails[2]['sender'] + ' - ' + mails[2]['title'])
	print (mails[3]['sender'] + ' - ' + mails[3]['title'])
	print (mails[4]['sender'] + ' - ' + mails[4]['title'])

Offline

#4 2015-02-06 21:19:50

whitie
Member
Registered: 2011-03-13
Posts: 23

Re: [SOLVED] Adapting conky "# of new emails" script to a message preview

Or even a little bit shorter wink

if len(mails):
    print(len(mails), 'new - email@gmail.com')
    for mail in mails:
        print('{sender} - {title}'.format(**mail))
else:
    print('email@gmail.com')
    print('No new emails to display')

Whitie

Offline

#5 2015-02-06 21:44:26

heyitstallchris
Member
Registered: 2013-10-01
Posts: 23

Re: [SOLVED] Adapting conky "# of new emails" script to a message preview

That works also, I originally wanted to view only 5 new emails but why not view em all, I like it.

Offline

Board footer

Powered by FluxBB