Created
January 9, 2020 01:05
-
-
Save ryuuji/20f51424bea9efc593a93a8891ed54b7 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import isbnlib | |
TRY_ISBN_DIRECT = ["direct"] # 直接 | |
TRY_ISBN_10 = ["i10"] # ISBN10桁で検索 | |
TRY_ISBN_13 = ["i13"] # ISBN13桁で検索 | |
TRY_ISBN_13_10 = ["i13", "i10"] # ISBN10桁と13桁で検索 | |
TRY_ISBN_13C_10 = ["i13c", "i10"] # ISBN10桁と13桁の978カットで検索 | |
TRY_ISBN_ALL = ["i13c", "i13", "i10"] # ISBN10桁と13桁の978カットで検索 | |
TRY_ISBN_13H_10H = ["i13h", "i10h"] # ISBN10桁と13桁のハイフンありで検索 | |
TRY_ISBN_13CH_10H = ["i13ch", "i10h"] # ISBN10桁と13桁のハイフンありで検索 | |
TRY_ISBN_9 = ["i9"] # ISBN9桁 | |
def isbn_variations(s, isbn_search_index): | |
# type: (str, list) -> list | |
# ISBNの変化形をリストで返す | |
def push(_s): | |
r.append(_s if _s else _isbn) | |
r = [] | |
_isbn = isbnlib.canonical(s) | |
if _isbn is None: | |
_isbn = s | |
if len(_isbn) == 10 and not isbnlib.notisbn('978' + _isbn): | |
push(_isbn) | |
_isbn = '978' + _isbn | |
if len(_isbn) >= 10: | |
for _type in isbn_search_index: | |
if _type == "direct": | |
push(_isbn) | |
elif _type == "i10": | |
push(isbnlib.to_isbn10(_isbn)) | |
elif _type == 'i13': | |
push(isbnlib.to_isbn13(_isbn)) | |
elif _type == 'i13c': | |
x = isbnlib.to_isbn13(s) | |
push(x[3:] if x and x[0:3] == '978' else _isbn) | |
elif _type == 'i13ch': | |
x = isbnlib.to_isbn13(s) | |
if x: | |
x = isbnlib.mask(x) | |
push(x[4:] if x and x[0:4] == '978-' else _isbn) | |
elif _type == 'i13h': | |
x = isbnlib.to_isbn13(s) | |
push(isbnlib.mask(x) if x else _isbn) | |
elif _type == 'i10h': | |
x = isbnlib.to_isbn10(s) | |
push(isbnlib.mask(x) if x else _isbn) | |
elif _type == 'i9': | |
x = isbnlib.to_isbn10(s) | |
push(x[0:9] if x else _isbn) | |
else: | |
raise Exception('invalid isbn type[%s]' % _type) | |
return list(set(r)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment