« Creative Timestamps | Main | Flickr on your Mobile Phone »
January 03, 2006
Improved del.icio.us Links
Here's a small script for all you del.icio.us users out there. It improves upon the idea of including your recent links on your website or blog.
Currently, when you use one of the official del.icio.us methods, you can specify the tags you'd like to filter your links on. For example, on the right side of this page I keep a list of my most recent bookmarks about PHP and CSS. The problem with this is that it will "AND" the two tags together - meaning it will only display links which are tagged with both PHP and CSS. What if we want to get links that are tagged PHP or CSS? To my knowledge there's no way to do this, which is why I wrote the following script. (If there is a way, please correct me in the comments below :)
<?PHP
$static_file = "delicious.static";
// If we haven't checked del.icio.us in at least an hour,
// do so now, and save the results to disk.
if(time() - filemtime($static_file) >= 300)
{
// Get our tags...
$tags = $_GET['tags'];
$tag_array = explode(",", $tags);
$bullet = "";
// Initialize our arrays...
$links = array();
$titles = array();
// Loop through each tag and get the feed from del.icio.us...
foreach($tag_array as $tag)
getFeed($tag, $links, $titles);
// And create our HTML...
for($i = 0; $i < count($links); $i++)
{
$link = $links[$i];
$title = $titles[$i];
$html .= "$bullet<a class='delLink' href='$link'>$title</a><br/>";
}
$fp = fopen($static_file, "w+");
fwrite($fp, $html);
echo $html;
}
else
{
include($static_file);
}
// Returns an array of delicious links for the given tag.
// If $tag is empty, it'll just pull the most recent links.
function getFeed($tag, &$links, &$titles)
{
// Download the feed from del.icio.us
$url = "http://del.icio.us/html/tylerhall/{$tag}?count=20&bullet=&tags=no";
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$html = str_replace("\n", "", $html);
// Now that we have the feed, extract the links
preg_match_all('/<div.*?<a.*?href="(.*?)">(.*?)<\/a>.*?<\/div>/', $html, $matches);
$links = array_merge($links, $matches[1]);
$titles = array_merge($titles, $matches[2]);
}
?>
howto , php , web2.0 | By tylerhall | 09:54 AM
Trackback Pings
TrackBack URL for this entry:
http://chattablogs.com/mt/mt-tb.cgi/28317
Listed below are links to weblogs that reference Improved del.icio.us Links:
Comments
I'm definitely going to use this on my personal blog (when I get around to updating it). Nice job!
Posted by: Jon Henshaw at January 3, 2006 10:45 AM



