def search_by_keyword(self, keyword, page=1): """ 根据关键字搜索 :param keyword: 关键字 :param page: 页码 :return: """ # 先检索数据库,再通过API搜索 books = Book.query.filter( or_(Book.title.contains(keyword), Book.author.contains(keyword))).all() # 分页获取数据 page_books = books[self.calculate_start(page):( page * current_app.config['PER_PAGE'])] if page_books: data = [] for book in page_books: data.append(book.__dict__) result = {'books': data, 'total': len(books)} else: url = self.keyword_url.format(keyword, current_app.config['PER_PAGE'], self.calculate_start(page)) result = HTTP.get(url) self.__save(result) self.__fill_collection(result)
def search_by_isbn(self, isbn): """ 根据isbn搜索 :param isbn: isbn号 :return: """ # 先检索数据库,再通过API搜索 result = Book.query.filter_by(isbn=isbn).first() if result: result = result.__dict__ else: url = self.isbn_url.format(isbn) result = HTTP.get(url) self.__save(result) self.__fill_single(result)
def search_by_keyword(self, keyword, page=1): page = int(page) url = YuShuBook.keyword_url.format(keyword, current_app.config['PER_PAGE'], self.calculate_start(page)) result = HTTP.get(url) self._fill_collection(result)
def search_by_isbn(self, isbn): url = self.isbn_url.format(isbn) # url = self.isbn_url result = HTTP.get(url) #数据可以缓存在数据库中 self._fill_single(result)
def get_by_api(url): return HTTP.get(url)
def search_by_keyword(self, key, page=1): url = self.keyword_url.format(key, self.per_page, self.calculate_start(page)) result = HTTP.get(url) self.__fill_collection(result)
def search_by_isbn(self, isbn): url = self.isbn_url.format(isbn) result = HTTP.get(url) self.__fill_single(result)