blob: 5b0e10ec8b90b53e23c16c7ee0a9a4fd28ccc12a (
plain)
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
|
#!/usr/bin/perl
use warnings;
use strict;
use Frontier::Client;
use Data::Dumper;
my $s = Frontier::Client->new('url' => 'http://www.upcdatabase.com/rpc');
$| = 1; # autoflush
foreach (@ARGV) {
lookup($_);
}
if(!-t) {
while(1) {
my $decode = <STDIN>;
last unless(defined($decode));
chomp($decode);
lookup($decode);
}
}
sub lookup {
my $decode = shift;
if($decode =~ m[^(EAN-13:|UPC-A:)?(\d{11,13})$] &&
($1 && $1 eq "UPC-A:") || ($2 && length($2) > 11)) {
my $ean = $2;
$ean = "0" . $ean
if($1 && $1 eq "UPC-A:");
$ean = $s->call('calculateCheckDigit', $ean . "C")
if(length($ean) == 12);
print("[$decode] ");
my $result = $s->call('lookupEAN', $s->string($ean));
if(ref($result)) {
print((!$result->{found} ||
(ref($result->{found}) && !$result->{found}->value()))
? "not found\n"
: "$result->{description}\n")
}
else {
print("$result\n");
}
}
else {
print("$decode\n");
}
}
|