Chinese => English   |   英文 => 中文
随笔-1  评论-0  文章-0  trackbacks-0

一段提交文章到cppblog的代码

实现:

# -*- coding: utf-8 -*-

import xmlrpclib as rpc


class Poster(object):

    service_url_tpl = 'http://www.cppblog.com/%s/services/metaweblog.aspx'

    def __init__(self, usr_name='your_name', usr_pwd='your_pwd'):
        self.usr_name = usr_name
        self.usr_pwd = usr_pwd
        self.service_url = self.service_url_tpl % usr_name
        self._s = rpc.ServerProxy(self.service_url)

    def login(self):
        try:
            rv = self._s.blogger.getUsersBlogs('', self.usr_name, self.usr_pwd)
            if rv:
                self.blog_id = rv[0]['blogid']
                return True
        except rpc.Error, e:
            print 'RPC_ERROR', e
        except Exception, e:
            print 'ERROR', e
        return False

    def add_post(self, title, content, category=None, is_publish=False):
        import datetime as dt
        post = {
            'dateTime':         dt.datetime.now(),
            'description':      content,
            'title':            title,
        }
        if category:
            post['categories'] = [category]

        rv = self._s.metaWeblog.newPost(self.blog_id, self.usr_name,
                self.usr_pwd, post, is_publish)
        return rv

    @property
    def categories(self):
        rv = self._s.metaWeblog.getCategories(self.blog_id,
                self.usr_name, self.usr_pwd)
        return [(e['categoryid'], e['title']) for e in rv]

    @property
    def recent_posts(self):
        return self._s.metaWeblog.getRecentPosts(self.blog_id, self.usr_name,
                self.usr_pwd, 7)

    def del_post(self, post_id, re_publish=True):
        rv = self._s.blogger.deletePost('', post_id, self.usr_name,
                self.usr_pwd, re_publish)
        return rv

使用:

s = Poster()
if s.login():
    s.add_post(u'标题', u'内容', '分类名称', True)

参见:

posted on 2010-09-15 20:37 yugi 阅读(1250) 评论(0)  编辑 收藏 引用 所属分类: Python

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理