feedparser 同步博客园rss到 qq zone

#!/usr/bin/env python

#coding=UTF-8

#FileName: syn_qzone.py

 

import smtplib, feedparser, cPickle, sys

from email.mime.text import MIMEText

 

url = "http://www.cnblogs.com/lexus/rss" #feed url(Atom)

qq_number = "165696916" #qq number

qq_password = "qw198465" #qq password

qq_username = qq_number + "@qq.com"

qzone_add = qq_number + "@qzone.qq.com"

mailto = [qzone_add]

 

try:

    file = open('published_title','r')

    published_title = cPickle.load(file)

except Exception, e:

    published_title = []

 

def mailto_blog(username,password,sub,content,mailto_list):

    msgRoot = MIMEText(content,'html','UTF-8')

    msgRoot['Subject'] = sub

    msgRoot['From'] = username

    msgRoot['To'] = ";".join(mailto_list)

 

    try:

        s = smtplib.SMTP()

        s.connect("smtp.qq.com")

        try:

            s.login(username, password)

        except Exception,e:

            print "请确认使用了正确的用户名及密码!"

            sys.exit(0)

        s.sendmail(username, mailto_list, msgRoot.as_string())

        s.close()

        return True

    except Exception, e:

        #print str(e)

        return False

 

def syn_articles(feed_url):

    feed_data = feedparser.parse(feed_url)

    items = feed_data['items']

    items.reverse()

    for item in items:

        title = item.title.encode('UTF-8')

        link = item.link.encode('UTF-8')

        article = "<b>本文为自动同步,原文地址:<a href=\"" + link + "\">" + link + "</a><br /></b>" + item.description.encode('UTF-8')

        if title in published_title:

            print "失败:\"%s\"已存在,发布失败!" % title

        else:

            if mailto_blog(qq_username,qq_password,title,article,mailto):

                published_title.append(title)

                print "成功:\"%s\"已经成功同步!" % title

 

syn_articles(url)

file = open('published_title','w')

cPickle.dump(published_title, file)

file.close()

 

非我原创我只是改了一下

reference:

http://www.feedparser.org/docs/common-rss-elements.html

你可能感兴趣的:(parser)