Author Topic: conky - Gmail new email information (perl version 1)  (Read 2351 times)

hakerdefo

  • Posts: 520
    • View Profile
    • Looking at Linux through the Windows of Life
conky - Gmail new email information (perl version 1)
« on: November 19, 2016, 07:34:55 PM »
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,

Code: [Select]
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,

Code: [Select]
#!/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),

Code: [Select]
my $n = 3;

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

Code: [Select]
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,

Code: [Select]
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

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #1 on: November 19, 2016, 07:47:32 PM »
Seems to function all right but is only seeing and saying No New Mail even though I have unread email...

VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #2 on: November 19, 2016, 07:49:11 PM »
But when I have multiple unread emails it seems to work fine...

Will keep on testing
VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #3 on: November 19, 2016, 09:32:23 PM »
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 Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #4 on: November 19, 2016, 09:39:57 PM »
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)

Code: [Select]
#!/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

Code: [Select]
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 Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #5 on: November 20, 2016, 05:06:44 PM »
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...

Code: [Select]
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

Code: [Select]
print "". @arr ." New Email(s) \n";
VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #6 on: November 20, 2016, 08:07:41 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),

Code: [Select]
my $n = 3;


Question: Does line 14 also have to change if you change 13?
VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

VastOne

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #7 on: November 21, 2016, 03:27:32 AM »
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

Code: [Select]
${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 Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

hakerdefo

  • Posts: 520
    • View Profile
    • Looking at Linux through the Windows of Life
Re: Gmail unread email information in conky
« Reply #8 on: November 22, 2016, 03:45:21 PM »
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

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #9 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

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 Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO

PackRat

  • Posts: 1909
    • View Profile
Re: Gmail unread email information in conky
« Reply #10 on: November 23, 2016, 12:45:17 AM »
..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

  •      v-ger
  • Posts: 3931
    • View Profile
    • VSIDO Community
Re: Gmail unread email information in conky
« Reply #11 on: November 23, 2016, 01:02:14 AM »
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 Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO