def openlibrary(num, num_type, force=False, as_holding=True): # ensure we're dealing with a proper identifier type and value try: if num_type.upper() not in ('ISBN', 'OCLC', 'LCCN', 'OLID'): raise if num_type.upper() == 'ISBN': num = clean_isbn(num) if len(num) not in (10, 13): raise except: return {} params = '%s:%s' % (num_type, num) url = 'http://openlibrary.org/api/books?format=json&jscmd=data' + \ '&bibkeys=%s' % params try: response = urlopen(url) json_data = json.loads(response.read()) book = json_data.get(params, {}) for ebook in book.get('ebooks', []): if ebook.get('availability', '') == 'full': return make_openlib_holding(book) if as_holding else book if not force: return {} return make_openlib_holding(book) if as_holding else book except: return {}
def hathitrust(num, num_type): # ensure we're dealing with a proper identifier type and value try: if num_type.upper() not in ('ISBN', 'OCLC', 'LCCN', 'OLID'): raise if num_type.upper() == 'ISBN': num = clean_isbn(num) if len(num) not in (10, 13): raise except: return {} params = '%s/%s' % (num_type, num) url = 'http://catalog.hathitrust.org/api/volumes/brief/%s.json' % params try: response = urlopen(url) json_data = json.loads(response.read()) for item in json_data.get('items', []): if item.get('usRightsString', '') == 'Full view': return make_hathi_holding(item.get('itemURL',''),item.get('fromRecord','')) except: return {}
def hathitrust(num, num_type): # ensure we're dealing with a proper identifier type and value try: if num_type.upper() not in ('ISBN', 'OCLC', 'LCCN', 'OLID'): raise if num_type.upper() == 'ISBN': num = clean_isbn(num) if len(num) not in (10, 13): raise except: return {} params = '%s/%s' % (num_type, num) url = 'http://catalog.hathitrust.org/api/volumes/brief/%s.json' % params try: response = urlopen(url) json_data = json.loads(response.read()) for item in json_data.get('items', []): if item.get('usRightsString', '') == 'Full view': return make_hathi_holding(item.get('itemURL', ''), item.get('fromRecord', '')) except: return {}
def test_bad_isbns(self): """ensure some odd cases come out right.""" for in_isbn, out_isbn in self.examples: self.assertEqual(clean_isbn(in_isbn), out_isbn)