python - wordpresslib attaching images to posts -
looking @ example provided wordpresslib, straight forward on how upload images media library. however, attachment of images looks never finished. has attached images?
#!/usr/bin/env python """ small example script publish post jpeg image """ # import library import wordpresslib print 'example of posting.' print url = raw_input('wordpress url (xmlrpc.php added):') user = raw_input('username:') password = raw_input('password:') # prepare client object wp = wordpresslib.wordpressclient(url+"xmlrpc.php", user, password) # select blog id wp.selectblog(0) # upload image post # imagesrc = wp.newmediaobject('python.jpg') # fixme if imagesrc: # create post object post = wordpresslib.wordpresspost() post.title = 'test post' post.description = ''' python best programming language in earth ! no image broken fixme <img src="" /> ''' #post.categories = (wp.getcategoryidfromname('python'),) # add tags post.tags = ["python", "snake"] # not publish post idnewpost = wp.newpost(post, false) print print 'posting successfull! (post has not been published though)'
wordpresspost class:
class wordpresspost: """represents post item """ def __init__(self): self.id = 0 self.title = '' self.date = none self.permalink = '' self.description = '' self.textmore = '' self.excerpt = '' self.link = '' self.categories = [] self.user = '' self.allowpings = false self.allowcomments = false self.tags = [] self.customfields = [] def addcustomfield(self, key, value): kv = {'key':key, 'value':value} self.customfields.append(kv)
wordpress saves images website.com/wp-content/uploads/year/month/filename
adding simple image tag above format in post.description
display image on post.
where year
current year 4 digit format (ex. 2015)
and month
current month leading 0 (ex. 01,02,... 12)
and filename
file name submitted via imagesrc = wp.newmediaobject('python.jpg')
example file name: website.com/wp-content/uploads/2015/06/image.jpg
here how posted image:
import time import wordpresslib import image datetime import datetime time = datetime.now() h = str(time.strftime('%h')) m = str(time.strftime('%m')) s = str(time.strftime('%s')) mo = str(time.strftime('%m')) yr = str(time.strftime('%y')) url = 'wordpressurl.xmlrpc.php' wp = wordpresslib.wordpressclient(url,'username','password') wp.selectblog(0) imagesrc = wp.newmediaobject('testimage'+h+m+s'.jpg') #used format if post images same name unlikely override eachother img = 'http://wordpressurl/wp-content/uploads/'+yr+'/'+mo+'/testimage'+h+m+s+'.jpg' post=wordpresslib.wordpresspost() post.title='title' post.description='<img src="'+img+'"/>' idpost=wp.newpost(post,true)
Comments
Post a Comment