MetaWeblog API csdn python implementation

Source: Internet
Author: User

Layout:post
Author: "Kele"
Title: "MetaWeblog API csdn python Implementation"
Tags

    • Python
    • MetaWeblog
      Date: "2018-02-14 22:15:32"
      ---

      MetaWeblog API csdn python implementation

      Always want to sync three blog, Dream is very big, has been looking for various tools, but one did not encounter, today suddenly found a call as open live Writer's things, carefully studied the discovery is called MetaWeblog API, Cnblog and CSDN are supported , went to GitHub to see, found the Python implementation of the Cnblog MetaWeblog API program, so studied, wrote the CSDN API

    #coding: Utf-8 import xmlrpclib import OS import sys import MARKDOWN2 import httplib import mimetypes Username= "" password= "" Url= "Http://write.blog.csdn.net/xmlrpc/index" #server =xmlrpclib.            Serverproxy (URL) class Metaweblog_csdn:def __init__ (Self,url,username,password): Self.url=url            #print Url,username,password #self. P=proxy () #self. P.set_proxy ("127.0.0.1:8888") Self.user=username Self.passwd=password Self.server=xmlrpclib.             Serverproxy (url,encoding= "Utf-8") self.blog_id=self.getusersblogs () [0][' blogID '] #print self.blog_id            Self.type=["original", "Report", "translated"] def Newpost (self,title,description,categories,publish): Return Self.server.metaWeblog.newPost (SELF.BLOG_ID,SELF.USER,SELF.PASSWD, Dict (Title=title,descrip tion=description,categories=categories), publish) def Getpost (Self,postid):           Return Self.server.metaWeblog.getPost (POSTID,SELF.USER,SELF.PASSWD) def getcategories (self): Return Self.server.metaWeblog.getCategories (SELF.BLOG_ID,SELF.USER,SELF.PASSWD) def getrecentposts (self,                numberofposts): Return self.server.metaWeblog.getRecentPosts (self.blog_id,self.user,self.passwd,numberofposts) def editpost (self,postid,title,content,categories,tags,publish,type=0,description= ""): Return SE Lf.server.metaWeblog.editPost (POSTID,SELF.USER,SELF.PASSWD, Dict (title=title,type=self.type[type],descripti On=description,content=content,categories= ",". Join (categories), tags= ",". Join (tags)), publish) def Newmediaobject ( Self,abspath): With open (Abspath, "RB") as F:bits=xmlrpclib. Binary (F.read ()) Type=mimetypes.guess_type (Abspath) [0] Name=os.path.basename (abspath) ret Urn Self.server.newMediaObject (self.blog_id,self.user,dict (bits=bits,name=Name,type=type)) def getuserinfo (self): return Self.server.blogger.getUserInfo ("0123456789ABCDEF", self.u SER,SELF.PASSWD) def deletepost (self,postid,publish): Return Self.server.blogger.deletePost ("0123456789A Bcdef ", Postid,self.user,self.passwd,publish) def getusersblogs (self): return Self.server.blogger . Getusersblogs ("0123456789ABCDEF", self.user,self.passwd) # Just to communicate through the Fiddler agent, to carry out the Grab Package Debug class proxy (Xmlrpclib.            Transport): Def set_proxy (self, proxy): Self.proxy = proxy def make_connection (self, host): Self.realhost = host H = httplib.            Httpconnection (Self.proxy) return H def send_request (self, connection, Handler, Request_body):            Connection.putrequest ("POST", ' http://%s%s '% (self.realhost, handler)) def send_host (self, Connection, host): Connection.putheader (' Host ', self.realhost) if __name__== "__main__": If Len (sYS.ARGV)!=3:print "error" ELSE:TEST=METAWEBLOG_CSDN (Url,username,password) test. Newpost (Sys.argv[1],markdown2.markdown_path (sys.argv[2]), [""],true] "else:test=metaweblog_csdn (url,us        Ername,password) with open (Sys.argv[2], "R") as Blog:test.newPost (title,description,[""],true)        If __name__== "__main__": Test=metaweblog_csdn (Url,username,password) #temp =test.getcategories () [0] #for I in temp: # print i+ ":" +temp[i].encode ("Utf-8") #print getblogid () [0][' blogID '] # print TEST.G Etuserinfo () #print os.listdir ("./") print test.getcategories () #print test.getusersblogs () #p Rint test.getrecentposts () #print test.getuserinfo () #print html.encode ("Utf-8") #print test. Newpost ("Test", "test", "note", "Test", True,html.encode ("Utf-8"), 0) Try: #print test.newpost ("test", "tests", [ "Notes"],["tests"],true, "TeSt ", 0) except:print" Erro " 

The point of comparison pit is that when sending categories must be a list, so he will be converted into an array type in Xmlrpclib, then can send normally, otherwise it will be 500 error.

Xmlrpclib Agent

I was through the fiddle grab the package to debug, fiddle open agent is 127.0.0.1:8888, so we need to allow the creation of Severproxy when the agent, check the official documents to get:
Import Httplib
Class Proxy (Xmlrpclib. Transport):
def set_proxy (self, proxy):
Self.proxy = Proxy

        def make_connection(self, host):            self.realhost = host            h = httplib.HTTPConnection(self.proxy)            return h        def send_request(self, connection, handler, request_body):            connection.putrequest("POST", ‘http://%s%s‘ % (self.realhost, handler))        def send_host(self, connection, host):            connection.putheader(‘Host‘, self.realhost)

proxy=Proxy("127.0.0.1:8888")Then when constructing the serverproxy, you need to add the transport parameter,server=xmlrpclib.ServerProxy("url",transport=proxy)
Then you can view the packets you send and receive via fiddle.

MetaWeblog API csdn python implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.