Пример #1
0
 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)
         })
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
    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
Пример #5
0
    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
Пример #6
0
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})
Пример #7
0
    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
Пример #8
0
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)
Пример #9
0
def validate_isbn(isbn):
    if not pyisbn.validate(isbn):
        raise ValidationError(u'{} is not a valid ISBN number'.format(isbn))
Пример #10
0
def test_validate_invalid(isbn):
    assert validate(isbn) is False
Пример #11
0
 def boolvalidate(isbn):
     try:
         p = pyisbn.validate(isbn)
     except:
         return False
     return p
Пример #12
0
def validate(isbn):
    try:
        return pyisbn.validate(isbn)
    except:
        return False
Пример #13
0
def validate_isbn(isbn):
    if not pyisbn.validate(isbn):
        raise ValidationError('Nieprawidłowy numer ISBN!')
Пример #14
0
def test_validate(isbn):
    expect(validate(isbn)) == True
Пример #15
0
def isbn_validator(isbn):
    try:
        pyisbn.validate(isbn)
    except pyisbn.IsbnError as err:
        raise ValidationError(err)
Пример #16
0
def test_validate_invalid(isbn):
    expect(validate(isbn)) == False
Пример #17
0
def test_validate(isbn):
    assert validate(isbn)