#Python, #Linux, automate, #WordPress

I posted about #SocialMedia #EBook #Marketing Experiment where I decided what my marketing campaign should do. Requirements are clear, goal is clear and it just needs to be done.

Sounds like a project.

I have my requirements. First thing what I did was to take a look what I have… couple of home computers, a Linux server hosting this site (and skills to maintain it). I know about programming (mainly Java, some Python and not so much PHP or Perl). And of course, I have Google to search anything related to automatic posting to WordPress. That should be enough to implement the project.

The next step was to decide where to run the solution. From what I have, I could use my home computer to post to blog. But that’s not good and even if I had automated blog posting, home computer is not always powered on. So it’s best to use hosted virtual Linux server. Added benefit for Linux is cron, capability to schedule automatic posts, and so  requirement for automation is covered.

Much more difficult was to decide how to post blog entries automatically. Fortunately, WordPress has XML-RPC interface. But, unfortunately, documentation was not so good. WordPress supports several XML-RPC APIs for blogging such as metaWeblog API (eventually I chose this API).

The documentation of metaWeblog API is not so clear and I had to read it couple of times before I really under understood the functions of the API.

Unfortunately again, examples of how to use WordPress XML-RPC API were altogether very limited. After some googling I found Automatic Post Creation with WordPress, PHP, and XML-RPC and Command Line Blogging – WordPress. Both were very good and showed me how to use WordPress XML-RPC and that automatic posting can be done. But… both were using PHP. I didn’t want to use PHP.

After more googling, I found Perl-library for WordPress XML-RPC. Looks good, but I didn’t want to use Perl.

Even more googling, and I found wordpress-java, a Java client library to interact with the xmlrpc interface of wordpress. This also looked very good and this would have been my choice. But, unfortunately, hosting provider doesn’t support Java in their Linux servers.

More googling… and I found Python xmlrpclib. Python lib is not related to WordPress at all so I went back to metaWeblog API, and to blog posts Automatic Post Creation with WordPress, PHP, and XML-RPC and Command Line Blogging – WordPress.

Applying knowledge from the blogs and Python documentation, I made a short Python script that takes the last two lines of my novelette, posts them to blog along with some text and then saves the novelette minus the last two lines. Cron-job executes the script every second odd-day of the month at 10:17AM GMT and Twitterfeed notes the new blog entry a little later and then posts it to Twitter, Facebook and LinkedIn.

And so.. project done.

PS. Below is the Python script that sends a new blog post to WordPress.

#!/usr/local/bin/python

#This is part of marketing experiment, run via cron every odd day
#Publish to blog

import xmlrpclib
#maximum numer or blog posts
max=221

serverUrl='http://url.to.wordpress/xmlrpc.php'
userName='USER'
password='PASSWORD'

fileName="/path/to/file/endofanera.txt"
marketingUrl="http://more.info.url"

# Create an object to represent our server.;
server = xmlrpclib.Server(serverUrl);

#Get file contents and total lines
f = open(fileName)
lines = f.readlines()
f.close()
lastLine=lines[-1]
secondToLastLine=lines[-2]
totalLines = len(lines)
part=str(totalLines/2)+"/"+str(max)

#set blog entry content
category="The Strangers' Universe"
title="#SciFi "+part
content=secondToLastLine
content+="\n"
content+=lastLine
content+="\n"
content+="<!--more--> "
content+="\n"
content+="\n"
content+="<p>================================================================</p>\n"
content+="\n"
content+="<p>Part "+part+" of <a href='http://www.smashwords.com/books/view/31150'>End of an Era</a>, my science fiction novelette, set in the Strangers' Universe.</p>"
content+="<p>Stories from the Strangers' Universe are:</p>"
content+="<ul>"
content+="<li><a href='http://www.smashwords.com/books/view/31150'>End of an Era</a>, <i>Long ago, the Hwzeenn Empire, the oldest civilization in the Milky Way, is going into battle against an enemy known as the Strangers. They expect an easy victory, but the Hwzeenn do not know the enemy. And those who do know better have decided not to warn the Hwzeenn; a decision that will have grave consequences for the Hwzeenn Empire.</i></li>"
content+="<li> <a href='http://www.smashwords.com/books/view/110000'>Truths, Half-Truths and Lies</a>, <i>In early 1947, a long-awaited message finally comes and the meeting between Commander Johnson and aliens, the Greys, is scheduled in Roswell, New Mexico. The meeting, and the deal made there, are the first steps on a journey that takes the Commander through the great conspiracy, over a thousand years to unified Earth, to distant worlds and through the first portents of a great war.</i></li>"
content+="<li><a href='http://www.smashwords.com/books/view/30897'>Dawn to Dawn</a>, <i>'Good morning! Today is Tuesday, May 27th, 2927.' Nick, a mineworker in the mining station KCap16 in the Kuiper Belt, has another day ahead of him. He's going to work on the asteroid field during the day and then spend his evening in the pub - like he does every day. He couldn't know it, but his life would change forever before the next dawn.</i></li>"
content+="<li><a href='http://www.smashwords.com/books/view/14403'>Just Another Scout</a>, <i>It is five years after the Revelation. A lone scout ship with a crew of five is on a routine mission to explore habitable planets for colonization. The routine is about to end when the ship enters a seemingly normal system and discovers an unidentified radio signal from one of the planets.</i></li>"
content+="</ul>"
content+="\n"
content+="<p>This post is part of <a href='%s'>social media marketing experiment</a>.</p>" % (marketingUrl)
publish=True

postData={'title':title,
'description': content,
'categories':[category] }

result = server.metaWeblog.newPost(1, userName,password,postData,publish)
f = open(fileName,'w')
f.writelines(lines[:-2])
f.close()

print "Part %s posted. Post URL: http://sami.salkosuo.net/?p=%s" % (part,result)

(edit 9.5.2012: changed post frequency to every second odd-day)

1 thought on “#Python, #Linux, automate, #WordPress”

Leave a Reply

Your email address will not be published. Required fields are marked *