#!/usr/bin/env perl
use strict;
use warnings;
package getimg;
my ($htmldir, $imagedir, $sounddir, $lower, $upper) = @ARGV;
if(!($htmldir && $imagedir && $sounddir && $lower && $upper)) {
print "Usage $0 html-dir image-dir sound-dir lower-bound upper-bound\n";
#die;
}
if(! -d $htmldir) {
mkdir $htmldir or die "could not create ${htmldir}\n";
}
if(! -d $imagedir) {
mkdir $imagedir or die "could not create ${imagedir}\n";
}
if(! -d $sounddir) {
mkdir $imagedir or die "could not create ${imagedir}\n";
}
sub rand_range {
my ($x, $y) = @_;
return int(rand($y - $x)) + $x;
}
my %imagehash;
for(my $i = $lower; $i <= $upper; $i++) {
our $rand_value = rand_range(0, 5);
print "going to sleep for $rand_value seconds\n";
#sleep($rand_value);
print "slept\n";
our $htmlfile = "${htmldir}/${i}.html";
my $useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17";
my $cmd = "curl -A \"$useragent\" \"http://www.hearthhead.com/card=$i\" > \"$htmlfile\"";
if (! -e $htmlfile) {
system($cmd);
} else {
print "using cached html file: \"${htmlfile}\"\n"
}
my $document = do {
local $/ = undef;
open my $fh, "<", $htmlfile
or die "could not open $htmlfile: ${!}\n";
<$fh>;
};
my $name = undef;
my $normalimagefile = undef;
my $premiumimagefile = undef;
my $sound1file = undef;
my $sound2file = undef;
my $sound3file = undef;
if ($document =~ /_\[${i}\]={"name_enus":"(.+?)","icon":"/) {
$name = $1;
$name =~ s/\\(.)/$1/g;
$name =~ s/"/\\"/g;
print "Name: " . $name . "\n";
} else {
print "No name found\n";
next;
}
if ( ! exists $imagehash{$name} ) {
$normalimagefile = "${imagedir}/${name}.png";
$premiumimagefile = "${imagedir}/${name}_premium.gif";
$sound1file = "${sounddir}/${name}_battlecry.ogg";
$sound2file = "${sounddir}/${name}_attack.ogg";
$sound3file = "${sounddir}/${name}_death.ogg";
$imagehash{$name} = 1;
} else {
print $imagehash{$name};
$normalimagefile = "${imagedir}/${name}_" . $imagehash{$name} . ".png";
$premiumimagefile = "${imagedir}/${name}_" . $imagehash{$name} . "_premium.gif";
$sound1file = "${sounddir}/${name}_" . $imagehash{$name} . "_battlecry.ogg";
$sound2file = "${sounddir}/${name}_" . $imagehash{$name} . "_attack.ogg";
$sound3file = "${sounddir}/${name}_" . $imagehash{$name} . "_death.ogg";
$imagehash{$name}++;
}
my $get_normal_cmd = undef;
my $get_premium_cmd = undef;
my $get_sound1file_cmd = undef;
my $get_sound2file_cmd = undef;
my $get_sound3file_cmd = undef;
if ($document =~ /_\[${i}\]\.tooltip_enus = '
\"${normalimagefile}\"";
print "Normal: " . $1 . "\n";
} else {
print "Could not find normal version of ${name}\n";
}
if ($document =~ /_\[${i}\]\.tooltip_premium_enus = '
\"${premiumimagefile}\"";
print "Premium: $1 \n";
} else {
print "Could not find premium version of ${name}\n";
}
if ($document =~ /