You are not logged in Log in Join
You are here: Home » Members » stevea » barcode_to_amazon » cat_reader.py » View File

Log in
Name

Password

 

cat_reader.py

File details
Size
3 K
File type
text/x-python

File contents

# This software is public domain

from string import *

# expects to find the encoded barcode under 
# REQUEST.get('code')
def redirect_to_amazon(self):
    REQUEST=self.REQUEST
    code=get_isbn(decode_cue(REQUEST.get('code'))[2])
    REQUEST.RESPONSE.redirect('http://www.amazon.com/exec/obidos/ASIN/'+code)

# Derived from cat.py 
# http://www.plover.net/~skip/cc/

# pass it a scancode
# returns a tuple of serial#, type, code
def decode_cue(s):
    return tuple(
      map(decode, filter(lambda x: x and x[0] > ' ' and x[0]!='^', 
      split(s, '.'))))

def decode(s):
    s = map(lambda x: index(letters+digits+'+-', x), s)
    l = len(s) % 4
    if l:
        if l == 1: raise ValueError()
        l = 4-l
        s.extend([0]*l)
    r = ''
    while s:
        n = ((s[0] << 6 | s[1]) << 6 | s[2]) << 6 | s[3]
        r = r + chr((n >> 16) ^ 67) + chr((n >> 8 & 255) ^ 67) + chr((n & 255) ^ 67)
        s = s[4:]
    return l and r[:-l] or r


# The following methods derived from makeisbn.py

# makeisbn.py: Turn UPC and EAN barcodes into ISBNs
# This program is in the public domain.

# Written by Andrew Plotkin ([email protected])
# http://www.eblong.com/zarf/bookscan/

import re

isbndigits = re.compile("^[0-9Xx]+")
bardigits = re.compile("^[0-9 ]+")
justdigits = re.compile("^[0-9]+")

def matchesall(reg, str):
    res = reg.search(str)
    if (res == None):
        return 0
    if (len(res.group()) == len(str)):
        return 1
    return 0

def isbnchecksum(line):
    if len(line)!=9 and len(line)!=10:
        raise 'ISBN Error', '# ISBN should be 9 digits, excluding checksum!'
    line=line[:9]
    sum = 0
    count = 0
    for ix in line:
        sum = sum + (10 - count) * atoi(ix)
        count = count + 1
    sum = sum % 11
    if (sum != 0):
        sum = 11 - sum
    if (sum == 10):
        line = line + 'X'
    else:
        line = line + digits[sum]
    return line


upcmap={
'014794':'08041',
'018926':'0445',
'027778':'0449',
'037145':'0812',
'042799':'0785',
'043144':'0688',
'044903':'0312',
'045863':'0517',
'046594':'0064',
'047132':'0152',
'051487':'08167',
'051488':'0140',
'060771':'0002',
'065373':'0373',
'070992':'0523',
'070993':'0446',
'070999':'0345',
'071001':'0380',
'071009':'0440',
'071125':'088677',
'071136':'0451',
'071149':'0451',
'071152':'0515',
'071162':'0451',
'071268':'08217',
'071831':'0425',
'071842':'08439',
'072742':'0441',
'076714':'0671',
'076783':'0553',
'076814':'0449',
'078021':'0872',
'079808':'0394',
'090129':'0679',
'099455':'0061',
'099769':'0451'
}

def get_isbn(line):
    linelen = len(line)
    if (matchesall(isbndigits, line) and linelen == 10):
        return line
    if (matchesall(bardigits, line) and (linelen == 13 or linelen == 19)):
        if (line[0:3] != '978'):
            raise 'ISBN Error', '%s is not a 978 EAN' % line
        line = isbnchecksum(line[3:12])
        return line
    if (matchesall(bardigits, line) and linelen == 12):
        raise 'ISBN Error', '%s UPC barcode requires five-digit extension' % line
    if (matchesall(bardigits, line) and linelen == 18):
        prefix = line[0:6]
        suffix = line[13:18]
        if (not upcmap.has_key(prefix)):
            raise 'ISBN Error', '%s Unknown UPC prefix' % line
        ipref = upcmap[prefix]
        line = ipref + suffix[(len(ipref)-4) : ]
        line = isbnchecksum(line)
        return line
    raise 'ISBN Error', '%s Unrecognized format' % line