#!/usr/bin/perl -w
#######################################################################
#
# towerSearch.pl
#
#	search tower records' website for artist name formatted for sorting
#
# by Jacob Turino.
#
# I just threw this together last night. It's not really done.
#
#######################################################################

use strict;
use WWW::Mechanize;

my $VERSION = "0.1";
my $DEBUG = 0;

my $searchEngine = 
	'http://www.towerrecords.com/Music/Default.aspx?search_in=music&free_text=';
my $UPC = shift(@ARGV) or die "Usage: towerUPCSearch.pl <UPC> <Artist> <Album>\n";
my $towerUPC = $1 if $UPC =~ m/^\s*(\w*)\w\s*$/;
my $searchCriteria = $towerUPC;
my $sortName;

print "\n";
die "Invalid UPC\n" unless $towerUPC;

debug ("Searching for $towerUPC\n");

my $agent = WWW::Mechanize->new( autocheck => 1 );

while (1) {
	$agent->get($searchEngine . $searchCriteria);
	
	my $response = $agent->content;

	my $beginName = filterForRegex('href="Default.aspx?artist=');

	my %searchResults;

	while ($response =~ m/$beginName([^ &]+)&/gc) {
		$searchResults{$1} = 1;
	}
	my $numResults = scalar( keys(%searchResults) );
	if ($numResults == 1) {
		($sortName) = keys(%searchResults);
		$sortName = translateHex($sortName);
		print  "\tSort Name: $sortName\n\n";
		last;
	} else {
		print "$numResults results found.\n";
		my $artist = shift(@ARGV) or die "Nowhere else to look\n";
		my $album  = shift(@ARGV) or die "Nowhere else to look\n";
		debug ("Searching by artist/album...\n");
		$searchCriteria = $artist . " " . $album;
		$searchCriteria =~ s/ /%20/g;
	}
}

#========================================= Subroutines =========================================#
# debug($debugStatement): print debug statement
# parameters: print statement
# returns: nothing

sub debug {
  my ($statement) = @_;
  print "\tDEBUG: $statement\n" if ($DEBUG);
}
#filterForRegex($regexString): escapes all characters that might interfere w/ regex
# parameters: string
# returns: filtered string
sub filterForRegex{
	my ($word) = @_;
	# change "/" to "\/", etc
	$word =~ s/([\/
				 \\
				 \*
				 \+
				 \.
				 \?
				 \{
				 \}
				 \(
				 \)
				 \$
				 \^
				 \|
				 \ ])/\\$1/gx;
	return $word; 
}
#translateHex($line): translates Hex characters
# parameters: string
# returns: translated string
sub translateHex{
	my ($word) = @_;
	
	my %hex = ('20' => ' ',
			   '21' => '!',
			   '22' => '"',
			   '23' => '#',
			   '24' => '$',
			   '25' => '%',
			   '26' => '&',
			   '27' => "'",
			   '28' => '(',
			   '29' => ')',
			   '2a' => '*',
			   '2b' => '+',
			   '2c' => ',',
			   '2d' => '-',
			   'f7' => '÷',
			   '2e' => '.',
			   '2f' => '/',
			   '3a' => ':',
			   '3b' => ';',
			   '3c' => '<',
			   '3d' => '=',
			   '3e' => '>',
			   '3f' => '?',
			   '40' => '@',
			   '5b' => '[',
			   '5c' => '\\',
			   '5d' => '[',
			   '5e' => '^',
			   '5f' => '_',
			   '60' => '`',
			   '7b' => '{',
			   '7c' => '|',
			   '7d' => '} ',
			   '7e' => '~');	

	$word =~ tr/+/ /s; # replace '+' (plus) with ' ' (space)
   
	foreach my $key (keys %hex) {
		my $sub = filterForRegex($hex{$key});
		$word =~ s/%$key/$sub/g;
	}
	return $word; 
}