You are not logged in.
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
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
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
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
Offline
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
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
Offline
Offline
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
/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
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
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
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