def _validate_book_id(self): # TODO: add check constraints # Validate book identifier if self.book_id_type == 'ISBN': if pyisbn.validate(self.book_id): if len(self.book_id) == 10: self.isbn10 = self.book_id self.isbn13 = pyisbn.convert(self.book_id) else: self.isbn13 = self.book_id self.isbn10 = pyisbn.convert(self.book_id) else: raise ValidationError( {'book_id': '{} is an invalid ISBN'.format(self.book_id)}) elif self.book_id_type == 'ASIN': regex = r"[A-Z0-9]{10}" # Remove whitespaces self.book_id = self.book_id.strip() if len(self.book_id) == 10 and re.fullmatch(regex, self.book_id): self.asin = self.book_id else: raise ValidationError( {'book_id': '{} is an invalid ASIN'.format(self.book_id)}) else: raise ValidationError({ 'book_id_type': 'Allowed Book Id types: {}'.format(self.allowed_book_id_types) })
def clean(self, value): value = super(ISBNField, self).clean(value) value = ''.join(value.split('-')) try: if not pyisbn.validate(value): raise ValidationError('Invalid ISBN') except (TypeError, ValueError), e: raise ValidationError, str(e)
def validate_identifiers(self, data={}): """ Identifier verification: types can only be used once """ if 'label' in data and data['label'] == 'ISBN': isbn = data.get('value') if not isbn: return try: isbn_ok = pyisbn.validate(isbn) except ValueError, e: isbn_ok = False if not isbn_ok: return 'Invalid ISBN %s' % isbn
def search(request): results = { 'sources': {}, 'error': {}, } ''' more: false, results: [ { text: "Western", children: [ { id: "CA", text: "California" }, { id: "AZ", text: "Arizona" } ] }, { text: "Eastern", children: [ { id: "FL", text: "Florida" } ] } ] ''' title = request.QUERY_PARAMS.get('title', False) if title: sources = Source.objects.filter(title__icontains=title) source_serializer = SourceSerializer(sources, many=True) results['sources']['spuqi'] = source_serializer.data isbn_str = request.QUERY_PARAMS.get('isbn', False) if isbn_str: try: pyisbn.Isbn(isbn_str) except pyisbn.IsbnError: results['error'] = { 'message': _('ISBN number must contain only digit-numbers'), } else: isbn_number = pyisbn.convert(isbn_str) if not pyisbn.validate(isbn_number): results['error'] = { 'message': _('A valid ISBN number is required') } #try: # if googlebooks_api.list('isbn:%s' % isbn)['totalItems'] > 0: # results['sources']['googlebooks'] = googlebooks_api.list( # 'isbn:%s' % isbn)['items'] results['sources']['googlebooks'] = googlebooks_api.list( 'isbn:%s' % isbn_number) # except ConnectionError: # results.errors.append(_('Could not connect to Google Book API')) return Response({"results": results})
def get_by_isbn(cls, isbn, create=True, defaults=None): if len(isbn) == 10: # NOTE: always use ISBN-13 # this should also raise an exception if isbn is invalid isbn = pyisbn.convert(isbn) if not pyisbn.validate(isbn): raise ValueError("Invalid ISBN: %s" % isbn) memkey = "book:%s" % isbn book = memcache.get(memkey) if book: return book if not create: book = Book.get_by_key_name(memkey) else: defaults = defaults or {} defaults["isbn13"] = isbn book = Book.get_or_insert(memkey, **defaults) if book: memcache.set(book.memkey, book, Book.MEMCACHE_TTL) return book
def filter_language(): import pyisbn with open('isbn2ind.pickle', 'rb') as f: isbn2ind = pickle.load(f) with open('isbn2lang.pickle', 'rb') as f: isbn2lang = pickle.load(f) with open('isbn2ratings_count4.pickle', 'rb') as f: isbn2ratings_count = pickle.load(f) with open('isbn2rating4.pickle', 'rb') as f: isbn2ratings = pickle.load(f) with open('isbn2title4.pickle', 'rb') as f: isbn2title = pickle.load(f) with open('isbn2authors4.pickle', 'rb') as f: isbn2authors = pickle.load(f) isbns = [] # rating_counts = list(isbn2ratings.values()) # list(isbn2ratings_count.values()) # print('max rating', max(rating_counts)) # plt.hist(rating_counts, bins=np.arange(0, 20, 1)) # plt.show() for isbn in tqdm(isbn2ind): lang = isbn2lang[isbn] rating_count = isbn2ratings_count[isbn] rating = isbn2ratings[isbn] title = isbn2title[isbn] # print(lang) if isbn in ['ordernumber85'] or 'http:' in isbn: continue try: if not pyisbn.validate(isbn): continue except: continue if rating_count > 230 and ( lang == 'en') and 3.8 <= rating <= 20 and len(title) > 1: isbns.append(isbn) print('num_left', len(isbns)) titles = set() new_isbns = [] for isbn in tqdm(isbns): title = isbn2title[isbn].lower() # if a replace ind is None because rating is smaller # than similar book, then add is false if title in titles: continue else: titles.add(title) new_isbns.append(isbn) print(titles) # for isbn in tqdm(isbns): # title = isbn2title[isbn].lower() # rating_count = isbn2ratings_count[isbn] # author = isbn2authors[isbn] # replace_ind = None # # if a replace ind is None because rating is smaller # # than similar book, then add is false # add = True # for i, isbn1 in enumerate(new_isbns): # title1 = isbn2title[isbn1].lower() # author1 = isbn2authors[isbn1] # if author1 != author: # continue # if fuzz.ratio(title, title1) > 93: # rating_count1 = isbn2ratings_count[isbn1] # if rating_count > rating_count1: # replace_ind = i # add = False # # print(title1, ',', title) # break # if replace_ind is not None: # new_isbns[replace_ind] = isbn # elif add: # new_isbns.append(isbn) isbns = sorted(new_isbns) # isbns = sorted(isbns) isbn2ind = {isbn: ind for ind, isbn in enumerate(isbns)} # print(isbn2ind.keys()) print('num_left', len(isbns)) with open('isbn2ind.pickle', 'wb') as f: pickle.dump(isbn2ind, f, protocol=pickle.HIGHEST_PROTOCOL)
def validate_isbn(isbn): if not pyisbn.validate(isbn): raise ValidationError(u'{} is not a valid ISBN number'.format(isbn))
def test_validate_invalid(isbn): assert validate(isbn) is False
def boolvalidate(isbn): try: p = pyisbn.validate(isbn) except: return False return p
def validate(isbn): try: return pyisbn.validate(isbn) except: return False
def validate_isbn(isbn): if not pyisbn.validate(isbn): raise ValidationError('Nieprawidłowy numer ISBN!')
def test_validate(isbn): expect(validate(isbn)) == True
def isbn_validator(isbn): try: pyisbn.validate(isbn) except pyisbn.IsbnError as err: raise ValidationError(err)
def test_validate_invalid(isbn): expect(validate(isbn)) == False
def test_validate(isbn): assert validate(isbn)