topical media & game development

talk show tell print

mashup-delicious-10-freshtags-index.php / php



  <?php
  
  //mysql database information
  define('MYSQL_HOST','localhost');
  define('MYSQL_DATABASE','freshtags');
  define('MYSQL_USERNAME','delicious_sql');
  define('MYSQL_PASSWORD','password');
  
  //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());
  
  //bring in magpie goodness
  require_once('lib/rss_fetch.inc');
  
  //set cache directory
  define('MAGPIE_CACHE_DIR', 'cache');
  
  //get rid of error Notices because magpie produces some
  error_reporting(E_ERROR | E_WARNING | E_PARSE);
  
  //number of items in a re-generated feed
  define('NUM_RSS_ITEMS',30);
  
  //base tag feed url
  define('BASE_TAG_FEED_URL','http://del.icio.us/rss/tag/');
  
  //check to see if _GET['tag'] exits (and if not the script dies)
  if (isset(_GET['tag'])) {
  //assemble complete feed url
  feed_url = BASE_TAG_FEED_URL._GET['tag'];
  //grab the feed
  rss = fetch_rss(feed_url);
  //go through each feed item by link and see if they exist already
  //in the database. if not, add the items
  foreach (rss->items as item) {
      //exists already?
      link_exists_check = "select count(*) from bookmarks where link LIKE '".mysql_real_escape_string(item['link'])."' and tag LIKE '".mysql_real_escape_string(_GET['tag'])."'";
      link_exists_check_result = mysql_query(link_exists_check) or die(mysql_error());
      //if no rows were returned then add the link to the database
      if (!mysql_result(link_exists_check_result,0)) {
          bookmark_insert_query ="insert into bookmarks (
              tag,
              link,
              title,
              description,
              dc_date
          ) values (
              '".mysql_real_escape_string(_GET['tag'])."',
              '".mysql_real_escape_string(item['link'])."',
              '".mysql_real_escape_string(item['title'])."',
              '".mysql_real_escape_string(item['description'])."',
              '".mysql_real_escape_string(item['dc']['date'])."'
          );";
          mysql_query(bookmark_insert_query) or die(mysql_error());
      }
  }
  //select the last NUM_RSS_ITEMS added and output a new feed
  bookmarks_select_query = "select * from bookmarks where tag='".mysql_real_escape_string(_GET['tag'])."' order by id desc limit ".NUM_RSS_ITEMS."";
  bookmarks_select_results = mysql_query(bookmarks_select_query) or die(mysql_error());
  //setup the headers
  header ("Content-type: text/xml"); // Output file as XML
  echo ("<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n");
  //standard RSS 2.0 bits including dc
  ?>
  <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
  <title>del.icio.us/tag/<?php echo _GET['tag']; ?> - filtered</title>
  <link>http://del.icio.us/tag/ echo _GET['tag']; ?></link>
  <description>Filtered tag feed for the del.icio.us <?php echo _GET['tag']; ?> tag.</description>
  <?php
  //itereate through each result returned and make an <item>
  while (this_bookmark = mysql_fetch_assoc(bookmarks_select_results))
  {
      ?>
      <item>
      <guid isPermaLink="false"><?php echo sanitize_for_xml(this_bookmark['id']); ?>@http://del.icio.us/tag/ echo _GET['tag']; ?></guid>
      <title><?php echo sanitize_for_xml(this_bookmark['title']); ?></title>
      <link><?php echo sanitize_for_xml(this_bookmark['link']); ?></link>
      <description><?php echo sanitize_for_xml(this_bookmark['description']); ?></description>
      <dc:date><?php echo sanitize_for_xml(this_bookmark['dc_date']); ?></dc:date>
      </item>
      <?php
      unset (this_bookmark);
  }
  ?>
  </channel>
  </rss>
  <?php
  } else {
      //no tag provided as a query string
      die('No tag defined!');
  }
  
  //make non xml-friendly characters safe for xml
  function sanitize_for_xml(string) {
      final = '';
      //remove high bit characters
      string_chars = array();
      for (i = 0; i < strlen(string); i++) { 
          string_chars[] = string[i]; 
      }
      foreach (string_chars as char) {
          if (ord(char) < 128) final .= char;
      }
      //sanitize the non-xml friendly characters
      return str_replace ( 
          array ( '&', '"', "'", '<', '>' ), 
          array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;' ), final );
  }
  
  //close the database connection
  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.