Created
November 25, 2014 23:18
-
-
Save gandro/2186611af9544171ec50 to your computer and use it in GitHub Desktop.
Script to generate wiki page based on ISBN
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
import json, sys | |
from urllib.request import urlopen | |
API='http://xisbn.worldcat.org/webservices/xid/isbn/{isbn}?method=getMetadata&format=json&fl=*' | |
OPT_KEYS = ['author', 'city', 'ed', 'form', 'lang', 'lccn', | |
'originalLang', 'publisher', 'year'] | |
def get_metadata(isbn): | |
rawjson = urlopen(API.format(isbn=isbn)).readall().decode('utf-8') | |
response = json.loads(rawjson) | |
if not response['stat'] == 'ok': | |
sys.exit("could not get data from xISBN") | |
data = response['list'][0] | |
# fill optional keys | |
for k in OPT_KEYS: | |
if k not in data: data[k] = "" | |
# make lccn list | |
data['lccn'] = ', '.join(data['lccn']) | |
# unpack isbn, form, oclcnum | |
data['isbn'] = data['isbn'][0] | |
data['form'] = data['form'][0] | |
data['oclcnum'] = data['oclcnum'][0] | |
return data | |
# main | |
if len(sys.argv) < 2: | |
sys.exit("usage: {} ISBN".format(sys.argv[0])) | |
data = get_metadata(sys.argv[1]) | |
print("""===== {title} ===== | |
<block right> | |
{{{{http://covers.openlibrary.org/b/isbn/{isbn}-M.jpg}}}} | |
</block> | |
---- dataentry book ---- | |
type: book | |
isbn13: {isbn} | |
author: {author} | |
publisher: {publisher} | |
year: {year} | |
lang: {lang} | |
origlang: {originalLang} | |
worldcat_oclc: {oclcnum} | |
city: {city} | |
edition: {ed} | |
form_onix: {form} | |
lccns: {lccn} | |
borrower_user: | |
borrowdate_dt: | |
---- | |
""".format(**data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment