Last active
January 13, 2023 04:53
-
-
Save hornc/de53b2776e57dc88e1b624d438aac595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from baseconv import base58 | |
from isbnlib import is_isbn13, check_digit13 | |
def compress_isbn(isbn): | |
# compresses a valid checkdigit ISBN13 | |
isbn = isbn.replace('-', '') | |
assert is_isbn13(isbn) | |
prefix = isbn[:3] | |
body = isbn[3:-1] | |
n = int(body) | |
if prefix == '979': | |
n += 10**9 | |
return base58.encode(n) | |
def decompress_isbn(s): | |
n = int(base58.decode(s)) | |
n += 978 * 10**9 | |
n = str(n) | |
c = check_digit13(n) | |
return f'{n}{c}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from baseconv import base58 | |
from isbnlib import is_isbn13, check_digit13 | |
import sys | |
def compress_isbn(isbn): | |
# compresses a valid checkdigit ISBN13 | |
isbn = isbn.replace('-', '') | |
assert is_isbn13(isbn) | |
prefix = isbn[:3] | |
body = isbn[3:-1] | |
n = int(body) | |
if prefix == '979': | |
n += 10**9 | |
return base58.encode(n) | |
def decompress_isbn(s): | |
n = int(base58.decode(s)) | |
n += 978 * 10**9 | |
n = str(n) | |
c = check_digit13(n) | |
return f'{n}{c}' | |
if __name__ == '__main__': | |
with open(sys.argv[1], 'r') as f: | |
for line in f: | |
print(compress_isbn(line.strip())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment