Archive for the ‘curl’ tag
Schedule Twitter Posts
Many people (myself included) often post an update to Twitter when publishing a new blog post. WordPress (the blog software I use) and most other blogging platforms include a nice feature that allows you to schedule blog posts to be published at a particular date/time. Wouldn’t it be great if we could also schedule our tweets so that they are posted when our blog post is published? There are countless other potential uses for scheduled tweets. Maybe you’re launching a new product and want to announce it on Twitter on Monday morning at 9am, but are afraid you’ll get caught up on something else – set an automated Tweet and away you go.
Before I begin, I’ll start off by saying that this is a very simple example of scheduling tweets. It will give you a very simple introduction to the Twitter API, the curl tool and the Windows Task Scheduler. There is no pretty graphical user interface, and configuring your scheduled tweets is fairly manual to say the least. If you would like more features, why not incorporate them into your own little application? I’m certainly thinking of doing so. Also – I believe there is a WordPress plugin that will post a tweet when your blog post is published – if this fits your needs, great! Again – this is merely a simple example of what you can do with the Twitter API, it’s not intended to be the holy grail solution for scheduling tweets!
For the sake of this tutorial I am going to assume you’re using the Windows operating system. If you’re using Mac OS, Linux or another Unix variant, the technique used in this tutorial could be easily amended to work with cron. If enough people demand a Mac/Linux version of this tutorial, I’ll write one.
The first thing you’re going to need to do is download cURL. This command-line utility allows you to transfer files over a host of internet protocols, and is perfect for interacting with the Twitter API. It is free and open source, and is available for a wide range of platforms. Visit the cURL download page and download the No-SSL version provided by Daniel Stenburg. At the time of writing, the latest version available was 7.19.4. Once you have downloaded the ZIP file, extract the contents to c:\curl. If you need an archiving utility, download the excellent 7-Zip. That’s all there is to installing cURL! To test it out, open a command prompt (Start->Programs->Accessories->Command Prompt) and change to the curl directory (cd \curl). Enter the following command:
curl http://www.google.ie
Listing 1 – cURL’ing Google.ie
This should spit back a ream of continuous HTML code. This is the HTML source for the Google.ie home page. What you see should be similar to the screenshot below:
Now that we have verified that cURL is working, let’s see how we can use it to interact with the Twitter API. The Twitter API is what is known as a RESTful web service, meaning that it can be queryed using a URI, and will return data in a particular MIME type. Twitter can return data in the XML, JSON, RSS or Atom MIME types. Let’s start working with Twitter by retrieving the Public Timeline in RSS format. In your command prompt, issue the following command:
curl http://twitter.com/statuses/public_timeline.rss
Listing 2 – cURL’ing the Twitter Public Timeline
This should bring back the RSS feed for the Twitter Public Timeline, as seen in the following screenshot:
Great – but we’re not very interested in the public timeline are we? Let’s tell Twitter who we are and ask for the timeline of only the people we are following. This time, we will ask for a response in the JSON (JavaScript Object Notation) format. Please be sure to substitute your own Twitter username and password for the values below. And no, my Twitter password is not “password”!
curl -u joelennon:password http://twitter.com/statuses/friend_timeline.json
Listing 3 – cURL’ing our Friends’ Timeline
In this example, we are using HTTP authentication to tell Twitter who we are, and asking it to return a JSON representation of our friends’ timeline. For a sample of the output, see Figure 3 below:
At this point, you may be wondering what use all of the mumbo-jumbo we are receiving as a response is to us. In fact, in this tutorial, it’s not much use to us at all as we are only interested in updating Twitter. If we wanted to display our own, our friends or public tweets however, we would be able to parse this information and display it in a readable format. The Twitter API provides an array of methods for pulling back information like this, including:
- public_timeline
- friends_timeline
- user_timeline
- show
- replies
- friends
- followers
- many more…
If you would like to delve deeper into these methods, and the Twitter API in general, check out the REST API Documentation on the Twitter API Wiki. It has a host of information and examples on how to use the Twitter API. Now let’s get back to the tutorial!
Up until this point, we have concentrated mainly on retrieving data from Twitter. But we want to send updates to Twitter! Luckily, cURL allows us to neatly send POST data along with our HTTP request. To post an update, issue the following command in your command prompt:
curl -u joelennon:password -d status="Testing out using cURL for Twitter updates" http://twitter.com/statuses/update.xml
Listing 4 – Update Twitter with CURL
As previously, you will receive a response in the format you specified in the request (in this case, XML). This should look similar to the screenshot below:
But, more importantly, if you check out your Twitter page, you will see that your timeline has been updated with a new post – you guessed it, the one we just sent. To change the content of the post, simply change the text between the quote symbols in Listing 4 above. I think you’ll agree, using the Twitter API with curl is pretty easy! Now let’s take things a step further and create a script that will post the update for us, so that we don’t have to issue the long curl command every time we want to post.
Open a text editor (Notepad will do fine) and add the command from Listing 4 above to it. Feel free to change the status text to something else! Now go to File -> Save As and save it as “twitter.bat” in the C:\curl directory. Be sure to include the quotes when you are saving the file, otherwise Notepad will probably try to save it as .txt file, and you’ll end up with a file named twitter.bat.txt instead!
Now go back to your command prompt and ensure that you have changed in to the C:\curl directory. Enter the following command:
twitter.bat
Listing 5 – Running our Twitter script
Hey presto, your Twitter update has been posted! While this is nice and short, it’s still a bit of a pain as we have to modify the twitter.bat file everytime to change the status text. Let’s fix that. Re-open twitter.bat in Notepad, and change the contents to the following:
@ECHO OFF SET STATUS=%* c:\curl\curl.exe -u joelennon:password -d status="%STATUS%" http://twitter.com/statuses/update.xml
Listing 6 – Update twitter.bat file
We have changed the batch file so that it does not display the command each time it runs, and it sets a variable, STATUS to all the arguments entered when the command is executed. We then use this STATUS variable in our curl command instead of static text. This allows us to enter the status text we wish to update Twitter with when we run our script. This time, try running the command in Listing 7 below:
twitter.bat Passing arguments to the batch file
Listing 7 – Running our updated script
Well would you look at that, it’s submitted the Twitter update using the text we specified after the twitter.bat command! Take a step back and look at what you’ve just created – a Twitter updater client! Sure it’s basic, but it works! Now that we have the script to post our Twitter updates created, let’s look at how we can schedule it to automatically post an update at a specified date and time.
A feature of the Windows operating system that is often overlooked is the Task Scheduler. This allows you to create scheduled tasks that will run on a certain date and time. This tool has a GUI interface and a command-line interface for scheduling tasks, but it can be quite complex. Instead, we are going to use the at command, which is included with any NT-based Windows platform (NT/2000/XP/Vista/Server). This is a very basic and simple to use command-line scheduler.
I am writing this particular paragraph at 2:26pm. The line below will automatically send a tweet at 2:27pm. Change the time to the 24-hour value for a time that’s a few minutes into the future for you (otherwise it will send the next time 2:27pm comes around, and you might have to wait a long time to test it worked!)
at 14:27 cmd /c c:\curl\twitter.bat This is a scheduled tweet!
Listing 8 – Scheduling a tweet
The at command is quite simple, but it has some nice features that allow you to schedule tasks to run at set intervals – for example every Monday at 9.00am. For further information, see this Microsoft Knowledgebase article on the at command. Note in the above example that we preceded our twitter script file with the command “cmd /c”. This might not mean anything to you, but it is required in order for the script to work, as we are running a batch file and not a regular executable. This basically tells the scheduler to start a command window, run the specified command and close the command window when done.
Congratulations, you now have a way of automating your tweets so that they are posted at a set date/time or even at a regular interval. It’s not the prettiest solution in the world, but it’s simple and you have probably learned a bit about the Twitter API (and batch files and the scheduling tasks!) in the process. If you have any questions or need some help with this, feel free to leave a comment and I’ll do my best to help you out!



