#! /usr/bin/perl use strict; use CGI; use XML::Simple; use XML::Writer; use LWP::UserAgent; use URI::Escape; my $cgi = new CGI; print $cgi->header(-type=>'application/xspf+xml'); my $tag = $cgi->param('tag'); my $title = "Play All Songs"; my $infourl = "http://themusic.tumblr.com/"; if ($tag) { $title .= " Tagged $tag"; $infourl .= "tagged/" . lc(uri_escape($tag)); } my @records = fetchXML($tag); my $writer = new XML::Writer( DATA_MODE => 1, DATA_INDENT=>2 ); $writer->xmlDecl( 'UTF-8' ); $writer->startTag( 'playlist', 'version' => '1', 'xmlns' => 'http://xspf.org/ns/0/'); $writer->dataElement('title',$title); $writer->dataElement('creator','TheMusic'); $writer->dataElement('info',$infourl); $writer->startTag('trackList'); foreach my $item (@records) { $writer->startTag('track'); $writer->dataElement('location',$item->{'src'}); $writer->dataElement('annotation',$item->{'caption'}); $writer->dataElement('info',$item->{'posturl'}); $writer->endTag('track'); } $writer->endTag('trackList'); $writer->endTag('playlist'); $writer->end(); sub fetchXML() { my $tag = shift; my @res = (); my $ua = new LWP::UserAgent; my $req = new HTTP::Request GET => 'http://themusic.tumblr.com/api/read?type=audio&num=50'; my $content = $ua->request($req)->content(); my $xml = new XML::Simple (KeyAttr=>[]); my $data = $xml->XMLin($content); foreach my $item (@{$data->{posts}->{post}}) { if ($tag ne '' && lc($item->{'tag'}) ne lc($tag)) { next; } my $rec = {}; $rec->{'posturl'} = $item->{'url'}; $rec->{'caption'} = $item->{'audio-caption'}; # remove tags and trim $rec->{'caption'} =~ s/<(([^>]|\n)*)>//g; $rec->{'caption'} =~ s/^\s+//; $rec->{'caption'} =~ s/\s+$//; # just take the first line.. $rec->{'caption'} = ( split /\n/,$rec->{'caption'} )[0]; my ($player) = ($item->{'audio-player'} =~ /audio_file=(\S+)&/ ); $player .= "?tumblr-drm-goes-here-but-thats-for-you-to-figure-out"; $rec->{'src'} = $player; push (@res, $rec); } return @res; }