# Blosxom Plugin: technorati_api # Author: Dave Slusher # Version: 0.1, 20040601 # http://www.evilgeniuschronicles.org/ # See the perldoc info at the bottom for more details package technorati_api; use LWP::Simple; # --- Configurable variables ----- # IMPORTANT - YOU MUST CHANGE THIS TO MAKE IT WORK # WITHOUT AN API KEY NOTHING WILL HAPPEN # you need to have your own API key to use this. You can get one at # http://www.technorati.com/members/apikey.html my $api_key=""; my $separator = " - "; $debug_level = 1 unless defined $debug_level; # if you want a different URL than the default # blosxom url (such as using a domain rather than the fully qualified # path to the CGI) enter it here. If blank, it will use the default my $technorati_url = "http://www.evilgeniuschronicles.org" || $blosxom::url; my $cache_file = $blosxom::plugin_state_dir.'/'."technorati_api.dat"; # set this to the time in hours between downloading new copies of the # information. Set it to 0 to means never cached (highly NOT recommended - this will # slow everything down and quickly burn through your number of lookups per day) my $cache_hours = 6; my $limit = 100; # --- End Configurable variables ----- # ------------------------------------ use vars qw !$technorati_cosmos $inbound_blogs $inboundlinks!; my $cosmos_list = (); sub debug { my ($level, @msg) = @_; if ($debug_level >= $level) { print STDERR "$package debug $level: @msg\n"; } 1; } sub start { format_list(); 1; } sub get_list { use Data::Dumper; if ( !-e $cache_file or time() > (stat $cache_file)[9] + ($cache_hours * 3600)) { return fetch_cosmos(); } else { return load_cache(); } } sub load_cache { my $list = (); #open data file local *FH; if( -e "$cache_file") { open FH, "$cache_file" or debug (0, "Error opening cache: $!"); } flock(FH, 2); while () { chomp ($_); my ($url, $title) = m/(\S*)\s(.*)/; my $item = {url=>$url, title=>$title}; push @{$list}, $item; } close (FH); return $list; } sub save_cache { my $cosmos = shift; my $counter = 0; my $item; local *FH; open FH, ">$cache_file" or return 0; flock(FH, 2); debug(1, "writing to $cache_file at ".localtime()."\n"); while ($item = $cosmos->[$counter++]) { debug (1, $item->{url}." ".$item->{title}."\n"); print FH $item->{url}." ".$item->{title}."\n"; ; } close FH; return 1; } sub fetch_cosmos { my $list = (); my %seen_urls; # my $url = "http://apibeta.technorati.com/cosmos?format=xml&url=".$technorati_url."&key=".$api_key."&type=weblog"; my $url = "http://api.technorati.com/cosmos?format=xml&url=".$technorati_url."&key=".$api_key."&type=weblog"; if ($limit) { $url .= "&limit=$limit"; } my $xml = get($url); debug (1, "Results of fetching $url are:\n$xml"); my ($results, $item) = $xml =~ m#(.*?)(.*)#ms; #debug (1, "Results = $results\n\n"); #debug (1, "Items = $item\n\n"); ($inbound_blogs, $inbound_links) = $results =~ m#([^<]*).*([^<]*)#ms; while ($item =~ m#(.*?).*?(.*?)#gms ) { if (!$seen_url{$2} ) { my $item = {title=>$1, url=>$2}; push @{$list}, $item; $seen_url{$2} =1 ; } } save_cache($list); return $list; } sub format_list { my $list = get_list(); my $counter = 0; $technorati_cosmos = "
\n"; while ($item = $list->[$count++]) { $technorati_cosmos .= "{url}."\">".$item->{title}."
\n"; } $technorati_cosmos .= "
\n"; # debug(1, $technorati_cosmos); } 1;