#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;

sub usage {
	print "Usage: perl $0 <url>\n";
	print "example:\nperl $0 'http://www.mixcloud.com/C00kie/crossing-the-tracks-with-kieran-cooke-171114/'\n";
}

my $ua = LWP::UserAgent->new();

my $url = shift || die usage();

my $req = HTTP::Request->new();
$req->header( "User-Agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0" );
$req->header( "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" );
$req->header( "Accept-Language" => "en-GB,en;q=0.5" );
$req->header( "Connection" => "keep-alive" );
$req->header( "Cache-Control" => "max-age=0" );
$req->url( $url );
$req->method("GET");

my $resp = $ua->request( $req );
if( !$resp->is_success ) {
	die "error getting $url";
}
my $cont = $resp->content;
open( F, ">f" ); print F $cont; close(F);

# look for
# //waveforms-mix.netdna-ssl.com/9/6/1/d/11d2-7907-4fb8-9039-abe132b90b6b.json

my $ident;
if( $cont =~ m!//waveforms-mix.netdna-ssl.com/(([a-z0-9A-Z]/){4}[a-z0-9A-Z-]+).json! ) {
	$ident = $1;
	print "https://stream17.mixcloud.com/c/m4a/64/$ident\.m4a\n";
}

