[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)

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)

Similar Messages

  • I am having serious issues trying to alter my old iCloud account with an old non-functioning email to my new apple id with a new email address. The message keeps coming up on all my apple products with my old email (which is not available anymore).

    I am having serious issues trying to alter my old iCloud account with an old non-functioning email to my new apple id with a new email address. The message keeps coming up on all my apple products with my old email (which is not available anymore).

    I have the same problem - it is maddening. I rely on this iPad for work so this is not just an annoyance! The above solutions of changing the appleid on the device or on the website do not work.
    The old email address no longer exists - I haven't used it in a year probably and I no longer have the account.  I logged into the appleid website and there is no trace of the old email address so there is nothing that can be deleted or changed there.  On the iPad there is no trace of the old email address so nothing can be deleted there either. I have updated the iPad software and the same problem comes right back.  Every 2 seconds I am asked to log in using the old non-existent email.  The device is currently useless.
    The only recent change to anything was the addition of an Apple TV device, which was set up using the correct login and password.
    Does anyone have any ideas? The iPad has been backed up to the iCloud so presumably it now won't recognize the current iCloud account? So restoring may notbe an option?

  • Error when composing new email to send "This message cannot be saved" -?

    Just upgraded to Tiger 10.4.6 and finally got my email working except for some reason every time I compose a new email, I get an error pop up stating "this message cannot be saved" -what is it talking about???

    Yes, if this is an AOL account, that was the problem.
    AOL doesn’t let mail clients create mailboxes at the root level of the account directory on the server. If you have Store draft messages on the server enabled, Mail tries to save the draft on the server, but fails to do so because AOL doesn’t let Mail create the Drafts mailbox there.
    Actually, you should turn OFF Store messages on the server for all the mailboxes in Preferences > Accounts > Mailbox Behaviors, so that those mailboxes are stored locally instead.

  • My emails from the last five years have disappeared and trying to receive new emails generates an error message

    My antivirus scan secured a FakeAlert threat. Now, all emails from the past five years are no longer visible in my Inbox and are not in the Deleted Items folder. Any time I try to receive a new email I receive the following message:
    "There was an error downloading the following message:
    From: <sender>
    Subject: <subject>
    This message may contain a virus or there is not enough disk space. Skip this message?"
    Enter No:
    "Unable to write the email to the mailbox. Make sure the file system allows you write privileges, and you have enough disk space to copy the mailbox."
    I have write privileges and 184 GB of free disk space. I recently deleted many emails and compacted my inbox. I cannot restore any files that the scan deleted because there are none visible.
    I have contacted my antivirus software providers and they concluded that the problem lies with my email software. Can you please help?

    REmove all emails from the Inbox to other folders.
    When it is empty do the next stage.
    If you are using a POP mail account and have some emails in the webmail view that are missing in Thunderbird, you could get everything currently on the server downloaded again.
    Make hidden files and folders visible:
    * http://kb.mozillazine.org/Show_hidden_files_and_folders
    In Thunderbird
    * Help > Troubleshooting Information
    * click on 'Show Folder' button
    * '''close Thunderbird now - this is important. '''
    * clickon 'Mail' folder
    * click on mail account name
    * Locate and delete these files: '''Inbox and Inbox.msf'''
    * locate and delete this file: '''popstate.dat'''
    * close the window - top right X
    * restart Thunderbird
    A new Inbox will be created.
    A new popstate.dat file will be created.
    It keeps a record of everything downloaded, as it is new and empty, TB will believe everything currently in server Inbox needs to be downloaded.
    This will get everything currently in server Inbox back in Thunderbird. As you already have some emails, you may get some duplicates, but only the once.
    I would not expect any emails are recoverable from the Inbox in Thunderbird because they may have been corrupted by either an unknown reason or by the antivirus program doing something with the file to make emails lose information, it is possible that when you compacted the file emails were lost. Compacting removes 'marked as deleted' emails, but if it didi not know where a marked as deleted email ended, good emails could have been removed due to the corruption.
    In future, compacting should be done on a regular basis, but never compact a file/folder that you think may be corrupted. Getting the error message saying the file may be corrupted was a good hint that somethig was wrong.
    Report back on results.

  • Checking for new email deletes all previous messages from iphone 3g

    After receiving any email, the device is removing all email from all local folders. The remove setting is set to 'never', 'ask before deleting' is set to 'on' and it is set to show 75 messages. There is nothing in the trash, sent, or main inbox.
    I can see a test email for about 5 minutes. if I resynch or turn the device off, and and then check for new email, the old email is removed. No trace of it anywhere.
    It didn't do this originally. It just started in the last few days.
    Thanks.

    I suggest deleting and recreating the account.

  • As of yesterday 3/1/2015 T'bird stopped reading mail rationally on ONE of my two Windows 7 PC's and when aksed to check for new email retrieves the SAME message

    no matter how many times I ask T'bird to check for new mail I get back ANOTHER copy of the same message dated 7:28am and NO MAIL received after that reaches my INBOX. On my other PC running Windows 7 only ONE copy of the 7:28am transmission arrives as does all subsequent email sent to me at this address... both PC's are protected by Avast anti-virus software. Turning the rogue PC off and on did not help nor did uploading and installing an updated version of T'bird.

    Are the setup the same? same accounts, same folders, same messages, same filters same add-ons?
    If that the case make a copy of the good profile and replace the bad one with that.

  • New email page covers the message I am replying to!

    When I want to  Reply to a message in my email box, the new page which comes up for the reply completely covers the message I want to reply to. I cannot see it or find a way to move it.  I use Sky Yahoo email and this is very frustrating. I have to print out the message I am replying to so that I do not miss any points. Surely there must be a way of moving the incoming message to the lower half of the screen so that one can see it when using the top half of the screen to reply to. I am on iMac and have this problem on IOS Mavericks and it still exists now that I have downloaded Yosemite. Can someone help please?

    Never crossed my mind Eric! Most embarrassing but thank you for pointing out the solution - although shifting the window sizes around takes some figuring out. Is there a quick and easy way to half-screen incoming message and reply?

  • Try to create new email account, receive this message.

    Logging in to the fuse.net POP server “pop.fuse.net” failed. Make sure the user name and password you entered are correct, then click Continue. If the information isn’t correct, you cannot receive messages.
    have one fuse email account already that works correctly.

    Try deleting the account and adding it back. Also check Connection Doctor under the Mail/Window menu to see what the connection status is.
    Apple support articles.
    Mail Problems
    Mail

  • Need help adapting David Powers PHP email form script please!

    Hi all,
    I'm fairly inexperienced with PHP and I'm trying to adapt the David Powers email form script from THE ESSENTIAL GUIDE TO DREAMWEAVER CS4 WITH CSS, AJAX, AND PHP.
    I've created a basic form so that visitors to the site can request a telephone call back from the site owner. The form asks the visitor for their name, telephone number and to select a time of day suitable for the telephone call to be made using radio buttons selecting between morning and afternoon.
    I'd like to achieve my goal with minimal validation error messages and would like to redirect to another page when a message is sent successfully. It is also important that in the spirit of the David Powers script I'm trying to work with, that it filters out suspect input, avoids email header injection attacks and blocks submission by spam bots.
    There may be a really simple solution to this since I don't want users to be able to enter an email address at all but I don't know enough to be able to figure it out just yet.
    I'd be grateful for any advice.
    See below for the code for the form including PHP so far...
    Thanks to everyone looking in in advance
    Karl.

    GEAtkins wrote:
    > I am using the redirect to a personal page from page 515
    of The Essential
    > Guide to DWCS3 in the following form:
    $_SESSION[MM_Username].php in the "if
    > login succeeds" field.
    Thank you for reminding me. There's a mistake in the book,
    which I
    discovered over the Christmas period, so forgot to send to
    friends of ED
    for the errata page.
    Don't use $_SESSION[MM_Username]. Use $loginUsername instead.
    It then works.
    David Powers, Adobe Community Expert
    Author, "The Essential Guide to Dreamweaver CS3" (friends of
    ED)
    Author, "PHP Solutions" (friends of ED)
    http://foundationphp.com/

  • Offlineimap "New Email" notification scripts

    I recently started using offlineimap/mutt to keep track of three email accounts. I wanted notifications when new email arrived so I tried some notification programs that worked with Maildir but didn't find any that I liked. So I came up with this script:
    #!/bin/sh
    MAILDIR1=$HOME/Email/maildir1/INBOX/new
    MAILDIR2=$HOME/Email/maildir2/INBOX/new
    MAILDIR3=$HOME/Email/maildir3/Inbox/new
    echo "Watching $MAILDIR1"
    while MAILNAME1=$(inotifywait -qe MOVED_TO --format '%f' $MAILDIR1)
    do
    echo $MAILNAME1
    mailpop $MAILDIR1/$MAILNAME1
    done&
    echo "Watching $MAILDIR2"
    while MAILNAME2=$(inotifywait -qe MOVED_TO --format '%f' $MAILDIR2)
    do
    echo $MAILNAME2
    mailpop $MAILDIR2/$MAILNAME2
    done&
    echo "Watching $MAILDIR3"
    while MAILNAME3=$(inotifywait -qe MOVED_TO --format '%f' $MAILDIR3)
    do
    echo $MAILNAME3
    mailpop $MAILDIR3/$MAILNAME3
    done
    I modified a script for procmail notifications I found here http://miek.nl/posts/2008/Jun/09/a-popu … rocmailrc/ for nicely formatted notifications and added two lines to speak the notification as well.
    mailpop
    #!/bin/bash
    # popup a small notification with 'notify-send'
    from=`formail -X From: < $1`
    sub=`formail -X Subject: < $1`
    # tweaks < > are special
    from=${from//</\(}
    from=${from//>/\)}
    from=${from//&/\.}
    sub=${sub//</\(}
    sub=${sub//>/\)}
    sub=${sub//&/\.}
    sub=${sub:0:200}
    from=${from:0:75}
    TM=2000
    /usr/bin/notify-send -u normal -t $TM "$from" "$sub"
    while pgrep speech2 > /dev/null; do sleep 1; done
    speech2 New Email ${from%(*}, With $sub, has arrived
    #echo "New Email $from, With $sub has arrived" | festival --tts
    At first I used festival for speech, but Google's voice sounded so much better. This script will speak up to 100 characters, just requires mplayer:
    speech2
    #!/bin/bash
    say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?tl=en&q=$*"; }
    say $*
    For messages with longer subjects: http://danfountain.com/2013/03/raspberr … to-speech/
    Anyone else have a script/program they like for maildir notifications? Also I would be interested in making these work as a systemd user service.

    For any other i3 users, jasonwryan script works great with i3status and i3bar useing the example from i3status manual:
    i3status | while :
    do
    read line
    echo "Email: $(eml) | $line" || exit 1
    done
    Edit: This is better, works with colors:
    i3status | (read line && echo $line && read line && echo $line && while :
    do
    read line
    dat=$(eml)
    dat="[{ \"full_text\": \"Email: ${dat}\" },"
    echo "${line/[/$dat}" || exit 1
    done)
    Last edited by kristopher004 (2015-03-01 18:19:00)

  • When i move my macbook from home to office, i cannot get new emails. how to solve?

    hi, i set up the mail at home, now move the mac to office, i cannot get any new emails now. anyone know why and how to do?

    Connection Doctor
    <http://support.apple.com/kb/TS3276>

  • [SOLVED] Thunderbird Firetray disappears upon new email

    On my previous system (arch), when I got a new email, the firetray extension for Thunderbird will show a yellowish letter picture and show the number of new messages. But in my new arch system, everytime I got a new email, it'll just disappear. Then I'd have to open up thunderbird again, then it would appear. It's other functions all work as per-normal, so I'm thinking it's some problem with the letter picture.
    I've checked the preferences, and they are all unchanged. I've even re-installed it just to be sure. I seems to me that there's a certain notification dependency that's missing. And I probably installed it in my last arch (by accident), and this time I haven't.
    Last edited by zephyrus17 (2009-01-02 10:14:43)

    I'm not sure it this the the right one for firetray, but it looks like it.
    (gecko:30450): Gdk-WARNING **: gdkpixbuf-drawable.c:1247: Source drawable has no colormap; either pass in a colormap, or set the colormap on the drawable with gdk_drawable_set_colormap()
    (gecko:30450): Gdk-WARNING **: The gdk_draw_*_image require the drawable argument to
    have a specified colormap. All windows have a colormap,
    however, pixmaps only have colormap by default if they
    were created with a non-NULL window argument. Otherwise
    a colormap must be set on them with gdk_drawable_set_colormap
    (gecko:30450): Gdk-WARNING **: Using Cairo rendering requires the drawable argument to
    have a specified colormap. All windows have a colormap,
    however, pixmaps only have colormap by default if they
    were created with a non-NULL window argument. Otherwise
    a colormap must be set on them with gdk_drawable_set_colormap
    (gecko:30450): Gdk-WARNING **: gdkpixbuf-drawable.c:1247: Source drawable has no colormap; either pass in a colormap, or set the colormap on the drawable with gdk_drawable_set_colormap()
    Seems like something to do with cairo. but I have cairo installed. should I have installed cairo-perl or something?

  • How can I integrade calendar to new email account?

    How can I integrate my calendar with my new email account, the calendar only work with Microsoft account or Gmail account and Facebook calendar and doesn't work with any other account.
    Moderator's Note: We have changed the title into a subject-related title. This is to keep the forum organized and let other forum users easily see and respond to this post.

    Calendars can be synced with either vCal or EAS accounts such as Google or Microsoft accounts. You can't link or sync a calendar with a 'regular' POP or IMAP mail account.
    Click on the blue Star Icon below if my advice has helped you or press the 'Accept As Solution' link if I solved your problem..

  • Unable to reset or add new email addresses on HP Officejet Pro 8600 Premium e-All-in-one N911n

    Unable to reset or add new email addresses on my HP Office jet J4680

    Hi johndavis196,
    Welcome to the HP Support forums.  I see that you are trying to add new email addresses to your Officejet J4680 printer.
    To better assist you, would you please provide some additional information:
    How is the printer connected (USB cable, ethernet, or wireless)? The Officejet J4680 supports all three methods.
    What operating system are you using on your computer (Windows 7, Mac 10.8, or Windows 8, etc)?
    Where are you trying to update the email addresses?
    Are you encountering any error messages? If yes, what is the error message and where is it displayed (printer or computer)?
    What steps are you taking before you are stopped?
    Thank you.
    Regards,
    Happytohelp01
    Please click on the Thumbs Up on the right to say “Thanks” for helping!
    Please click “Accept as Solution ” on the post that solves your issue to help others find the solution.
    I work on behalf of HP

  • (new to scripting) Only allow certain paragraph styles to break to next column/page

    Hi all,
    As stated, I'm very new to scripting. I've done a few small scripts to automate Photoshop tasks and such. I'm in the process of trying to learn Javascript as much as possible, with the hope of bringing about more efficiency for our small production department. It's a daunting task, but I'm trying my darnedest.
    One of the number one goals I have is the find a way to automate pagination of weekly transaction records. These records run between 32 to 48 pages normally, with each page having 5 columns. Creation of these pages is very systematic, so it would seem that this process should be able to be automated, but there are some challenges I'm having a hard time thinking about how to attack.
    One of the key tasks needing to be figured out is getting the records to go in with headings and subheadings always starting at the top of a page/column. Just about all the copy comes in with predetermined paragraph styles, so a script should easily be able to search for instances of each style. What needs to eb accomplished is only allowing the flow of copy to break on certain styles and not others ... (counties, towns, etc.)
    For example: if you have the following  ...
    Countyland <styled "county">
    Utopiatown <styled "town">
    46 Appletree Lane ...... $500,000  <styled "listing">
    Sold by: Some Guy   <styled "listing">
    Bought by: Other Guy  <styled "listing">
    Mortgage: This Bank  <styled "listing">
    Date: 12/14/11  <styled "listing">
    Dystopia Village <styled "town">
    47 Orangeplant Road ...... $400,000  <styled "listing">
    Sold by: Some Guy   <styled "listing">
    Bought by: Other Guy  <styled "listing">
    Mortgage: This Bank  <styled "listing">
    Date: 12/14/11  <styled "listing">
    If the two records above were to fall near the end of a column, the break to the next column should NOT occur anywhere except:
    - Before the county (causing the county to start at the top of the next column
    - Before the second town (Dystopia Village, forcing that town to the top of the next column)
    - After both (forcing whatever town/county is next to goto the top of the next column)
    So basically, I need to find a way PREVENT column breaks where they should not occur. Inserting column breaks is one thing (I know how to do that). Prevention is the issue though that confounds me.
    Does anyone have any thoughts of how to achieve such a thing? I know there are "non-breaking space" characters ... is there, or could there be, some kind of a "non-breaking return" character? Or would it be more feasible to test for the paragraph style at the bottoms of columns and insert column breaks in once it finds an appropriate match?
    Am I going about this all wrong?
    Again, any help or suggestions are more than welcome.
    Thanks much.
    ~Nate
    FYI .. using InDesign cs5.5

    I am now wondering about a possible solution. It would be one hell of a challenge for me to figure out how to code it, but I at least need to figure out the theory first, then I'll try to figure out how to code it.
    Again, the issue at hand is preventing column breaks for the "listing" style in the following example. Please note, the number of "listing" lines varies. If the following two entries (in a long list of similar entries) were to be near the end of a column, I'm trying to cause the flow of text to break only before a county style, or a town style (if it's not the first town in the county).  Basically, no breaks in the middle of listings.
    Countyland <styled "county">
    Utopiatown <styled "town">
    46 Appletree Lane ...... $500,000  <styled "listing">
    Sold by: Some Guy   <styled "listing">
    Bought by: Other Guy  <styled "listing">
    Mortgage: This Bank  <styled "listing">
    Date: 12/14/11  <styled "listing">
    Dystopia Village <styled "town">
    47 Orangeplant Road ...... $400,000  <styled "listing">
    Sold by: Some Guy   <styled "listing">
    Bought by: Other Guy  <styled "listing">
    Mortgage: This Bank  <styled "listing">
    Date: 12/14/11  <styled "listing">
    Naomi previously suggested messing with the "keep options" in the styles. This will solve the issue of preventing breaks before listing details start. But it won't help in keeping listing details from breaking, since the number of lines can vary.
    I'm now thinking ... might it be possible to have a script search all copy, looking for the last line of listings, and then apply a second listing style with a different keep option setting? The "normal listing" style would "keep next 1 line". The "last listing" style would not, therefore, allowing a break.
    Here's a map of what I'm thinking: (bear with me on the terrible syntax, this is just for illustrative purposes)
    listingNormal has keep with next 1 line applied
    listingBreak does not keep with next line
    if currentLineStyle == listingNormal
    AND
    style of next line != listingNormal {
    currentLineStyle = listingBreak
    Does this make sense? If so, does this sound feasible?
    Thanks any and all.
    ~Nate

Maybe you are looking for

  • Can I find out the full filename of input file in message mapping?

    I have in XI 2.0 the following scenario : In the inbound fileadapter I read my input file. The filename of the input file is part fixed, part variable (Like INDATA01.txt, INDATA03.txt, etc). So in my Adapterconfiguration, I specify the filename with

  • Creating variables in BO using BO SDK

    Hi, We can create variables which can be formulas applied on objects using infoview. Is there any way, these variables can be created using java BO SDK? Please paste some sample code for us. Cheers, Shruti

  • Why does Satellite L300-10Q not recognise a wireless lan

    This new laptop computer is working ok on a wired LAN but does not recognise a Wireless Lan. Got message that no networks can be found. In the same room with another Toshiba Laptop the wireless LAN is found and has a good signal strength. The switch

  • I have a question about iMovie?

    I have a quick question about Imovie and I hope someone can please please please give me an answer, I want to hear lol. Is there any way i can get more transitions or titles on Imovie? or would I need to invest in photoshop or something that would gi

  • Connecting AEBS to third party modem/router

    I'm not having any luck setting up my AEBS as remote basestation to connect wirelessly to my Zyxel wireless modem/router. I have the AEBS wired to my quad G5. In Airport Admin Utility of the internet settings Airport WDS is selected. At the WDS host