Skip to content

Instantly share code, notes, and snippets.

@sooop
Last active December 13, 2015 22:48
Show Gist options
  • Save sooop/4986757 to your computer and use it in GitHub Desktop.
Save sooop/4986757 to your computer and use it in GitHub Desktop.
MetaWeblog API Wrapper for python
#!/usr/local/bin/python
#filename:metaWeblog
'''
MetaWeblog API wrapper for python
Copyright 2013. All right reserved to sooop.
'''
import xmlrpclib
class Post:
def __init__(self):
self.keys = ["username", "permaLink", "guid",
"description", "pubDate", "author",
"title", "dateCreated", "tags",
"link", "postid", "categories"]
self.categories = []
self.postid = ""
self.link = ""
self.tags = []
self.dateCreated = ""
self.title = ""
self.author = ""
self.pubDate = ""
self.description = ""
self.guid = ""
self.permaLink = ""
self.username = ""
self.publish = True
def struct(self):
struct = {}
for k in self.keys:
struct[k] = getattr(self, k)
return struct
def addCategory(self, cats):
if type(cats) == list:
self.categories.extend(cats)
else:
self.categories.append(cats)
def addTags(self, *args, **kwargs):
if type(self.tags) == str and self.tags.strip() == "":
self.tags = []
for i in args:
print i
self.tags.append(i)
@staticmethod
def postFromStructs(structs):
if type(structs) == list:
result = []
for elem in structs:
a = Post()
for key in a.keys:
setattr(a, key, elem[key])
result.append(a)
if len(result) > 1:
return result
else:
return result[0]
else:
result = Post()
for key in structs:
setattr(result, key, structs[key])
return result
class Weblog:
def __init__(self, service_url, blog_id=None, user_id=None, password=None):
self.blog_id = blog_id
self.user_id = blog_id
self.password = password
self.server_url = service_url
self.server = xmlrpclib.ServerProxy(self.server_url)
self.categories = []
self.lastPostID = None;
self.getCategories()
def getCategories(self,refresh=False):
cats = self.server.metaWeblog.getCategories(self.blog_id,self.user_id,self.password)
if refresh or self.categories == []:
for elem in cats:
self.categories.append(elem['title'])
return cats
def getPost(self,post_id):
result = self.server.metaWeblog.getPost(post_id, self.user_id, self.password)
if result:
pass
return result
def getRecentPosts(self, count=1):
result = self.server.metaWeblog.getRecentPosts(self.blog_id, self.user_id, self.password, count)
if result:
self.lastPostID = result[len(result)-1]['postid']
return result
def newPost(self, aPost):
newPostID = self.server.metaWeblog.newPost(self.blog_id, self.user_id, self.password, aPost.struct(), aPost.publish)
self.lastPostID = newPostID
return newPostID
def editPost(self, aPost, post_id=None):
if post_id == None:
post_id = aPost.postid
result = self.server.metaWeblog.editPost(post_id, self.user_id, self.password, aPost.struct(), aPost.publish)
if result:
self.lastPostID = post_id
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment