You are not logged in.

#1 2017-01-03 19:04:10

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Getting gmail to work with Conky [Solved]

Hi all ,

I'm trying to get conky to display e-mail updates using the wiki but I seem to be missing something. Here's my pyton script and I'll put my conky conf below as well as permissions on the scipt.

gmail.py

#!/usr/bin/env python

from urllib.request import FancyURLopener

email = 'username' # @gmail.com can be left out
password  = 'password'

url = 'https://%s:%s@mail.google.com/mail/feed/atom' % (email, 
password)

opener = FancyURLopener()
page = opener.open(url)

contents = page.read().decode('utf-8')

ifrom = contents.index('<fullcount>') + 11
ito   = contents.index('</fullcount>')

fullcount = contents[ifrom:ito]

print(fullcount + ' new')

Conky Config

conky.config = {
    alignment = 'top_right',
    background = true,
    border_width = 0,
    cpu_avg_samples = 2,
	default_color = 'black',
    default_outline_color = 'white',
    default_shade_color = 'white',
    double_buffer = true,
    draw_borders = false,
    draw_graph_borders = true,
    draw_outline = true,
    draw_shades = true,
    use_xft = true,
    font = 'DejaVu Sans Mono:size=10',
    gap_x = 5,
    gap_y = 30,
    minimum_height = 5,
	minimum_width = 5,
    net_avg_samples = 2,
    no_buffers = true,
    out_to_console = false,
    out_to_stderr = false,
    extra_newline = false,
    own_window_transparent = true,
    own_window = true,
    own_window_type = 'override',
    own_window_class = 'Conky',
    stippled_borders = 0,
    update_interval = 1.0,
    uppercase = false,
    use_spacer = 'none',
    show_graph_scale = false,
    show_graph_range = false
}

conky.text = [[
${execpi 5 python ~/scripts/gmail.py}
#${scroll 16 $nodename - $sysname $kernel on $machine | }
#$hr
#${color black}Uptime:$color $uptime
${color black}Frequency (in MHz):$color $freq
#${color black}Frequency (in GHz):$color $freq_g
${color black}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}
#${color black}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 
#4}
${color black}CPU Usage:$color $cpu% ${cpubar 4}
#${color black}Processes:$color $processes  ${color black}Running:$color 
#$running_processes
$hr
${color black}File systems:
 / $color${fs_used /}/${fs_size /} ${fs_bar 6 /}
#${color black}Networking:
#Up:$color ${upspeed wlp2s0} ${color black} - Down:$color ${downspeed 
#wlp2s0} 
${if_existing /sys/class/net/wlp2s0/operstate up}
${color black}Net Down:$color ${downspeed wlp2s0}     ${color black}Net Up:$color ${upspeed wlp2s0}
${color black}${downspeedgraph wlp2s0 32,155 104E8B 0077ff} $alignr${color black}${upspeedgraph wlp2s0 32,155 104E8B 0077ff}
${endif}
$hr
${color black}Name              PID   CPU%   MEM%
${color black} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}
${color black} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}
${color black} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}
${color black} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}
]]

permission for pyton script

-rw-r--r--  1 dan  dan   446 Jan  3 13:43 gmail.py

Thank you!

Last edited by ArchNoobYo (2017-01-04 19:07:49)

Offline

#2 2017-01-04 04:16:49

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Getting gmail to work with Conky [Solved]

So what is the problem that you are facing? If you run the script manually, does it work?

Offline

#3 2017-01-04 14:17:28

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Re: Getting gmail to work with Conky [Solved]

x33a wrote:

So what is the problem that you are facing? If you run the script manually, does it work?

Conky doesn't change relating to the configuration I did to add gmail fetches.

But you are correct, the script doesn't run. It says it's an authentication issue but I've updated my password and am sure it works.

Here's the output when I try to run the script.

python gmail.py 
Traceback (most recent call last):
  File "gmail.py", line 11, in <module>
    page = opener.open(url)
  File "/usr/lib/python3.5/urllib/request.py", line 1698, in open
    return getattr(self, name)(url)
  File "/usr/lib/python3.5/urllib/request.py", line 1908, in open_https
    return self._open_generic_http(self._https_connection, url, data)
  File "/usr/lib/python3.5/urllib/request.py", line 1873, in _open_generic_http
    response.status, response.reason, response.msg, data)
  File "/usr/lib/python3.5/urllib/request.py", line 1889, in http_error
    result = method(url, fp, errcode, errmsg, headers)
  File "/usr/lib/python3.5/urllib/request.py", line 2143, in http_error_401
    headers)
  File "/usr/lib/python3.5/urllib/request.py", line 1898, in http_error_default
    raise HTTPError(url, errcode, errmsg, headers, None)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

Offline

#4 2017-01-04 17:11:43

svnset
Member
Registered: 2016-07-31
Posts: 75

Re: Getting gmail to work with Conky [Solved]

First of all, did this script ever work? Because you say you are sure the authentication works. Things can go wrong on both sides gmail and urllib when it comes to third party auth. Let's start with the google side, did you enable IMAP in gmail? (Settings -> POP/IMAP -> activate IMAP)


After one has played a vast quantity of notes and more notes,
it is simplicity that emerges as the crowning reward of art. - Chopin

Offline

#5 2017-01-04 17:35:03

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,475

Re: Getting gmail to work with Conky [Solved]

I tried the script with my gmail account and it works, so problem is with configuration of OP's gmail account. So as svnset suggested either imap is not enabled or if there is two-factor authentication set, password should be application key (not the user pass)
Edit: it works with this conky config

 

conky.config = {
    alignment = 'bottom_left',
    background = true,
    border_width = 1,
    cpu_avg_samples = 2,
    default_color = 'white',
    default_outline_color = 'white',
    default_shade_color = 'white',
    draw_borders = false,
    draw_graph_borders = false,
    draw_outline = false,
    draw_shades = false,
    use_xft = true,
    font = 'Anonymous Pro:size=16',
    format_human_readable = true,
    gap_x = 10,
    gap_y = 25,
    minimum_height = 5,
    minimum_width = 30,
    net_avg_samples = 2,
    double_buffer = true,
    out_to_console = false,
    out_to_stderr = false,
    extra_newline = false,
    own_window = true,
    own_window_class = 'Conky',
    own_window_type = 'override',
    own_window_transparent = true,
    stippled_borders = 0,
    update_interval = 1.0,
    uppercase = false,
    use_spacer = 'none',
    show_graph_scale = true,
    show_graph_range = false,
    override_utf8_locale = true
}

conky.text = [[
${color green}${font ConkySymbols:size=24}j$font$color ${membar 4}
${font Gill Sans MT Pro UltraBold:size=12}${color yellow}  Name            		 CPU%  MEM%
$font${color f9f999}${top_mem name 1}${color lightgrey}${top_mem cpu 1}  ${color ff9922}${top_mem mem_res 1}
${color f9f999}${top_mem name 2}${color lightgrey}${top_mem cpu 2}  ${top_mem mem_res 2}
${color f9f999}${top_mem name 3}${color lightgrey}${top_mem cpu 3}  ${top_mem mem_res 3}
${color f9f999}${top name 1}${color lightgrey}${color ff9922}${top cpu 1}  ${color lightgrey}${top mem_res 1}
${color green}${font ConkySymbols:size=24}F$font$color ${cpubar 4}${font Anonymous Pro:size=8}
#${color f9f999}${font Comic Sans MS:size=14}${color orange} ${execi 60 ls ~/Mail/new | wc -l}$color new mail(s)
$alignc${color f9f999}${font Comic Sans MS:size=14}${color orange} ${execi 60 ~/bin/gmail}
${font Anonymous Pro:size=11}$color$hr
$alignc${if_up wlan0}${color green}${font ConkySymbols:size=16}g${font Comic Sans MS:size=13}${color} $wireless_essid: ${addr wlan0} ${wireless_link_qual_perc wlan0}%${endif}${if_up ppp0}${color green}${font FontAwesome:size=16}${font Comic Sans MS:size=13}${color}:  ${addr ppp0}${endif}${font Comic Sans MS:size=5}
$color${font Anonymous Pro:size=3}$hr
$hr
]]

Look at the line with ~/bin/gmail.

Last edited by Docbroke (2017-01-04 17:44:03)

Offline

#6 2017-01-04 17:38:48

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Re: Getting gmail to work with Conky [Solved]

svnset wrote:

First of all, did this script ever work? Because you say you are sure the authentication works. Things can go wrong on both sides gmail and urllib when it comes to third party auth. Let's start with the google side, did you enable IMAP in gmail? (Settings -> POP/IMAP -> activate IMAP)

They thanks for the response. I guess what I meant is that I could login to the account so I assumed the credentials were good.

IMAP is indeed activated.

Offline

#7 2017-01-04 17:39:41

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Re: Getting gmail to work with Conky [Solved]

Docbroke wrote:

I tried the script with my gmail account and it works, so problem is with configuration of OP's gmail account. So as svnset suggested either imap is not enabled or if there is two-factor authentication set, password should be application key (not the user pass)

IMAP is enabled and I'm not sure what you mean by the two-factor authentication being set? Would that have been something I did?

What do you mean by application key instead of password?

Many thanks

Offline

#8 2017-01-04 17:48:14

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,475

Re: Getting gmail to work with Conky [Solved]

Nothing two worry if you don't remember setting up two-factor authentication ( it is extra protection of your gmail account where you have to provide OTP after your password to login to gmail)
So if you have imap enabled but still your script is not working and you are not using two-factor authentication, you need to allow less secure application in your gmail settings (Gmail blocks all the login attempts for applications that it thinks less secure - apps which are not created by or approved by google)
https://support.google.com/accounts/answer/6010255

Last edited by Docbroke (2017-01-04 18:04:57)

Offline

#9 2017-01-04 17:55:30

svnset
Member
Registered: 2016-07-31
Posts: 75

Re: Getting gmail to work with Conky [Solved]

2-Step Vertification: https://support.google.com/accounts/answer/185839

EDIT: Sorry, did not see you replied already Docbroke smile

Last edited by svnset (2017-01-04 17:57:23)


After one has played a vast quantity of notes and more notes,
it is simplicity that emerges as the crowning reward of art. - Chopin

Offline

#10 2017-01-04 17:57:01

summit69
Member
Registered: 2016-12-13
Posts: 12

Re: Getting gmail to work with Conky [Solved]

How about trying a mail client ex. Thunderbird just to isolate the problem is indeed with your smtp setup.

Offline

#11 2017-01-04 18:31:38

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Re: Getting gmail to work with Conky [Solved]

summit69 wrote:

How about trying a mail client ex. Thunderbird just to isolate the problem is indeed with your smtp setup.

Thunderbird is setup and working .

Offline

#12 2017-01-04 18:40:50

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Re: Getting gmail to work with Conky [Solved]

Docbroke wrote:

Nothing two worry if you don't remember setting up two-factor authentication ( it is extra protection of your gmail account where you have to provide OTP after your password to login to gmail)
So if you have imap enabled but still your script is not working and you are not using two-factor authentication, you need to allow less secure application in your gmail settings (Gmail blocks all the login attempts for applications that it thinks less secure - apps which are not created by or approved by google)
https://support.google.com/accounts/answer/6010255

Looks like the enabling less secure apps did it. Just testing but will post again shortly. Thanks again to everyone for the help.!!

Offline

#13 2017-01-04 19:07:35

ArchNoobYo
Member
Registered: 2016-12-07
Posts: 39

Re: Getting gmail to work with Conky [Solved]

Rocked it! Thanks guys smile

Offline

#14 2018-04-01 23:27:24

su8
Member
Registered: 2018-04-01
Posts: 2

Re: Getting gmail to work with Conky [Solved]

And C program that does the same job

/*
   04/02/2018 https://github.com/su8

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include <curl/curl.h>

#define GMAIL_ACC "foo"
#define GMAIL_PASS "bar"

static size_t read_gmail_data_cb(char *, size_t size, size_t nmemb, char *);

static size_t
read_gmail_data_cb(char *data, size_t size, size_t nmemb, char *str1) {
  char *ptr = data;
  size_t sz = nmemb * size, x = 0;

  for (; *ptr; ptr++, x++) {
    if ((x+17) < sz) { /* Verifying up to *(ptr+17) */

      if ('f' == *ptr) { /* fullcount */
        if ('f' == *ptr && 'u' == *(ptr+1) && 'l' == *(ptr+2)) {
          *str1++ = *(ptr+10); /* 1 email */
          if (0 != (isdigit((unsigned char) *(ptr+11)))) {
            *str1++ = *(ptr+11); /* 10 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+12)))) {
            *str1++ = *(ptr+12); /* 100 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+13)))) {
            *str1++ = *(ptr+13); /* 1000 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+14)))) {
            *str1++ = *(ptr+14); /* 10 000 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+15)))) {
            *str1++ = *(ptr+15); /* 100 000 emails */
          }
          *str1 = '\0';
          break;
        }
      }

    }
  }

  if ('\0' != *str1) {
    *str1++ = '\0';
  }
  return sz;
}


int main(void) {
  char str[1000] = "0";
  const char *const da_url = "https://mail.google.com/mail/feed/atom";

  CURL *curl = NULL;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_ALL);

  if (NULL == (curl = curl_easy_init())) {
    goto error;
  }

  curl_easy_setopt(curl, CURLOPT_USERNAME, GMAIL_ACC);
  curl_easy_setopt(curl, CURLOPT_PASSWORD, GMAIL_PASS);
  curl_easy_setopt(curl, CURLOPT_URL, da_url);
  curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
  curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); 
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, read_gmail_data_cb);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, str);

  res = curl_easy_perform(curl);
  if (CURLE_OK != res) {
    goto error;
  }

error:
  if (NULL != curl) {
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();

  printf("Unread Mails %s\n", str);
  return EXIT_SUCCESS;
}

Last edited by su8 (2018-04-01 23:31:56)

Offline

#15 2018-04-01 23:33:56

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Getting gmail to work with Conky [Solved]


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB