topical media & game development

talk show tell print

student-twitter-tweets.php / php



  <?php
          noOfFriends = 6;
          numTweets = 21;
          
          //include("stopwords.incl.php");
          stopwords = Array(        "i",        "a",        "about","an",        "and",        "are",        "as",        "at",        "be",        "by",        
                                  "for",        "from",        "how",        "in",        "is",        "it",        "la",        "of",        "on",        "or", 
                                  "that",        "the",         "this",        "to",         "was",         "what",        "when",        "where","who",         "will",
                                  "with",        "under"        );
          
          function printForUse (url) {
                  global stopwords;
                  
                  lines = file(url);
                  foreach(lines as line) {
                          if (        strstr(line, "<status>") ||
                                  strstr(line, "</status>") ||
                                  strstr(line, "<created_at>") ||
                                  strstr(line, "<id>") ||
                                  strstr(line, "<text>") ||
                                  strstr(line, "user>") ||
                                  strstr(line, "<screen_name>") ||
                                  strstr(line, "<profile_image_url>") ) {
                                  echo(line);
                          }
                          if ( strstr(line, "<text>") ) {
                                  echo("\t<keywords>\n");
                                  line = str_replace(array("<text>", "</text>", ".", ",", ";", ":", "?", "!", "(", ")", "&quot"), "", strtolower(line));
                                  
                                  keywords = Array();
                                  foreach(explode(" ", trim(line)) as word) {
                                          if (!in_array(word, stopwords) && !in_array(word, keywords) && word != "" && !strstr(word, "http")) {
                                                  keywords[] = word;
                                                  echo("\t\t<keyword>word</keyword>\n");
                                          }
                                  }
                                  
                                  echo("\t</keywords>\n");
                          }
                  }
          }
          
          function getFriends (username, max) {
                  lines = file("http://twitter.com/$username");
                  
                  for(i=0; i < count(lines); i++) {
                          if (strstr(lines[i], 'class="vcard">')) {
                                  fragments = explode("http://twitter.com/", lines[i+1]);
                                  fragments = explode("\" class=\"url\"", fragments[1]);
                                  results[] = fragments[0];
                          }
                          if (count(results) == max) {
                                  return results;
                          }
                  }
                  return results;
          }
          
          function getMessages (username) {
                  global numTweets, noOfFriends;
                  
                  users = getFriends(username, noOfFriends);
                  users[] = username;
                  
                  max = floor(numTweets / count(users));
                  
                  foreach(users as user) {
                          printForUse( "http://twitter.com/statuses/user_timeline/$user.xml?count=$max" );
                  }
          }
          
          header("Content-type: xml");
          
          echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
          echo("<statuses type=\"array\">");
          
          if (isset(_GET["user"]) && _GET["user"] == "all") {
                  printForUse( "http://twitter.com/statuses/public_timeline.xml");
          } else if (isset(_GET["user"]) && _GET["user"] != "default") {
                  getMessages(_GET["user"]);
          } else {
                  printForUse( "student-twitter-default.xml" );
          }
          
          echo("</statuses>");
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.