conky - Gmail new email information (perl version 1)

hakerdefo

After fiddling with snakes (read Python) & gem hunting (read Ruby), finally I have found the pearl of wisdom to wash away the conky blues  :D

First install these script dependencies,


sudo apt-get install perl
sudo apt-get install libxml-simple-perl
sudo apt-get install libwww-perl


Next copy-paste following code in your text-editor,


#!/usr/bin/perl
use strict;
use XML::Simple;
use LWP::Simple;
my $u = $ARGV[0];
my $p = $ARGV[1];
my $xmldata = get("https://$u:$p\@mail.google.com/mail/feed/atom");
my $xml = new XML::Simple;
my $data = $xml->XMLin($xmldata);
my @arr = reverse sort keys %{$data->{'entry'}};
if (@arr != 0 ) {
print "[$u] :: New mails ". @arr ."\n";
my $n = 3;
$n = @arr if @arr < 3;
for(my $i = 0; $i < $n; $i++) {
    my $title = substr $data->{'entry'}->{$arr[$i]}->{'title'}, 0, 45;
    my $frm = substr $data->{'entry'}->{$arr[$i]}->{'author'}->{'name'}, 0, 30;
    my $dt = substr  $data->{'entry'}->{$arr[$i]}->{'modified'}, 0, 10;
    print "\n";
    print "From: $frm, Date: $dt\n";
    print "$title\n";
}
    print "\n";
} else {
    print "No new mail..\n";
}
exit 0;



Now by default this script will show the last three (3) unread mails. If you want to change this number, edit the following line in the script (line number 13),


my $n = 3;


Save the script with the name 'check-gmail.pl' somewhere in your $PATH and give it execute permissions,


chmod 755 /path/to/check-gmail.pl


Next open your .conkyrc in text-editor and add following line to call 'check-gmail.pl' via conky,


execi 600 perl /path/to/check-gmail.pl "your_gmail_username" "your_gmail_password"


Of-course you will need to replace "your_gmail_username" and "your_gmail_password" above with your Google username and password.

Tested-Okay & Satisfaction-Guaranteed 8)

In a bit of a hurry at the moment so no scrot as a proof-of-the-pudding  :D

Cheers!!!
You Can't Always Git What You Want

VastOne

Seems to function all right but is only seeing and saying No New Mail even though I have unread email...

VSIDO      VSIDO Change Blog    

    I dev VSIDO

VastOne

But when I have multiple unread emails it seems to work fine...

Will keep on testing
VSIDO      VSIDO Change Blog    

    I dev VSIDO

VastOne

Testing results and script modifications

Scrot showing 6 Unread emails and setup to show 4 in the script:  Works perfectly


 


Scrot showing 2 Unread emails and setup to show 4 in the script:  Works perfectly


 


Scrot with only one email unread. Consistent with same number 7 all the time: Not working perfectly ...


 


Scrot showing conky when there are no emails: Works perfectly


 


VSIDO      VSIDO Change Blog    

    I dev VSIDO

VastOne

#4
I have also modified the script to show less detail and format the way I like to see it in Conky

Here is the modified version of

check-gmail.pl (must be executable)

#!/usr/bin/perl
use strict;
use XML::Simple;
use LWP::Simple;
use Term::ANSIColor qw( colored );
my $u = $ARGV[0];
my $p = $ARGV[1];
my $xmldata = get("https://$u:$p\@mail.google.com/mail/feed/atom");
my $xml = new XML::Simple;
my $data = $xml->XMLin($xmldata);
my @arr = reverse sort keys %{$data->{'entry'}};
if (@arr != 0 ) {
#print "New Email(s) ". @arr ."\n";
print "". @arr ." New Email(s)\n";
my $n = 4;
$n = @arr if @arr < 4;
for(my $i = 0; $i < $n; $i++) {
    my $title = substr $data->{'entry'}->{$arr[$i]}->{'title'}, 0, 45;
    my $frm = substr $data->{'entry'}->{$arr[$i]}->{'author'}->{'name'}, 0, 30;
    my $dt = substr  $data->{'entry'}->{$arr[$i]}->{'modified'}, 0, 10;
    print "\n";
    print "$frm\n";
    print "$title\n";
}
    print "\n";
} else {
    print "0 New Email(s)\n";
}
exit 0;


@hakerdefo:

You'll notice a new line I have added

use Term::ANSIColor qw( colored );

I am working on getting a simple color change to the script and will post it when I figure it out

Edit: Updated the above script to address this:

I am also working on this line:

Instead of it saying

New Email(s) 2

I would rather have it be reversed

2 New Email(s)

but I am struggling to get it right


thank you for this one.. it is extremely close to perfect and workable enough to get there... I happen to really like perl so well done mate!
VSIDO      VSIDO Change Blog    

    I dev VSIDO

VastOne

#5
On the second mod to get the format of how the emails are displayed, I have played with it and found the correct code for it...

print "". @arr ."\nNew Email(s)";

There is was an issue of it sending the New Emails to the next line though that I still have to work out

Edit:

Worked it out and updated the script above

print "". @arr ." New Email(s) \n";
VSIDO      VSIDO Change Blog    

    I dev VSIDO

VastOne

Quote from: hakerdefo on November 19, 2016, 07:34:55 PM

Now by default this script will show the last three (3) unread mails. If you want to change this number, edit the following line in the script (line number 13),


my $n = 3;



Question: Does line 14 also have to change if you change 13?
VSIDO      VSIDO Change Blog    

    I dev VSIDO

VastOne

#7
Never did get a grasp on colorizing the single output of the ". @arr ." argument so I fixed it in the conky line, though it changes the color of the entire script... which is fine


${color 7D8C93}                ${time %I:%M%P}
${color 7D8C93}         ${voffset 5}${time %a %b %d %Y}
${if_up eth0} ${color 7D8C93}${offset -18} ${voffset 6} VastOne  - ${color white} ${execi 25 perl ~/check-gmail.pl your_gmail_login your_gmail_password}${endif}



 
VSIDO      VSIDO Change Blog    

    I dev VSIDO

hakerdefo

Great work there VastOne!
I can confirm that the script borks with only one new mail in the inbox. The problem lies with 'XML::Simple' module so I'm currently working on replacing it with another module. And a gmail-checker written in bash is almost complete as well. A few personal things are not allowing time to complete them. Coming soon is all I can say right now.
Cheers!!!
You Can't Always Git What You Want

VastOne

..and of course as fate would have it, a new update to python fixes the original script.. We now have tons of options

The thing I love about this forum is when a problem and explanation is offered, several solutions rise

Great group of people here, thank you all!
VSIDO      VSIDO Change Blog    

    I dev VSIDO

PackRat

Quote from: VastOne on November 22, 2016, 10:02:19 PM
..and of course as fate would have it, a new update to python fixes the original script.. We now have tons of options

They're linked (or will be) in one How To thread aren't they? One stop shopping.
I am tired of talk that comes to nothing.
-- Chief Joseph

...the sun, the darkness, the winds are all listening to what we have to say.
-- Geronimo

VastOne

Yes... as soon as the bash script is complete I will sort it and put up a good How To

Thanks for the idea..  8)
VSIDO      VSIDO Change Blog    

    I dev VSIDO