# Usages:
# ./farnell-find [<options>] NNN-NNNN
# Prints info about the specified relevant part.
+# ./farnell-find [<options>] NNN-NNNN <qty> [...] <sid>|<url>
+# Add specified parts and quantities to the cart.
# ./farnell-find [<options>] <file> ...
# Processes input files and produces BOM parts list.
#
use POSIX;
use IO::Handle;
use IO::File;
+use LWP::UserAgent;
+use HTTP::Request;
+use HTTP::Response;
our(@warn);
our(@fault);
our(@sorthow);
# list of desc price avail
+our($useragent);
+our($jsessionid);
+
our($partfrac_unique)= 'aaaa'; # increments
our($partword1_re)= '\S*[^ \t0-9-]\S*';
our(%stockmap)= ('In Stock' => 'Y',
'Awaiting Delivery' => 'A');
+sub get_useragent () {
+ my ($whoami,$mailname);
+ return if defined $useragent;
+ die "want http_proxy\n" unless exists $ENV{'http_proxy'};
+ chomp($whoami= `whoami`); $? and die $?;
+ chomp($mailname= `cat /etc/mailname`); $? and die $?;
+ $useragent= new LWP::UserAgent(env_proxy => 1, keep_alive =>1);
+}
+
sub by_item ($;$) {
my ($itemcode,$why) = @_;
# looks up $itemcode (ddd-d...) at Farnell and returns
$chr->{Shortcode}= $1.$2;
$url .= $chr->{Shortcode};
if (!-f "farn.$itemcode") {
- die "want http_proxy\n" unless exists $ENV{'http_proxy'};
- system "wget -otmp.farnerr.$itemcode -Otmp.farn.$itemcode $url";
- if ($?) { system "cat tmp.farnerr.$itemcode"; die; }
+ my ($response);
+ get_useragent();
+ print STDERR "got ua... $url\n";
+ $response= $useragent->get($url);
+ print STDERR "got response... $response\n";
+ $response->is_success or die "$url ".$response->as_string." ?";
+ $_= $response->content;
+ open N, ">tmp.farn.$itemcode" or die $!;
+ print N or die $!;
+ close N or die $!;
rename "tmp.farn.$itemcode", "farn.$itemcode" or die $!;
unlink "tmp.farnerr.$itemcode";
+ } else {
+ $_= `cat farn.$itemcode`; $? and die $?;
}
- $_= `cat farn.$itemcode`; $? and die $?;
s/[^\n\t\x20-\x7e\£]/\?/g;
if (!eval {
$sc2= snarf('Order Code / Sku');
# price
return $chr;
}
-
+
sub snarf ($;$) {
my ($thing, $allowbefore) = @_;
$allowbefore= defined $allowbefore ? "(?:$allowbefore)?" : '';
return $1;
}
+sub cart_add (@) {
+ my (@orderlist) = @_;
+ my ($i);
+ while (@orderlist) {
+ for ($i=0; $i<5; $i++) {
+ # fixme finish this
+ }
+ }
+}
+
sub read_spec ($) {
my ($filename) = @_;
local ($_);
foreach $k (sort keys %$chr) {
printf "%-20s %s\n", $k, $chr->{$k} or die $!;
}
+} elsif (@ARGV > 1 && (@ARGV % 1) && $ARGV[0] =~ m/^\d\d\d\-/) {
+ my (@orderlist,$item,$qty);
+ $jsessionid= pop @ARGV;
+ if ($jsessionid =~ m/\;jsessionid=([0-9A-Z]+)\b/) {
+ $jsessionid= $1;
+ } elsif ($jsessionid =~ m/^([0-9A-Z]+)$/) {
+ } else {
+ die "bad jsessionid\n";
+ }
+ while (@ARGV) {
+ ($item,$qty,@ARGV) = @ARGV;
+ $item =~ m/^$item_re$/ or die "$item ?";
+ $qty =~ m/^[1-9]\d{0,5}$/ or die "$qty ?";
+ push @orderlist, { Item => $item, Qty => $qty };
+ }
+ cart_add(@orderlist);
} elsif (@ARGV) {
my ($filename);
foreach $filename (@ARGV) {