Code a Twitter Auto Post Script for WordPress Blogs in Python
I can bet that quite some of you use Twitter often, while some others use it all the time. It’s a quite addicting website, at least for the first few months of its usage. When i first registered at Twitter and started learning more about it, i thought that it would be a good idea to use it in order to leave short automated tweets about the posts on my WordPress blog (this was actually about another website, not codercaste).
This actually made me code a python script to automate this process. To allow it to work, you would have to enable XML-RPC through your admin panel at Settings->Writing for WordPress. Let’s see the actual code :
#!/usr/bin/env python import wordpresslib import random import urllib import twitter wordpress = 'http://yourwebsitedomain/xmlrpc.php' user = 'your_username' # admin by default password = 'your_password' def tiny_url(url): apiurl = "http://tinyurl.com/api-create.php?url=" tinyurl = urllib.urlopen(apiurl + url).read() return tinyurl def content_tiny_url(content): regex_url = r'http:\/\/([\w.]+\/?)\S*' for match in re.finditer(regex_url, content): url = match.group(0) content = content.replace(url,tiny_url(url)) return content # prepare client object wp = wordpresslib.WordPressClient(wordpress, user, password) Tweet = '' postList =  posts = wp.getRecentPosts(numPosts=1000) for post in posts: postList.append(post) postNumber = random.randint(1, len(postList)) print postNumber counter = 1 for post in postList: if counter == postNumber: Tweet += post.title + " " Tweet += tiny_url(post.permaLink) break counter += 1 print Tweet myUsername = 'twitterUsername' loginPassword = 'twitterPassword' api = twitter.Api(username = myUsername, password = loginPassword) api.PostUpdate(Tweet) print "Tweet Posted Successfully !"
This script uses 2 external libraries to handle WordPress and Twitter, called wordpresslib and python twitter. Before executing this script, you need to install them. It also uses the integral python urllib and random libs. The first is used to handle the http requests and the other one is used for selecting a random post to tweet about.
This script is very straightforward. First of all, a WordPressClient object is initialized and provided with our login administrator credentials for WordPress. Then, we call the getRecentPosts() to get the latest 1000 posts of our blog (we most possibly have much less than that but it is a value that you can change). We create a list that holds all the posts information and randomly select one of them (i know it can be done in a faster way but there is no real need to make it run fast, it’s just a script running once or twice per hour).
After the selection of our post, we get the important information about it, like the title and it’s permalink. We provide the permalink to tinyurl so that it forms a smaller url for us and we now have our tiny url ready. Lastly, we just print the tweet to the standard output (so that we know what was outputted to Twitter) and then we use the python Twitter API to login to our account and post the actual tweet.
Automating the Script
At this point, you should of course be thinking of using cron to automate the process of running the script hourly or whenever you like. I will that as a practice for you. If you’re not familiar with cron though, you can easily find the answer if you read this post about how to use the unix cron tool.