Juicy lil' bytes

How to Pipe Twitter Data to Other Applications With Tweetledee

Tweetledee is a MIT licensed PHP project on GitHub that simplifies access to Twitter JSON and RSS feed data for user timelines, home timelines, lists, favorites, and tweet search. You can run the PHP files directly through the PHP interpreter using standard CLI syntax, take advantage of the many available options through command line switches, and pipe the Twitter data to any application that you’d like. Here’s how you do it.

The Software

How to Run Tweetledee from the Command Line

You can access Twitter JSON and RSS feed data through the Tweetledee files with standard command line syntax and display your results in a terminal or pipe the data through the standard out stream to any other application. Instead of running the Tweetledee scripts via URL on a web server, you run the them directly through the PHP interpreter. The Tweetledee files automatically detect whether you are accessing the data by URL or from a terminal window.

The Tweetledee scripts have a number of available options that allow you to modify the user of interest, exclude retweets or replies, define your search query and more. These parameter names do not change with the CLI approach; however, you use standard CLI syntax to define them. For single character options, use a short switch format (-c). For multiple character options, use a long switch format (--user).

Here are a few examples. Download the Tweetledee files from the GitHub site, navigate to the inner tweetledee directory and then test the following commands that demonstrate the options available for the user timeline JSON file.

User Timeline JSON (default user = you)

php userjson.php

User Timeline JSON (another user = me)

php userjson.php --user=csimpkins

User Timeline JSON (another user + exclude RT’s)

php userjson.php --user=csimpkins --xrt=1

User Timeline JSON (another user + exclude RT’s + include only 5 tweets)

php userjson.php --user=csimpkins --xrt=1 -c 5

Pipe the Twitter Data to Another Application

Viewing the data in your terminal is not terribly exciting. The beauty of this approach is that the JSON data travels through the standard out stream when you run a Tweetledee script directly through the PHP interpreter so you can pipe it to any application that accepts data through the standard in stream. This provides an easily implemented Twitter JSON (or Atom feed) data source for any application without the hassle of generating the OAuth code yourself.

As a trivial, relatively useless demonstration of the approach, let’s pipe the user timeline JSON to the *nix application wc to perform a character count.

Twitter User JSON Data Piped to wc for a Character Count

php userjson.php | wc -c

Simple. I leave it as an exercise for the interested reader to come up with a much more useful application of your Twitter data.