Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

UPCdatabase lookup in perl

by beretboy (Chaplain)
on Jun 28, 2001 at 23:54 UTC ( [id://92439]=perlcraft: print w/replies, xml ) Need Help??

   1: # Takes raw cuecat output and looks up the barcode scanned
   2: # through upcdatabase.com
   3: # I DID NOT WRITE THE SECTION THAT DOES THE DECODING
   4: # and at the moment the name of the person who did escapes
   5: # me
   6: use LWP::Simple;
   7: $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
   8: 
   9: sub decode
  10: {
  11:     ($encoded) = @_;
  12:     @s = map { index($seq,$_); } split(//,$encoded);
  13:     $l = ($#s+1) % 4;
  14:     if ($l)
  15:     {
  16: 	if ($l == 1)
  17: 	{
  18: 	    print "Error!";
  19: 	    return;
  20: 	}
  21: 	$l = 4-$l;
  22: 	$#s += $l;
  23:     }
  24:     $r = '';
  25:     while ($#s >= 0)
  26:     {
  27: 	$n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
  28: 	$r .=chr(($n >> 16) ^ 67) .
  29: 	     chr(($n >> 8 & 255) ^ 67) .
  30: 	     chr(($n & 255) ^ 67);
  31: 	@s = @s[4..$#s];
  32:     }
  33:     $r = substr($r,0,length($r)-$l);
  34:     return $r;
  35: }
  36:     chomp($s = <STDIN>);
  37:     @fields = split(/\./,$s);
  38:     @results = map(decode($_), @fields[1..$#fields]);
  39: 
  40: $code = $results[2];
  41: 
  42: $upcdata = get("http://www.upcdatabase.com/item.pl?upc=$code");
  43: 
  44: $upcdata =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
  45: 
  46: $upcdata =~ /Description(.*)\n/;
  47: 
  48: print "\n\n$1\n";
  49: 
  50: $upcdata =~ /Manufacturer(.*)\n/;
  51: 
  52: print "Made by: $1\n";
  53: 
  54: $upcdata =~ /Size\/Weight(.*)\n/;
  55: 
  56: print "Size: $1";

Replies are listed 'Best First'.
Re: UPCdatabase lookup in perl
by mexnix (Pilgrim) on Jun 29, 2001 at 00:25 UTC
    If you can get your paws on the last couple issues of 2600 Magazine, they had an interesting article in one of them call "Disecting the :CueCat" (or something like that). They show you how to disable the thing that sends a message, and tracks your cuecat scans. I'll look back, and see if I can find it.

    __________________________________________________
    %mexnix = (email = > "mexnix@hotmail.com", website => "http://mexnix.perlmonk.org");

      Thanx anyway menix but I am all too familiar with the declwing process }-)

      "Sanity is the playground of the unimaginative" -Unknown

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found