You are not logged in.

#1 2010-10-19 10:05:08

tawan
Member
Registered: 2010-03-02
Posts: 290
Website

python3 gmail and conky [solved by using python2]

I have become quite dependent on conky displaying the contents of my gmail mailboxes and something seems to have broken since python3 came about.

Can anyone with python knowledge point me to how to get it working again?

Old code

## needs The urllib and feedparser Python libraries

import sys
import urllib3             # For BasicHTTPAuthentication
import feedparser         # For parsing the feed
from textwrap import wrap

_URL = "https://mail.google.com/gmail/feed/atom"

uname = sys.argv[1]
password = sys.argv[2]
maxlen = sys.argv[3]

urllib.FancyURLopener.prompt_user_passwd = lambda self, host, realm: (uname, password)
    
def auth():
    '''The method to do HTTPBasicAuthentication'''
    opener = urllib.FancyURLopener()
    f = opener.open(_URL)
    feed = f.read()
    return feed

def readmail(feed, maxlen):
    '''Parse the Atom feed and print a summary'''
    atom = feedparser.parse(feed)
    print '%s' % (len(atom.entries))

if __name__ == "__main__":
    f = auth()  # Do auth and then get the feed
    readmail(f, int(maxlen)) # Let the feed be chewed by feedparser

error

 File "theabovescript.py", line 26
    print '%s' % (len(atom.entries))
             ^
SyntaxError: invalid syntax

Last edited by tawan (2010-10-19 20:05:50)

Offline

#2 2010-10-19 10:16:54

drm00
Member
Registered: 2010-06-01
Posts: 38

Re: python3 gmail and conky [solved by using python2]

I think there is no substitution with % anymore, and print is now a function.

Try

print("{0}".format(len(atom.entries)))

or

print(len(atom.entries))

Edit: I would also suggest that the function 'readmail' returns the value instead of just printing it. 'maxlen' is not used either, or am I missing something?

def readmail(feed):
    '''Parse the Atom feed and print a summary'''
    atom = feedparser.parse(feed)
    return len(atom.entries)

and

print(readmail(f)) # Let the feed be chewed by feedparser

Last edited by drm00 (2010-10-19 10:24:16)

Offline

#3 2010-10-19 11:23:48

Foucault
Member
From: Athens, Greece
Registered: 2010-04-06
Posts: 214

Re: python3 gmail and conky [solved by using python2]

For python3

print '%s' % (len(atom.entries))

should be

print ('%s' % (len(atom.entries)))

However the script won't work anyway since urllib3 depends upon python2 not python3. So, you'd better update your conky to invoke python2 instead of python (which is python3)

Last edited by Foucault (2010-10-19 11:24:06)

Offline

#4 2010-10-19 11:39:22

wonder
Developer
From: Bucharest, Romania
Registered: 2006-07-05
Posts: 5,941
Website

Re: python3 gmail and conky [solved by using python2]

doesn't work if you add on the first line in your script #!/usr/bin/python2 ?


Give what you have. To someone, it may be better than you dare to think.

Offline

#5 2010-10-19 12:32:55

tawan
Member
Registered: 2010-03-02
Posts: 290
Website

Re: python3 gmail and conky [solved by using python2]

So far still broken here. Thanks for the ideas.

After some more Gooogling it turns out the feedparser is broken under python3.

I did think the idea of running the script as python2 would work but no...

Offline

#6 2010-10-19 16:32:21

kachelaqa
Member
Registered: 2010-09-26
Posts: 216

Re: python3 gmail and conky [solved by using python2]

tawan wrote:

I did think the idea of running the script as python2 would work but no...

exactly what errors do you get when running your script in a terminal with python2?

Offline

#7 2010-10-19 19:40:42

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: python3 gmail and conky [solved by using python2]

conkyemail works if you run it with python2. You might be able to use that or find inspiration in the source. . .


Arch x64 on Thinkpad X200s/W530

Offline

#8 2010-10-19 20:05:08

tawan
Member
Registered: 2010-03-02
Posts: 290
Website

Re: python3 gmail and conky [solved by using python2]

Pank wrote:

conkyemail works if you run it with python2. You might be able to use that or find inspiration in the source. . .

great the above should get me up and running again thanks!

Offline

#9 2010-10-20 08:04:06

tawan
Member
Registered: 2010-03-02
Posts: 290
Website

Re: python3 gmail and conky [solved by using python2]

Fixed the original script smile

I added the shebang for python2 and also edited the bash script I use to call the script as python2 - thanks for all the ideas, truly solved now.

Offline

#10 2010-10-21 01:06:32

fuzymonkey
Member
From: Austin, TX
Registered: 2009-07-28
Posts: 20

Re: python3 gmail and conky [solved by using python2]

Let me know if anyone successfully ports this to python3. I've been trying, but it's not as easy as I expected


End of line

Offline

#11 2010-10-25 05:34:08

dlin
Member
From: Taipei,Taiwan
Registered: 2005-09-21
Posts: 265

Re: python3 gmail and conky [solved by using python2]

/usr/bin/2to3 -w foo.py  # change the code to valid python3 usage.


Running 4 ArchLinux including sh4twbox,server,notebook,desktop. my AUR packages

Offline

#12 2010-10-26 01:11:53

fuzymonkey
Member
From: Austin, TX
Registered: 2009-07-28
Posts: 20

Re: python3 gmail and conky [solved by using python2]

I've managed to hack this together:

#!/usr/bin/env python
import urllib.request
import re

URL = "https://mail.google.com/gmail/feed/atom"

username = "username"
password = "********"

def find(term, lines):
    for line in lines:
        if re.search(term, line):
            return line

    print("?")
    exit(1)

def get_feed():
    auth_handler = urllib.request.HTTPBasicAuthHandler()
    auth_handler.add_password(realm = "New mail feed", uri = "https://mail.google.com/", user = username, passwd = password)
    opener = urllib.request.build_opener(auth_handler)

    urllib.request.install_opener(opener)

    try:
        feed = urllib.request.urlopen(URL).read()
    except urllib.error.URLError:
        print("?")
        exit(1)

    return feed

xml = get_feed()
xml = xml.decode()
linebyline = xml.splitlines()

count = find("<fullcount>", linebyline)
count = re.sub("^.*(\d+).*$", r"\1", count)
print(count)

End of line

Offline

#13 2010-12-03 11:36:43

tawan
Member
Registered: 2010-03-02
Posts: 290
Website

Re: python3 gmail and conky [solved by using python2]

Thanks for the above - my python2 script broke again recently, even with the hacks, so I've taken your python3 one and adapted to my needs smile

fuzymonkey wrote:

I've managed to hack this together:

#!/usr/bin/env python
import urllib.request
import re

URL = "https://mail.google.com/gmail/feed/atom"

username = "username"
password = "********"

def find(term, lines):
    for line in lines:
        if re.search(term, line):
            return line

    print("?")
    exit(1)

def get_feed():
    auth_handler = urllib.request.HTTPBasicAuthHandler()
    auth_handler.add_password(realm = "New mail feed", uri = "https://mail.google.com/", user = username, passwd = password)
    opener = urllib.request.build_opener(auth_handler)

    urllib.request.install_opener(opener)

    try:
        feed = urllib.request.urlopen(URL).read()
    except urllib.error.URLError:
        print("?")
        exit(1)

    return feed

xml = get_feed()
xml = xml.decode()
linebyline = xml.splitlines()

count = find("<fullcount>", linebyline)
count = re.sub("^.*(\d+).*$", r"\1", count)
print(count)

Offline

Board footer

Powered by FluxBB