IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
1 | #!/usr/bin/perl |
2 | |
3 | sub rows |
4 | { |
5 | local $s = $_[0]; |
6 | my @res = (); |
7 | |
8 | while ($s =~ m#.*?<tr>(.*?)</tr>#) { |
9 | push(@res, $1); |
10 | $s = $'; |
11 | } |
12 | return @res; |
13 | } |
14 | |
15 | |
16 | sub cols |
17 | { |
18 | local $s = $_[0]; |
19 | my @res = (); |
20 | |
21 | while ($s =~ m#.*?<td[^>]*>(.*?)</td>#) { |
22 | push(@res, $1); |
23 | $s = $'; |
24 | } |
25 | return @res; |
26 | } |
27 | |
28 | |
29 | sub usage |
30 | { |
31 | print STDERR "usage: $0 (query [-i cache_file] | dsc | inv) [file ...]\n"; |
32 | exit(1); |
33 | } |
34 | |
35 | |
36 | $mode = shift @ARGV; |
37 | &usage unless $mode eq "query" || $mode eq "dsc" || $mode eq "inv"; |
38 | |
39 | if ($mode eq "query") { |
40 | if ($ARGV[0] eq "-i") { |
41 | shift @ARGV; |
42 | $name = shift @ARGV; |
43 | open(OLD, $name) || die "$name: $!"; |
44 | $q = join("", <OLD>); |
45 | ($old = $q) =~ tr/\r\n//d; |
46 | close OLD; |
47 | } |
48 | |
49 | while (<>) { |
50 | chop; |
51 | s/#.*//; |
52 | next if /^\s*$/; |
53 | next if /^\s/; |
54 | s/\s.*//; |
55 | next if $old =~ m#align=right>Digi-Key Part Number</th><td>$_</td#; |
56 | push(@pn, $_); |
57 | } |
58 | |
59 | if (0+@pn) { |
60 | $cmd = "wget -nv -O - ".join(" ", |
61 | map |
62 | "http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail\\&name=$_", |
63 | @pn); |
64 | $q .= `$cmd`; |
65 | } |
66 | |
67 | print $q; |
68 | exit; |
69 | } |
70 | |
71 | |
72 | $q = join("", <>); |
73 | $q =~ tr/\r\n//d; |
74 | |
75 | print "#DSC\n" if $mode eq "dsc"; |
76 | print "#INV\n" if $mode eq "inv"; |
77 | print "# MACHINE-GENERATED. DO NOT EDIT !\n"; |
78 | print "# ", `date -u`; |
79 | |
80 | for (split(/<!DOCTYPE HTML/, $q)) { |
81 | next unless m#align=right>Digi-Key Part Number</th><td>([^<]+)</td#; |
82 | $pn = $1; |
83 | $qty = 0; |
84 | if (m#align=right>Quantity Available</th><td[^>]*>([0-9,]+)<#) { |
85 | ($qty = $1) =~ tr/,//d; |
86 | } |
87 | next unless m#align=right>Description</th><td>(.*?)</td#; |
88 | $dsc = $1; |
89 | next unless m#<table.*<th>Price Break<(.*?)</table>#; |
90 | if ($mode eq "dsc") { |
91 | print "DIGI-KEY $pn $dsc\n"; |
92 | next; |
93 | } |
94 | print "DIGI-KEY $pn $qty USD"; |
95 | for (&rows($1)) { |
96 | @c = &cols($_); |
97 | next unless $c[0] =~ /^[0-9,]+$/; |
98 | next unless $c[1] =~ /^[0-9.]+$/; |
99 | $c[0] =~ tr/,//d; |
100 | $c[1] =~ tr/,//d; # let's hope we don't need this one often :) |
101 | $c[1] =~ s/0+$// if $c[1] =~ /\./; |
102 | print " $c[0] $c[1]"; |
103 | } |
104 | print "\n"; |
105 | } |
106 |