In the previous post I showed a way to archive your tweets, in this post I do want to leverage the power of Twitter's search interface, now to instantly search through tweets from the command line.

You can receive each new post by subscribing via email or RSS.

featured image

I made a quick PHP script to query twitter search and check out who twitted what, when and from which source. Two advantages, 1 disadvantage:

  • I like working from CLI, it gives me options to filter the output and send it through pipes to other programs like sed/awk, lynx or even sendmail.
  • Sometimes you see a link go viral, but from which sources did users tweet? With this query you can easily see this.
  • One disadvantage, as stated in my last post is the short search span of twitter search: only 6-9 days.

The following script is the most basic form, you can easily expand this checking out GET search and Using the Twitter Search API. You might want to use getopt to let the script accept more command line options. Interesting additions might be : page and rpp for returning more results, since_id/ until to get a timeframe or zoom in on location with geocode.

A simple PHP script

   #!/usr/bin/php
   results as $item) {
     echo "$item->from_user_name ($item->from_user_id) ";
     echo "nt- tweeted: $item->text nt- via: $item->source";
     echo "nt- at: $item->created_at nn";
   }
   
   function getTweets($str) {
   	$url = "http://search.twitter.com/search.json?q=".urlencode($str);
     if(!$info = @file_get_contents($url, true)) {
   	  die("Not able to get twitter history for this stringn"); 
     }	
     $tweets = json_decode($info);
     return $tweets;
   }
   ?>

Pretty straightforward. Note that you could use yql but unfortunately it didn't always return results in my case. I found search.twitter to be more reliable.

The script is very easy, it just queries search.twitter, returns json, which we decode and loop. Use cases:

  • check who mentions my domain: $ php twitter_search.php bobbelderbos
  • or onother keyword: $ php twitter_search.php "kony 2012"
  • who is mentioning me?: $ php twitter_search.php @bbelderbos
  • with a hash (need to escape): $ php twitter_search.php #python
  • etc.

widget example

Now you can do all kinds of stuff with this data, like set up a cronjob to alert when certain patterns come by, mail yourself when you have a new mention, etc. (you can do this also in Twitter itself, but now you are in control :-)

You can build on top of this script as discussed earlier and pipe the results to powerful sed/awk/perl or whatever.

Raw data you get back:

   stdClass Object
   (
   ..
       [results] => Array
           (
               [0] => stdClass Object
                   (
                       [created_at] => Mon, 12 Mar 2012 17:20:11 +0000
                       [from_user] => KCITP
                       [from_user_id] => 80589393
                       [from_user_id_str] => 80589393
                       [from_user_name] => KC IT Professionals
                       [geo] => 
                       [id] => 179255478474383360
                       [id_str] => 179255478474383360
                       [iso_language_code] => en
                       [metadata] => stdClass Object
                           (
                               [result_type] => recent
                           )
   
                       [profile_image_url] => http://a0.twimg.com/profile_images/1143479908/kcit-twitter1_normal.jpg
                       [profile_image_url_https] => https://si0.twimg.com/profile_images/1143479908/kcit-twitter1_normal.jpg
                       [source][/source] => <a href="http://bufferapp.com" rel="nofollow">Buffer</a>
                       [text][/text] => How to push your code to your remote web server with Git  http://t.co/om2hAHEK
                       [to_user] => 
                       [to_user_id] => 
                       [to_user_id_str] => 
                       [to_user_name] => 
                   )


Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me