#!/usr/bin/perl
# Genasx Grabber
# Grabs the asx file from the auth-encoded ClearChannel radio stream.
# by hector@ucsd.edu, Licensed under GPL 3
# Why?
#
# So you can listen to ClearChannel radio streams without using CC radio's
# flash player. It seems ClearChannel usually supports this for people who have
# players without flash, or various operating systems. Sometimes CC breaks this
# method by not offering any asx stream and using their flash rtmp protocol,
# but usually it starts working again.
# Version 0.9: Remove extra whitespace around generated asx
# Version 0.8: Clean up ClearChannel's asx to be more standards compliant (to work with foobar2000 v. 1.0)
# Version 0.7: Add in URL redirection option for players that support it
# Version 0.6: Added lots of commented debugging to print to terminal
# Version 0.5: Format change by clearchannel, fix the script
# Version 0.4: Speed up using better regex's
# Version 0.3: Clean up the perl code
# Version 0.2: Port to perl to work anywhere
# Version 0.1: Initial implementation as a bash script
# GPL3 Notice
# -----------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
use LWP::Simple qw(!head);
use CGI qw(:standard);
#the call paramater in the command line, #tune.pl station=kfi640 in a terminal, or tune.pl?station= on a website
my($station) = param('station');
#print a blank page if the station param isn't given
print "Content-type: text/html\n\n" if(! $station);
#so far all ClearChannel streams seem to be .com
$content = get("http://www.$station.com/cc-common/ondemand/player2.html?world=st");
die "Couldn't get it@!" unless defined $content;
#find the streaming_token line, and match the token into variable $1 and the stream_id into $2
if($content =~ m/streaming_token = '(\w+)'.*'(\d+)'/) {
#the token has an extra character at the beginning, remove this character
$token = substr $1, 1;
$station_id = $2;
$content = get("http://www.$station.com/cc-common/universal_player/services/1_4_1_4/getstationlist2.php?dontcacheme=1&site_id=$station_id");
die "Couldn't get the station id!" unless defined $content;
#extract the station id
if($content =~ m/stream_id" : (\d+)/) {
$station_id = $1;
}
#print our genasx call in the terminal for debuging
#print "\nOur HTTP request (returns asx): \n\n http://www.$station.com/cc-common/universal_player/services/genasx.php?ua=$token&id=$station_id\n";
#now get the asx file
$asx = get("http://www.$station.com/cc-common/universal_player/services/genasx.php?ua=$token&id=$station_id");
# global lookup hash
my %ESCAPES = (
'&' => '&',
'<' => '<',
'>' => '>',
'"' => '"',
);
# xml encoding subroutine
sub xml_encode {
my ($str) = @_;
#original string: $str =~ s/([&<>"])/$ESCAPES{$1}/ge;
$str =~ s/([&])/$ESCAPES{$1}/ge;
return $str;
}
#clean up the asx to be more standards compliant
$asx = xml_encode($asx);
#clean up leading and trailing whitespace
$asx =~ s/^\s+//;
$asx =~ s/\s+$//;
#create the asx with type asf.
open (MYFILE, '>tune.asx');
print MYFILE $asx;
close (MYFILE);
#debug to console
#print "Location: $1\n\n";
#302 redirect
#the location of your generated tune.asx. Usually the same as this file.
#my $URL = "http://ordorica.org/tune.asx";
#redirect the player to tune.asx. I have to do this because my webserver doesn't allow me to use
#.asx as a cgi program. If your's does, then you can simply use tune.asx as the script by
#uncommenting the text below. Then you have to comment out the above .asx file creator.
#
#print "Status: 302 Moved\nLocation: $URL\n\n";
#301 redirect
#$q = new CGI;
#print $q->redirect("http://ordorica.org/tune.asx");
#print "Content-Type: video/x-ms-as; charset=iso-8859-1\n\n";
#print "Content-Type: video/x-ms-as\n\n";
#print the asx to the browser/music player
print<