python写博客_用python写的博客客户端

1 #!/usr/bin/python

2

3 importxmlrpclib4 importsys5 importtime6 importos7

8 classBlog(object):9 """

10 works with www.cnblogs.com11 """

12 def __init__(self, serviceURL, username, password, appKey):13 """

14 Args:15 serviceURL: URL to the XML-RPC API.16 username: username for the Blog account.17 password: password for the Blog account.18 """

19 self.serviceURL =serviceURL20 self.appKey =appKey21 self.username =username22 self.password =password23 self.methods =[]24

25 #Connect to the api, and keep available methods

26 self.server =xmlrpclib.ServerProxy(serviceURL)27 self.list_methods()28

29 deflist_methods(self):30 """

31 Call system.listMethods on ths server.32

33 Return:34 List of XML-RPC methods implemethed by the server.35 """

36 if notlen(self.methods):37 self.methods =self.server.system.listMethods()38 returnself.methods.sort()39

40 classcnblog(Blog):41 """

42 Python interface to cnblog API43 This class extends Blog to implement cnblog API.44 """

45 def __init__(self, serverapi, username, password, appkey='0x001'):46 Blog.__init__(self, serverapi, username, password, appkey)47 info =self.getUserBlogs()48 self.blogid = info[0]['blogid']49 self.url = info[0]['url']50 self.blogName = info[0]['blogName']51

52 #delete a post

53 def deletePost(self, postid, publish=True):54 returnself.server.blogger.deletePost( self.appKey, postid, self.username, self.password, publish)55

56 #Returns information on all the blogs a given user is a member.

57 defgetUserBlogs(self):58 returnself.server.blogger.getUsersBlogs(self.appKey, self.username, self.password)59

60 #Updates and existing post to a designated blog

61 def editPost(self, posiid, newpostm, publish=True):62 returnself.server.metaWeblog.editPost( postid, self.usernamem, self.password, newpost, publish)63

64 #Returns a list of categories.

65 defgetCategories(self):66 returnself.server.metaWeblog.getCategories(self.blogid, self.username, self.password)67

68 #Returns dictionary based post content corresponding to postid.

69 defgetPost(self, postid):70 returnself.server.metaWeblog.getPost(postid, self.username, self.password)71

72 #Returns 'numposts' number of recent post for the blog identified by 'blogid'

73 def getRecentPosts(self, numposts=10):74 returnself.server.metaWeblog.getRecentPosts(self.blogid, self.username, self.password, numposts)75

76 #Makes a new file to a designated blog

77 defnewMediaObject(self, new_object):78 returnself.server.metaWeblog.newMediaObject(self.blogid, self.username, self.password, new_object)79

80 #New post

81 def newPost(self, post, publish=True):82 returnself.server.metaWeblog.newPost(self.blogid, self.username, self.password, post, publish)83

84 defmenu():85 print "*" * 20

86 print "1. New Post"

87 print "2. Get Recent Posts"

88 print "3. Get Post"

89 print "0. Exit"

90 print "*" * 20

91

92 if __name__ == "__main__":93 serviceURL= 'http://www.cnblogs.com/waikeung/services/MetaWeblog.aspx'

94 appKey = 'waikeung'

95 username = 'waikeung'

96 password = '******'

97 blog =cnblog(serviceURL,username, password, appKey)98 print "*" * 5, blog.blogName, "*" * 5

99 print '\n'

100 whileTrue:101 menu()102 choose = int(raw_input("choose:"))103 if choose == 1:104 blog.newPost(dict(title='title', description='description'))105 elif choose == 2:106 posts =blog.getRecentPosts()107 for index inrange(len(posts)):108 print posts[index]['postid'], posts[index]['title']109 elif choose == 3:110 post = blog.getPost('3473517')111 print post['title']112 elif choose ==0:113 print "Bye!"

114 sys.exit(0)115 else:116 print "choose Error"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值