You are not logged in.

#1 2011-11-20 04:54:30

duke11235
Member
Registered: 2009-10-09
Posts: 221

[SOLVED]Conky Gmail Script Incorrect

The python gmail script on the arch wik no longer works on python3 or python2 for me. This error is shown in wing:

builtins.ValueError: invalid literal for int() with base 10:

I'm not sure how to fix the incorrect int value, but I would love to see it fixed and posted to the arch wiki again.

 
#Enter your username and password below within double quotes
# eg. username="username" and password="password"
username="****"
password="****"
com="wget -q -O - https://"+username+":"+password+"@mail.google.com/mail/feed/atom --no-check-certificate"

temp=os.popen(com)
msg=temp.read()
index=msg.find("<fullcount>")
index2=msg.find("</fullcount>")
fc=int(msg[index+11:index2])

if fc==0:
   print("0 new")
else:
   print(str((fc)+" new"))

Thanks

Last edited by duke11235 (2011-11-21 04:55:28)

Offline

#2 2011-11-20 18:20:49

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: [SOLVED]Conky Gmail Script Incorrect

Use this:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

NS = '{http://purl.org/atom/ns#}'
USERNAME = 'xxx'
PASSWORD = 'xxx'

URL = 'https://{0}:{1}@mail.google.com/mail/feed/atom'.format(
    USERNAME, PASSWORD)

def main():
    with urlopen(URL) as source:
        tree = etree.parse(source)
    fullcount = tree.find(NS + 'fullcount').text
    if not fullcount:
        print('failed to parse mail feed')
    print('{0} new'.format(fullcount.text.strip()))


if __name__ == '__main__':
    main()

I don't know why the script you've shown doesn't work any more, but you shouldn't use it anyway.  It is really bad, whoever wrote it did apparently not have much clue of Python.

Offline

#3 2011-11-20 20:21:31

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Conky Gmail Script Incorrect

It looks interesting, but this throws me a different error:

http.client.InvalidURL: nonnumeric port: 'abcde*$blahbLAH$@mail.google.com'

From what I see, it doesn't like the password format, and wants some use of auth_handler to fix it. I think. I'm still new to this language.

Offline

#4 2011-11-21 00:35:11

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED]Conky Gmail Script Incorrect

Your problem may be with wget.  Try curl:

com = 'curl -s -u "{}:{}" https://mail.google.com/mail/feed/atom'.format( username, password )

The url you are using is for the Gmail RSS feed, and it contains a ':'.  The python urllib.request module that lunar used expects that a port number will follow the ':'. That is why lunar's script is failing for you.

The script I use does not use the RSS feed.  It uses the python IMAP library and connects to imap.gmail.com at port 993.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys, imaplib

port = 993
server = 'imap.gmail.com'

username = '...'
passwd = '...'

imap_server = imaplib.IMAP4_SSL(server, port)
try:
	imap_server.login(username, passwd)
except:
	print('?? new')
	sys.exit( 1 )

typ, data = imap_server.select ('Inbox', True)
if typ == 'OK':
	total = int(data[0])
	typ, data = imap_server.search (None, 'SEEN')
	if typ == 'OK':
		seen = len(data[0].split())
		print('{}/{} new'.format(total, total - seen))

if typ != 'OK':
	print('?? new')

imap_server.logout()

Offline

#5 2011-11-21 00:40:13

kaptenen
Member
Registered: 2009-12-06
Posts: 287
Website

Re: [SOLVED]Conky Gmail Script Incorrect

I did also have that problem, I see gmail count in this way:
.netrc (in home) with this content
machine mail.google.com  login username password yourpassword

And this script:

#!/bin/bash
curl -n https://mail.google.com/mail/feed/atom -s | grep fullcount | tail -c +12 | head -c -13 

It works for me. smile You may want to make sure only you can read .netrc. Maybe you don´t even need to use it as a script.

Offline

#6 2011-11-21 04:55:13

duke11235
Member
Registered: 2009-10-09
Posts: 221

Re: [SOLVED]Conky Gmail Script Incorrect

Thanks for the solutions rockin turtle and captenen. Interesting to know that curl should have been used instead. It now happily queries the server and returns the correct value. Somebody ought to remove or fix that script on the wiki.

Offline

#7 2011-11-21 20:51:38

juster
Forum Fellow
Registered: 2008-10-07
Posts: 195

Re: [SOLVED]Conky Gmail Script Incorrect

wget works if you specify the username and password with options:

wget -q -O - --auth-no-challenge --user=username --password=secret 'https://mail.google.com/mail/feed/atom' | awk 'gsub(/<\/?fullcount>/, "")'

Offline

#8 2018-04-01 23:29:01

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

Re: [SOLVED]Conky Gmail Script Incorrect

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;
}

Offline

#9 2018-04-01 23:32:55

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

Re: [SOLVED]Conky Gmail Script Incorrect

There is no need to spam the boards, and necrobump old threads to promote your script.


Closing


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB