用python写的博客客户端

换过很多博客的客户端,感觉没有一款适合自己的。正好这几天在看python,就用python写一个博客园的,用来发布博客。用来博客园的MetaWeblog的API。

  1 #!/usr/bin/python
  2 
  3 import xmlrpclib
  4 import sys
  5 import time
  6 import os
  7 
  8 class Blog(object):
  9     """
 10         works with www.cnblogs.com
 11     """
 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 = serviceURL
 20         self.appKey = appKey
 21         self.username = username
 22         self.password = password
 23         self.methods = []
 24         
 25         # Connect to the api, and keep available methods
 26         self.server = xmlrpclib.ServerProxy(serviceURL)
 27         self.list_methods()
 28 
 29     def list_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 not len(self.methods):
 37             self.methods = self.server.system.listMethods()
 38         return self.methods.sort()
 39 
 40 class cnblog(Blog):
 41     """
 42     Python interface to cnblog API
 43     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         return self.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     def getUserBlogs(self):
 58         return self.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         return self.server.metaWeblog.editPost( postid, self.usernamem, self.password, newpost, publish)
 63 
 64     # Returns a list of categories.
 65     def getCategories(self):
 66         return self.server.metaWeblog.getCategories(self.blogid, self.username, self.password)
 67 
 68     # Returns dictionary based post content corresponding to postid.
 69     def getPost(self, postid):
 70         return self.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         return self.server.metaWeblog.getRecentPosts(self.blogid, self.username, self.password, numposts)
 75 
 76     # Makes a new file to a designated blog
 77     def newMediaObject(self, new_object):
 78         return self.server.metaWeblog.newMediaObject(self.blogid, self.username, self.password, new_object)
 79 
 80     # New post
 81     def newPost(self, post, publish=True):
 82         return self.server.metaWeblog.newPost(self.blogid, self.username, self.password, post, publish)
 83 
 84 def menu():
 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     while True:
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 in range(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"

基本的接口都实现了,具体的有时间去完善下。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。