#!/usr/bin/perl
#
# 2014-12-26 version 1.0 (c) Ewald

use strict; 
use warnings; 

use LWP::UserAgent;

my $DEBUG = 1;
my $count = 1;

# initialize pointer for HTTP fetch
my $ua = LWP::UserAgent->new();
    
# Pretend to be Chrome on a Mac
$ua->agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36");

while(<>) {
	if (/(http\:.*urishield\S+)/) {		# select URL's with urishield in it, discard the rest
		my $URL = $1;
		$DEBUG && print "found URL: $URL\n";

		# Get the mp3
    		my $response = $ua->get($URL);
    
    		unless($response->is_success) {
        		print "Error: " . $response->status_line;
    			} #unless
		
		# Let's save the output, every name gets an incrementing part number
	    	my $save = "part_" . $count++ . ".mp3";
    
    		unless(open SAVE, '>' . $save) {
        		die "Cannot create output file $save";
    		} #unless
    
    		print SAVE $response->content;
    
    		close SAVE;
    
    		$DEBUG && print "Saved " .  length($response->content) .  " bytes of data to '$save'\n\n";

		} #if
	} #while
