1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#!/usr/bin/perl
#$Revision: 1.4 $$Date: 2007-11-11 20:26:22 $$Author: boumenot $
#######################################################################
# FIXME: this script screen scapes the web to build the ItemSearch
# validate classes. Unfortunately, this breaks too frequently. A
# better way needs to be found.
#######################################################################
package main;
require 5.008_001;
use Getopt::Long;
use IO::File;
use Pod::Usage;
use LWP::Simple;
use Text::Template;
use Data::Dumper;
use File::Path;
use lib "$FindBin::Bin/../lib";
use HTML::TreeBuilder::XPath;
use Net::Amazon ();
use strict;
use warnings;
sub AWS4_ONLINE_HTML {
'http://docs.amazonwebservices.com/AWSECommerceService/'.$Net::Amazon::WSDL_DATE.'/DG/';
}
use constant AWS4_LOCALE_HTML => {
'us' => 'USSearchIndexParamForItemsearch.html',
# 'de' => 'DESearchIndexParamForItemsearch.html',
# 'es' => 'ESSearchIndexParamForItemsearch.html',
# 'jp' => 'JPSearchIndexParamForItemsearch.html',
# 'it' => 'ITSearchIndexParamForItemsearch.html',
# 'uk' => 'UKSearchIndexParamForItemsearch.html',
# 'fr' => 'FRSearchIndexParamForItemsearch.html',
# 'ca' => 'CASearchIndexParamForItemsearch.html',
};
my $Opt_Debug = 0;
my $Opt_Dest = "../lib/Net/Amazon/Validate/ItemSearch";
my $Opt_Overwrite = 0;
unless (&GetOptions (
"help|h" => \&usage,
"version|V" => \&version,
"debug|D" => \$Opt_Debug,
"dest=s" => \$Opt_Dest,
"overwrite" => \$Opt_Overwrite,
"<>" => \¶meter,
)) {
usage();
}
## main #########################################
unless (-d $Opt_Dest) {
die "The directory $Opt_Dest does not exist!\n";
}
for my $locale (keys %{(AWS4_LOCALE_HTML)}) {
my $link = AWS4_ONLINE_HTML.AWS4_LOCALE_HTML->{$locale};
print "fetching $link ...\n" if $Opt_Debug;
my $tree = HTML::TreeBuilder::XPath->new();
$tree->parse(get($link));
$tree->eof();
my @search_indicies = map { $_->as_text } $tree->findnodes("//div[\@class=\"section\"]//h2");
my %depts;
my %upc;
my %keywords;
for my $search_index (@search_indicies) {
(my $search_index_name) = $search_index =~ /SearchIndex:\s+(\w+)/;
next if $search_index_name eq 'All';
print $search_index_name."\n";
$upc{$search_index_name}++;
my @parameters = map { $_->as_text } $tree->findnodes("//div[\@class=\"section\"]//h2[contains(text(),\"$search_index\")]/../../../..//li/p");
for my $parameter (@parameters) {
print " -> $parameter\n";
push @{$depts{$search_index_name}}, $parameter;
$keywords{$locale}{$search_index_name}++ if $parameter eq "Keywords";
}
}
for my $dept (keys %depts) {
dump_library($depts{$dept}, $locale, $dept);
upc_add(\%upc, $depts{$dept});
}
for my $locale (keys %keywords) {
my @a = keys %{$keywords{$locale}};
dump_library(\@a, $locale, "Keywords");
}
my @a = keys %upc;
my $type = ($locale eq 'us') ? 'UPC' : 'EAN';
dump_library(\@a, $locale, $type);
}
## subs #########################################
sub usage {
print '$Revision: 1.4 $$Date: 2007-11-11 20:26:22 $$Author: boumenot $ ', "\n";
pod2usage(-verbose=>2, -exitval => 2);
exit (1);
}
sub version {
print '$Revision: 1.4 $$Date: 2007-11-11 20:26:22 $$Author: boumenot $ ', "\n";
exit (1);
}
sub parameter {
my $param = shift;
die "%Error: Unknown parameter: $param\n";
}
##################################################
# Attempt to pick a "favored" default for the different types of
# ItemSearch'es. The favored list is returned in order of preference.
# The most preferred is Books because that was the default for AWS3.
# As Books is not available for all types of ItemSearch'es use other
# "favored" defaults. They are Music, DVD, Software, etc. in that
# order. If none of those are a possible default then use the first
# item in the list of acceptable values.
sub select_default {
my $aref = shift;
my %hash = map { $_ => 1 } @$aref;
for my $favored_default (qw(Books Music DVD Software Title Keyword Keywords)) {
return $favored_default if defined $hash{$favored_default};
}
return $aref->[0];
}
sub upc_add {
my ($href, $aref) = @_;
$href->{$_}++ for @$aref;
}
sub dump_library {
my ($aref, $locale, $dept) = @_;
my $fn = "$Opt_Dest/$locale/$dept.pm";
my $dn = "$Opt_Dest/$locale";
unless (-d $dn) {
mkpath $dn or die "Failed to create '$dn'!\n";
}
if (-f $fn && !$Opt_Overwrite) {
warn "The file $fn already exists, skipping!\n";
return;
}
my $template = Text::Template->new(
TYPE => 'FILE',
SOURCE => 'aws4-itemsearch.tmpl',
DELIMITERS => [ '[%--', '--%]', ],
);
my $hash = {'MODULE_NAME' => "$locale".'::'."$dept",
'DEFAULT_OPTION' => select_default(\@$aref),
'LOCALE' => $locale,
'ITEM_SEARCH' => $dept,
'options' => \@$aref,
};
my $text = $template->fill_in(HASH => $hash);
unless ($text) {
die "Failed to fill in the text template for $locale/$dept!\n";
}
my $fouth = IO::File->new(">$fn") or
die "$! '$fn'!\n";
print $fouth $text;
$fouth->close();
}
##################################################
__END__
=pod
=head1 asw4-itemsearch
B<asw4-types> - convert Amazon's HTML data to Perl libraries to pick ItemSearch
defaults.
=head1 SYNOPSIS
B<asw4-itemsearch> - [I<OPTION>]... [I<FILE>]...
=head1 DESCRIPTION
B<asw4-itemsearch> converts the data stored in Amazon's HTML pages for
ASW4 into Perl libraries. These libraries are used by Net::Amazon to
validate user input, and select default entries for ItemSearch
operations.
=head1 ARGUMENTS
=over 4
=item -h, --help
Displays this message and program version and exits.
=item -V, --version
Displays the program's version and exits.
=item -D, --debug
Prints debug information.
=item --overwrite
Overwrite any libraries if they already exist.
=item --dest E<lt>directoryE<gt>
Specify the destination where the files should be written.
=back
=head1 AUTHORS
Written by Christopher Boumenot.
=head1 REPORTING BUGS
Report bugs to <boumenot@gmail.com>.
=head1 SEE ALSO
=cut
|