forked from phucdanghuu/ZBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupcrpc.py
executable file
·43 lines (40 loc) · 1.15 KB
/
upcrpc.py
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://www.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)