Вот нарыл в инете такой скрипт для забора тем писем с gmail'a, который запускается из conky:
# ======================================================================
# Copyright (C) 2006 Baishampayan Ghose <b.ghose@ubuntu.com>
# Modified 2008 Hunter Loftis <hbloftis@uncc.edu>
# Time-stamp: Mon Jul 31, 2006 20:45+0530
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# ======================================================================
import sys
import urllib # For BasicHTTPAuthentication
import feedparser # For parsing the feed
import pickle
from textwrap import wrap
_URL = "https://mail.google.com/gmail/feed/atom"
uname = sys.argv[1]
password = sys.argv[2]
maxlen = sys.argv[3]
filename = "~/scripts/subjects"
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 '${color1} %s new emails:\n' % (len(atom.entries))
file = open(filename, 'w')
data ='${color1}%s new emails:\n' % (len(atom.entries))
file.write(data)
for i in range(min(len(atom.entries), maxlen)):
# print ' ${color2}%s' % atom.entries[i].title.encode('utf-8')
data =' ${color2}' + atom.entries[i].title.encode('utf-8') + '\n'
file.write(data)
#uncomment the following line if you want to show the name of the sender
# print ' ${color2}%s' % atom.entries[i].author
if len(atom.entries) > maxlen:
print ' ${color}more...'
if __name__ == "__main__":
f = auth() # Do auth and then get the feed
readmail(f, int(maxlen)) # Let the feed be chewed by feedparser
#file.close()
Первый вопрос: Почему закомменченные строки в ф-ии readmail не выводят в консоль читаемый утф8, а лишь его коды, хоть я и сделал encode. Пришлось выводить в файл сначала, а потом cat этот файл в конки.
Воторой вопрос: На домашнем компе все работает (но через вышеозначенный костыль), но на работе нужно, чтоб скрипт забирал страницу через прокси. Заменяем ф-ию auth на следующее:
def auth():
'''The method to do HTTPBasicAuthentication'''
proxy = {'http':'http://10.10.176.177:8181'} // домашний прокси
opener = urllib.FancyURLopener(proxy)
f = opener.open(_URL)
feed = f.read()
print feed // для дебага, но ничего не выводит, тк через прокси не проходит :)
return feed
Не работает. Что поправить, чтоб через прокси ходил?