topical media & game development

talk show tell print

mashup-delicious-07-delicious-summary2-index.php / php



  <?php
  
  //define the delicious account information we need
  define('DELICIOUS_USERNAME','username');
  define('DELICIOUS_PASSWORD','password');
  
  //mysql database information
  define('MYSQL_HOST','localhost');
  define('MYSQL_DATABASE','delicious_summary2');
  define('MYSQL_USERNAME','delicious_sql');
  define('MYSQL_PASSWORD','password');
  
  //bring in Snoopy goodness
  include('lib/Snoopy.class.php');
  //get rid of error Notices because Snoopy produces some
  error_reporting(E_ERROR | E_WARNING | E_PARSE);
  
  //also bring in the del.icio.us Mashups PHP library we made
  include('lib/delicious_mashups.inc.php');
  
  //bring in Sajax goodness
  require("lib/Sajax.php");
  
  //create the MySQL Database Connection
  link = mysql_connect(MYSQL_HOST,MYSQL_USERNAME,MYSQL_PASSWORD)
      or die(mysql_error());
  @mysql_select_db(MYSQL_DATABASE) or die(mysql_error());
  
  //first let’s fill linkroll_data
  linkroll_data = array();
  //start by seeing if the first (and only) row in the linkroll table has a 
  //time_retrieved that is in the last hour   
  linkroll_cache_query = "select count(*) from linkroll where time_retrieved > date_sub(now(), interval 1 hour)";
  linkroll_cache_result = mysql_query(linkroll_cache_query);
  //if a count was returned, we should use cache and NOT make an api call
  if (mysql_result(linkroll_cache_result,0) >= 1) {
      //use the linkroll cache
      linkroll_data = get_last_cached_data('linkroll');
  } else {
      //no cache so time to update our cache with a new data fetched from the api
      do_api_call('/posts/recent?','count=20');
      //if status is 200 then create the cache and use api_return for our data.
      //otherwise, fetch the cache data instead.
      if (api_return['status'] == 200)
      {
          add_cache_query = sprintf(
              "insert into linkroll (time_retrieved,serialized_data_array) values (now(),'\%s')",
              mysql_real_escape_string(serialize(api_return))
              );
          mysql_query(add_cache_query) or die(mysql_error());
          linkroll_data = api_return;
      } else {
          //otherwise, force using the cache
          linkroll_data = get_last_cached_data('linkroll');
      }
  }
  
  //next let’s fill tagroll_data with the tags for this user for our tagroll 
  //using the API
  tagroll_data = array();
  //start by seeing if the first (and only) row in the tagroll table has a
  // time_retrieved that is in the last hour   
  tagroll_cache_query = "select count(*) from tagroll where time_retrieved > 
  date_sub(now(), interval 1 hour)";
  tagroll_cache_result = mysql_query(tagroll_cache_query);
  //if a count was returned, we should use cache and NOT make an api call
  if (mysql_result(tagroll_cache_result,0) >= 1) {
      //use the tagroll cache
      tagroll_data = get_last_cached_data('tagroll');
  } else {
      //no cache so time to update our cache with a new data fetched from the api
      do_api_call('/tags/get');
      //if status is 200 then create the cache and use api_return for our data.
      //otherwise, fetch the cache data instead.
      if (api_return['status'] == 200)
      {
          add_cache_query = sprintf(
              "insert into tagroll (time_retrieved,serialized_data_array) values 
              (now(),'\%s')",
              mysql_real_escape_string(serialize(api_return))
              );
          mysql_query(add_cache_query) or die(mysql_error());
          tagroll_data = api_return;
      } else {
          //otherwise, force using the cache
          tagroll_data = get_last_cached_data('tagroll');
      }
  }
  
  //this function will grab the links for a particular tag, whether from
  //cache or a del.icio.us API call
  function get_tag_linkroll(tag) {
      global api_return;
      
      //tag_linkroll_data with be filled with the 20 latest posted bookmarks
      //under tag
      tag_linkroll_data = array();
      //check for cache like with the normal linkroll but also query on the tag name
      tag_linkroll_cache_query = "select * from tag_linkroll where time_retrieved > date_sub(now(), interval 1 hour) and tag='".mysql_real_escape_string(tag)."'";
      tag_linkroll_cache_result = mysql_query(tag_linkroll_cache_query);
      
      //if a row was returned, we should use cache and NOT make an api call
      if (mysql_num_rows(tag_linkroll_cache_result) >= 1) {
          //use the tag linkroll cache
          tag_linkroll_data = get_last_cached_data('tag_linkroll',tag);
      } else {
          //no cache so time to update our cache with a new data fetched from the api
          do_api_call('/posts/recent?',"count=20&tag=".tag);
          //if status is 200 then create the cache and use api_return for our data.
          //otherwise, fetch the cache data instead.        
          if (api_return['status'] == 200)
          {
              add_cache_query = sprintf(
                  "insert into tag_linkroll 
                     (time_retrieved,serialized_data_array,tag)
                     values (now(),'\%s','\%s')",
                  mysql_real_escape_string(serialize(api_return)),
                  mysql_real_escape_string(tag)
                  );
              mysql_query(add_cache_query) or die(mysql_error());
              tag_linkroll_data = api_return;
          } else {
              //otherwise, force using the cache
              tag_linkroll_data = get_last_cached_data('tag_linkroll',tag);
          }
      }
      //return the list with proper html formatting
      posts = tag_linkroll_data[0]['children'];
      html = "<em>selected tag: tag</em>";
      html .= "<ul>";
      foreach (posts as post)
      {
          html .= "<li><a href=\"".post['attributes']['HREF']."\">".
          post['attributes']['DESCRIPTION']."</a></li>";
          if (isset(post['attributes']['EXTENDED']))
              html .= "<p>".post['attributes']['EXTENDED']."</p>";
      }
      html .= "</ul>";
      return html;
  }
  
  //this shared function fetches the last cached data for a given table
  function get_last_cached_data(table,tag = '')
  {
      //grab the newest cached item from table
      if (tag != '') {
          query = "select serialized_data_array from table where tag =  '".mysql_real_escape_string(tag)."' order by time_retrieved desc limit 1";
      } else {
          query = "select serialized_data_array from table order by time_retrieved desc limit 1";
      }
      result = mysql_query(query);
      row = mysql_fetch_row(result);
      //unserialize the data array and return it
      return unserialize(row[0]);
  }
  
  //initalize sajax and export the get_tag_linkroll function
  sajax_init();
  sajax_export("get_tag_linkroll");
  sajax_handle_client_request();
  
  ?>
  </?xml version="1.0" encoding="UTF-8"/?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
  <title><?php echo DELICIOUS_USERNAME; ?>'s del.icio.us Summary Page</title>
  <link href="stylesheet.css" rel="stylesheet" type="text/css" />
  <script>
  <?php
  //this brings in all the javascript SAJAX needs to do its thing
  sajax_show_javascript();
  ?>
  //called when you click a tag in the tag-cloud
  function get_tag_linkroll(tag) {
             document.getElementById("tag_linkroll").innerHTML = "<em>Fetching linkroll. Please wait....</em>";
             x_get_tag_linkroll(tag, get_tag_linkroll_cb);
  }
  //x_get_tag_linkroll callback
  function get_tag_linkroll_cb(result) {
      document.getElementById("tag_linkroll").innerHTML = result;
  }
  </head>
  <body>
      
  <h1 id="header"><?php echo DELICIOUS_USERNAME; ?>'s del.icio.us Summary Page</h1>
  
  <div id="center_col">
  <div class="col_banner">My Tags</div>
  <div style="text-align: justify">
  <?php
      //pull all of the tags and their count out of the data array first
      //and put it into a k => v array, where v = an array of all tags w/ that count
      tag_counts = array();
      tags = tagroll_data[0]['children'];
      foreach (tags as tag)
          tag_counts[tag['attributes']['COUNT']][] = tag['attributes']['TAG'];
      //sort tags by counts
      ksort(tag_counts);
      //remove groups of tags from tag_counts with a count of 3 or below
      for (i = 1; i <= 3; i++) {
          if (isset(tag_counts["i"]))
              unset(tag_counts["i"]);
      }
      //get the smallest count going
      smallest_count = array_shift(array_keys(tag_counts));
      //get the largest count going
      largest_count = array_pop(array_keys(tag_counts));
      //determine the difference between them
      count_difference = largest_count - smallest_count;
      count_distribution = count_difference / 3; //3 different sub-sizes
      //create a new array of all tag names, sorted, as keys with counts as values
      tag_values = array();
      foreach (tag_counts as count => tags_in_count){
          foreach (tags_in_count as tag_name)
              tag_values[tag_name] = count;
      }
      ksort(tag_values);
      //now go through tag_values and output them with the according classes
      foreach (tag_values as tag_name => tag_count)
      {
          if (tag_count == largest_count)
              echo "<span class=\"largest_tag\"> ";
          elseif (tag_count == smallest_count)
              echo "<span class=\"smallest_tag\"> ";
          elseif (tag_count > smallest_count + (count_distribution * 2))
              echo "<span class=\"large_tag\"> ";
          elseif (tag_count > smallest_count + count_distribution)
              echo "<span class=\"medium_tag\"> ";
          else
              echo "<span class=\"small_tag\"> ";
          echo "<a href=\"#\" onClick=\"get_tag_linkroll('".tag_name."'); return false;\">".tag_name."</a>";
          echo " </span>";
      }
  ?>
  </div>
  </div>
  
  <div id="left_col">
  <div class="col_banner">My Latest Bookmarks</div>
  <?php
      //list recent posts (linkroll)
      posts = linkroll_data[0]['children'];
      echo "<ul>";
      foreach (posts as post)
      {
          echo "<li><a href=\"".post['attributes']['HREF']."\">".post['attributes']['DESCRIPTION']."</a></li>";
          if (isset(post['attributes']['EXTENDED']))
              echo "<p>".post['attributes']['EXTENDED']."</p>";
      }
      echo "</ul>";
  ?>
  </div>
  
  <div id="right_col">
      <div class="col_banner">Browse by Tag</div>
      <div id="tag_linkroll">
          <p>&laquo; Click a tag in the column to the left to view my bookmarks for
          it here.</p>
      </div>
  </div>
  
  </body>
  </html>
  <?php
  //close the database connection since we won't need it past this point
  mysql_close(link);                                                   
  ?>


(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.