From f13d3ac019c024eeb46773a7e94df4f5003b494c Mon Sep 17 00:00:00 2001 From: Tomek Dubrownik Date: Fri, 11 May 2012 00:08:49 +0200 Subject: [PATCH] initial commit --- worldcat.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 worldcat.py diff --git a/worldcat.py b/worldcat.py new file mode 100644 index 0000000..422f65f --- /dev/null +++ b/worldcat.py @@ -0,0 +1,33 @@ +import json +import urllib2 +from sys import argv, stderr + +xisbn_url = 'http://xisbn.worldcat.org/webservices/xid/isbn/%s?method=getMetadata&format=json&fl=*' +class WCException(Exception): + pass + +def check_worldcat(isbn): + data = urllib2.urlopen(xisbn_url % isbn) + res = json.load(data) + if res['stat'] != 'ok': + raise WCException(res['stat']) + return res['list'] + +def main(fname): + output = [] + with open(fname, 'r') as f: + for line in f: + isbn = line.strip() + print >> stderr, '[II] Checking metadata for ISBN:', isbn + try: + res = check_worldcat(isbn) + if len(res) != 1: + print >> stderr, '[WW](ISBN: %s) %d results found, omitting from output' % \ + (isbn, len(res)) + output.append(res[0]) + except WCException as e: + print >> stderr, '[EE](ISBN: %s) Exception:' % isbn, e + print json.dumps(output, indent=4) + +if __name__ == '__main__': + main(argv[1])