34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
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])
|