File: upcrpc.py

package info (click to toggle)
zbar 0.10%2Bdoc-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,856 kB
  • ctags: 2,903
  • sloc: ansic: 21,641; sh: 10,745; cpp: 1,997; python: 774; xml: 473; perl: 289; makefile: 189
file content (43 lines) | stat: -rwxr-xr-x 1,178 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python
from xmlrpclib import ServerProxy
import sys, re

server = ServerProxy("http://dev.upcdatabase.com/rpc")
ean_re = re.compile(r'^(UPC-A:|EAN-13:)?(\d{11,13})$', re.M)

def lookup(decode):
    match = ean_re.search(decode)
    if match is None:
        print decode,
        return
    ean = match.group(2)
    if match.group(1) == "UPC-A:":
        ean = "0" + ean;
    elif len(ean) < 12:
        print decode,
        return
    if len(ean) == 12:
        ean = server.calculateCheckDigit(ean + "C")
    print "[" + match.group(1) + ean + "]",
    result = server.lookupEAN(ean)
    if isinstance(result, dict):
        if "found" not in result or not result["found"] or \
               "description" not in result:
            print "not found"
        else:
            print result["description"]
    else:
        print str(result)
    sys.stdout.flush()

if __name__ == "__main__":
    del sys.argv[0]
    if len(sys.argv):
        for decode in sys.argv:
            lookup(decode)
    if not sys.stdin.isatty():
        while 1:
            decode = sys.stdin.readline()
            if not decode:
                break
            lookup(decode)