From e638037364fbfa96c67f2ade8ee44ecd19cdb516 Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 18 Apr 2004 14:17:35 +0000 Subject: [PATCH] uses LWP::UserAgent etc --- farnell/farnell-find | 61 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/farnell/farnell-find b/farnell/farnell-find index 66bf1bc..da3871b 100755 --- a/farnell/farnell-find +++ b/farnell/farnell-find @@ -3,6 +3,8 @@ # Usages: # ./farnell-find [] NNN-NNNN # Prints info about the specified relevant part. +# ./farnell-find [] NNN-NNNN [...] | +# Add specified parts and quantities to the cart. # ./farnell-find [] ... # Processes input files and produces BOM parts list. # @@ -66,6 +68,9 @@ use strict; use POSIX; use IO::Handle; use IO::File; +use LWP::UserAgent; +use HTTP::Request; +use HTTP::Response; our(@warn); our(@fault); @@ -114,6 +119,9 @@ our(%incexc); # $incexc{$circuit}= 1; for those mentioned our(@sorthow); # list of desc price avail +our($useragent); +our($jsessionid); + our($partfrac_unique)= 'aaaa'; # increments our($partword1_re)= '\S*[^ \t0-9-]\S*'; @@ -124,6 +132,15 @@ our($item_re)= '\d{3}\-\d{3,7}'; 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 @@ -136,13 +153,21 @@ sub by_item ($;$) { $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'); @@ -170,7 +195,7 @@ sub by_item ($;$) { # price return $chr; } - + sub snarf ($;$) { my ($thing, $allowbefore) = @_; $allowbefore= defined $allowbefore ? "(?:$allowbefore)?" : ''; @@ -184,6 +209,16 @@ sub snarf ($;$) { 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 ($_); @@ -516,6 +551,22 @@ if (@ARGV==1 && $ARGV[0] =~ m/^\d\d\d\-/) { 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) { -- 2.30.2