chiark / gitweb /
alarming - it hides your order in your browser state
[trains.git] / farnell / farnell-resistor-convert.pl
1 #!/usr/bin/perl
2 # ways to invoke:
3 #  1. Get a search list from farnell and feed HTML source to
4 #     this script with no args.  Will produce part number list.
5 #  2. Feed one or more outputs from farnell-find <item> to
6 #     this script with args  <mfprefix> <valueprefix>...
7 #     where <mfprefix> is the re for the first part of the mfr
8 #     code and <valueprefix> is the words of the part name prefix.
9 if (@ARGV>=2) {
10     ($kind,@prefix)= @ARGV;
11     sub insdot ($) {
12         my ($x)= @_;
13         $x =~ s/[mkr]/./;
14         return $x;
15     }   
16     while (<STDIN>) {
17         if (m/^Avail\s/) {
18             undef $value;
19         } elsif (m/^Description\s+\[$kind (\d[0-9MKR]+)\]\s+RESISTOR/) {
20             $value= lc $1;
21         } elsif (m/^Description\s+(\S.*)/) {
22             print "#  $1\n";
23             $value= '';
24         } elsif (m/^Item\s+([-0-9]+)$/) {
25             die unless defined $value;
26             next unless length $value;
27             $item= $1;
28             die if exists $v2i{$value} and $v2i{$value} ne $1;
29             $v2i{$value}= $item;
30             undef $value;
31         }
32     }
33     foreach $v (sort {
34         foreach $mkr (qw(m k r)) {
35             $z= ($a =~ m/$mkr/) <=> ($b =~ m/$mkr/);
36             return $z if $z;
37         }
38         return insdot($a) <=> insdot($b);
39     }
40                 keys %v2i) {
41         printf "%s =\t%s\n", join(' ',@prefix,$v), $v2i{$v};
42     }    
43 } elsif (!@ARGV) {
44     while (<STDIN>) {
45         if (m/viewproductdetails.*\?prodId\=(\d{3})(\d{3,7})\"/) {
46             print "$1-$2\n" or die $!;
47         }
48     }
49 } else {
50     die;
51 }