<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,49 +1,107 @@
 &lt;?php
+
 /*
-    Thanks to Jonty (http://i.am.jonty.co.uk/) for this script.
+    Thanks to Jonty (http://jonty.co.uk/) for this script.
     We use it to see a feed of twitter search results for a given term in IRC.
 
     Needs php5.
 
-    Run like:  php twitter-feeder.php | nc -q0 localhost 12345
+    Run like:  php twitter-feed.php #twitters 'monkey' | nc -q0 localhost 12345
+    (Where #twitters is the channel to pipe to, and monkey is the search term)
+
+    If you want to filter things from the feed you can optionally add regexes 
+    to the end of the params.
 */
 
-    $originalUrl = &quot;?since_id=0&amp;q=last.fm&quot;; // &lt;-- term in there.
-    $mainUrl = &quot;http://search.twitter.com/search&quot;;
+    if (count($argv) &lt; 3) {
+        print &quot;Usage: twitter-feed.php '#channel' 'searchterm' [url filter regexp, [url-filter regexp, ...]]\n&quot;;
+        print &quot;(Quotes are important, remember # is a comment in bash!)\n&quot;;
+        exit;
+    }
+
+    array_shift($argv);
+    $channel = array_shift($argv);
+    $search = array_shift($argv);
+    $filters = $argv ? $argv : array();
+
+    print &quot;{$channel} Starting twitter.com search feed for {$search}...\n&quot;;
+
+    $originalUrl = &quot;?since_id=4028479400&amp;q=&quot;.urlencode($search);
+    $baseUrl = &quot;http://search.twitter.com/search&quot;;
+
+    $firstRun = true;
+    $refreshUrl = $originalUrl;
 
-    print &quot;#playdar Starting search.twitter.com feed...\n&quot;;
+    while (1) {
+        $source = file(&quot;{$baseUrl}{$refreshUrl}&amp;refresh=true&quot;);
+        if ($source) {
+            $json = json_decode(implode('', $source));
 
-    $firstRun = 1;
-    $nextUrl = $originalUrl;
-    while(1){
-        $pageContent = file($mainUrl.$nextUrl.'&amp;refresh=true');
-        if ($pageContent) {
-            $source = implode(&quot;&quot;, $pageContent);
-            if (preg_match(&quot;/\&quot;refresh_url\&quot;:\&quot;(.*?)\&quot;/&quot;, $source, $urlMatches)) {
-                $nextUrl = $urlMatches[1];
+            if ($json &amp;&amp; isset($json-&gt;refresh_url)) {
+                $refreshUrl = $json-&gt;refresh_url;
 
-                preg_match_all(&quot;/{\&quot;text\&quot;:\&quot;(.*?)\&quot;,\&quot;to_user_id\&quot;:.*?,\&quot;from_user\&quot;:\&quot;(.*?)\&quot;.*?}/sm&quot;, $source, $postMatches, PREG_SET_ORDER);
-                foreach($postMatches as $match){
-                    $message = cleanString($match[1]);
-                    if($firstRun == 0) print &quot;{$match[2]}: &quot;.$message.&quot;\n&quot;;
+                if ($firstRun) {
+                    $firstRun = false;
+                    continue;
                 }
-                if($firstRun == 1) $firstRun = 0;
+
+                foreach ($json-&gt;results as $result) {
+                    if ($message = processMessage($result-&gt;text, $filters)) {
+                        print &quot;{$result-&gt;from_user}: {$message}\n&quot;;
+                    }
+                }
+
             } else {
                 // On error, reset to front page and retry
-                $nextUrl = $originalUrl;
+                $refreshUrl = $originalUrl;
             }
         }
 
         sleep(60);
     }
 
-    function cleanString($string) {
+    function processMessage ($string, $filters) {
+        $string = utf8_decode($string);
+
+        // Resolve shortened URL's to the full thing for filtering
+        preg_match_all('/http:\/\/[^ $)]+/i', $string, $urls);
+        if ($urls) {
+            foreach ($urls[0] as $url) {
+                if ($redirect = getRedirect($url)) {
+                    $string = str_replace($url, $redirect, $string);
+                }
+            }
+        }
+
+        $string = cleanString($string);
+        if ($filters) {
+            foreach ($filters as $filter) {
+                if (preg_match(&quot;/{$filter}/i&quot;, $string)) {
+                    $string = '';
+                    break;
+                }
+            }
+        }
+
+        return $string;
+    }
+
+    function cleanString ($string) {
         $string = preg_replace('/\\\u([[:alnum:]]{4})/u', '?', $string);
         $string = str_replace('\/','/', urldecode($string));
         $string = html_entity_decode($string, null, 'UTF-8');
-        $string = htmlspecialchars_decode($string);
-        $string = utf8_decode($string);
-        return $string;
+        return htmlspecialchars_decode($string);
     }
 
-?&gt;
+    function getRedirect ($url) {
+        $curl = curl_init();
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($curl, CURLOPT_URL, $url);
+        curl_setopt($curl, CURLOPT_HEADER, true);
+        curl_setopt($curl, CURLOPT_NOBODY, true);
+        $headers = curl_exec($curl);
+        curl_close($curl);
+
+        preg_match('/Location:(.*?)[\r\n]/i', $headers, $redirect);
+        return $redirect ? urlencode(trim($redirect[1])) : '';
+    }</diff>
      <filename>examples/twitter-feed.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ce2e69c1469ec499a1b98ede6a472949ed2c9b24</id>
    </parent>
    <parent>
      <id>be5e6854b92fb4a37d87df1b151a8cb298c8e503</id>
    </parent>
  </parents>
  <author>
    <name>RJ</name>
    <email>rj@IS-2229.fordirectadmin.com.fordirectadmin.com</email>
  </author>
  <url>http://github.com/RJ/irccat/commit/fbb4df3a17e55d37ffc53081936ea68b70c85a68</url>
  <id>fbb4df3a17e55d37ffc53081936ea68b70c85a68</id>
  <committed-date>2009-10-09T07:55:54-07:00</committed-date>
  <authored-date>2009-10-09T07:55:54-07:00</authored-date>
  <message>Merge branch 'master' of git@github.com:RJ/irccat</message>
  <tree>7ed4e1b6577daec30ccf6cf38afe4392a3354c09</tree>
  <committer>
    <name>RJ</name>
    <email>rj@IS-2229.fordirectadmin.com.fordirectadmin.com</email>
  </committer>
</commit>
