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: 218

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: 266
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

Board footer

Powered by FluxBB