post blog remotely with wordpress-library

wordpress-library

# http://code.google.com/p/wordpress-library/source/checkout
import wordpresslib
import getopt
import sys
import re
import random

TITLE_RE = re.compile(r'title=(.*)',re.IGNORECASE)
TAGS_RE = re.compile(r'tags=(.*)',re.IGNORECASE)

def usage():
	print "Usage: python " + sys.argv[0] + " -w wordpress_url -u user -p password [-t title] -f file"

def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:], "w:u:p:t:f:")
	except getopt.GetoptError, err:
		# print help information and exit:
		print str(err) # will print something like "option -a not recognized"
		usage()
		sys.exit(2)

	user = passwd = title = file = url = tags = None

	for o,a in opts:
		if o == "-u":
			user = a
		elif o == "-p":
			passwd = a
		elif o == "-t":
			title = a
		elif o == "-f":
			file = a
		elif o == "-w":
			url = a
		else:
			assert False, ("unhandled option '%s'" % o)

	if not user or not passwd or not url or not file:
		usage()
		sys.exit(3)

	fh = open(file)
	flines = fh.readlines()
	fh.close()

	if not flines:
		print 'error: nothing to post'
		sys.exit(4)

	match = TITLE_RE.search(flines[0])
	if match:
		title = match.group(1)
		flines = flines[1:] # skip line
	
	match = TAGS_RE.search(flines[0])
	if match:
		tags = match.group(1)
		flines = flines[1:]
	
	if not flines:
		print 'error: nothing post body found'
		sys.exit(5)

	body = ''.join(flines)

	wp = wordpresslib.WordPressClient(url, user, passwd)

	# select blog id
	wp.selectBlog(0)

	post = wordpresslib.WordPressPost()
	post.title = title or ('untitled-' + str(random.randint(500, 50001)))
	post.tags = tags or ''
	post.description = body 
	#post.categories = (wp.getCategoryIdFromName('Python'),)

	# pubblish post
	idNewPost = wp.newPost(post, True) # False

	print 'posting successful', idNewPost

if __name__ == '__main__':
  main()

sample blog post


title=Test1
tags=testing,xmlrpc,wordpresslib

<span style="background-color:yellow">testing 123</span>
<div style="margin-left:10%;margin-right:10%">hello again</div>
<ol>
<li>one</li>
<li>two</li>
</ol>
<ui>
<li>a point</li>
<li>another point</li>
</ui>
<strong>very bold</strong>
<span style=’color:red’>and this is red</span>
and a table
<table border="1" style="margin:2%">
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
</table>

<div>new line</div>


editplus cliptext file for writing wordpress blog
wordpress.ctl

happey blogging with editplus

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s