#!/usr/local/bin/perl # 2007-07-22 v0.01 # License : Same as Perl # Sample : http://blog.bulkneets.net/misc/ustream/channel2json.pl use strict; use CGI; use LWP::UserAgent; use Cache::FileCache; use URI::Fetch; use JSON::Syck; my $q = CGI->new; my $cache = Cache::FileCache->new({ 'cache_root' => '.cache', 'namespace' => 'ustream2json', 'default_expires_in' => 3600 }); my $channel_name = $q->param("channel_name"); # $channel_name = "yappo,mala"; my @channels = split(/,/, $channel_name); if (scalar @channels > 30){ output_json($q, "too large"); } my %result = map { $_ => channel2usc($_) } map { $_=~s{http://www.ustream.tv/channel/}{}; $_ } @channels; output_json($q, \%result); sub channel2usc { my $channel = shift; my $url = "http://www.ustream.tv/channel/" . $channel; my $ua = LWP::UserAgent->new; $ua->agent('ustream2json/0.01'); my $res = URI::Fetch->fetch( $url, UserAgent => $ua, Cache => $cache, NoNetwork => 3600 ); return undef unless $res->is_success; my $content = $res->content; $content =~m{src="http://www.ustream.tv/([^"]*?).usc"}; return $1 ? $1 : undef; } sub output_json { my ($q, $obj) = @_; print $q->header( "-type" => "text/javascript", "-charset" => 'utf-8' ); my $json = JSON::Syck::Dump($obj); if(defined (my $p = $q->param('callback'))){ $json = "$p($json);" if($p =~ /[a-zA-Z0-9\.\_\[\]]/); } print $json; }