#!/usr/bin/perl -w ################################################### # # locate the libraries (stolen from mt.cgi) (real men indent with tabs) # my($MT_DIR); BEGIN { if ($0 =~ m!(.*[/\\])!) { $MT_DIR = $1; } else { $MT_DIR = './'; } # for testing from the command line :) # $MT_DIR = 'c:/www/local/mt3/plugins/'; unshift @INC, $MT_DIR . '../lib'; unshift @INC, $MT_DIR . '../extlib'; } ################################################### # # use only libraries that ship with mt3 # use XML::Parser::Lite; use LWP::Simple; use CGI; use strict; ################################################### # # grab the rdf # my $cgi = new CGI; my $username = $cgi->param('username'); $username = 'audioscrobbler' unless defined $username; # i'm dubious - no browsers seem to need the right content-type, and setting # it to text/plain makes debug eleventy billion times easier. hmmm print $cgi->header( # -type => 'text/javascript', -type => 'text/plain', ); my $url = "http://ws.audioscrobbler.com/rdf/history/$username"; print qq|// Getting $url ...\r\n|; my $content = get($url); ################################################### # # parse it and display some results # my ($artist, $track, $artist_url, $track_url); my @tag_stack = ('STACK_ROOT'); my $next_tag = 'STACK_ROOT'; my @lines = (); my $parser = new XML::Parser::Lite Handlers => { Start => \&start_tag, Char => \&do_char, End => \&end_tag, }; $parser->parse($content); &out_lines() if scalar(@lines); &die_bitch() unless scalar(@lines); ################################################### # # the parser stuff # sub start_tag { shift; $next_tag = $_[0]; push @tag_stack, $next_tag; if ($next_tag eq 'item'){ $artist = ''; $track = ''; $artist_url = ''; $track_url = ''; } if ($next_tag eq 'mm:Artist'){ $artist_url = $_[2]; } } sub do_char { shift; if ($next_tag eq 'dc:title'){ if ($tag_stack[$#tag_stack-1] eq 'dc:creator'){ $artist .= $_[0]; } if ($tag_stack[$#tag_stack-1] eq 'item'){ $track .= $_[0]; } } if ($next_tag eq 'link'){ $track_url .= $_[0]; } } sub end_tag { shift; if ($_[0] eq 'item'){ $track = $track ?$track :''; $track_url = $track_url ?$track_url :''; $artist = $artist ?$artist :''; $artist_url = $artist_url ?$artist_url :''; $track = qq|$track| if $track_url; $artist = qq|$artist| if $artist_url && $artist; $artist = " by $artist" if $artist; if ($track){ push @lines, qq|

$track$artist

|; } } pop @tag_stack; $next_tag = $tag_stack[$#tag_stack]; } ################################################### # # output the goods # sub die_bitch { print qq|document.write('

Audioscrobbler is currently down.

');\r\n|; } sub out_lines { for my $line(@lines){ $line =~ s!'!\\'!g; print qq|document.write('$line');\r\n|; } } ################################################### # # silly perl :) # 1;