twitter-follow-achieverI 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.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related Posts

5 Responses to “Code a Twitter Auto Post Script for WordPress Blogs in Python”
  1. Pranab says:

    I don’t see how this can be used in a cron – won’t it just re-tweet posts it has tweeted before?

    It needs a way to mark which posts have been tweeted already!

  2. Spyros Panagiotopoulos says:

    What this does is post a random tweet each time. It does not just tweet for a single post once. It randomly selects a wordpress post and creates a tweet about it. Of course, some tweets will be the same, depending on the total number of posts in your blog.

    However, this can easily be fixed, by simply marking what was posted, as you said.

  3. James Foy says:

    I’m just starting to learn Python and Linux. I’ve been a computer user for years, but want more control of what I’m doing.

    Could you use this for blogspot also? I think it’s amazing this can be done in Python with 55 lines of code.

    Thanks

  4. Spyros Panagiotopoulos says:

    Hey, sorry for the late reply.

    Yes, this can be done for blogspot as well, but you would need a different python library, or you could always do it manually by means of python urllib2. Check this :

    http://www.codercaste.com/2009/11/28/how-to-use-the-urllib-python-library-to-fetch-url-data-and-more/

  5. Web Reviews says:

    Thanks for the code.. Hope I can figure out rest of it to integrate in my site

  6.  
Leave a Reply