def search_amazon(term): results = [] if term: amazon_results = None if _is_isbn(term): amazon_results = get_by_isbn(term) else: amazon_results = search_by_keywords(term, settings.AMAZON_QUERY_COUNT) if amazon_results: for info in amazon_results: book = Book.objects.create( isbn=info['isbn'], title=info['title'], edition=info['edition'], authors=info['authors'], format=info['format'], publisher=info['publisher'], publication_date=info['publication_date'], list_price=info['list_price'] ) book.save() # todo: move it to task queue try: # save book image to file image_temp = NamedTemporaryFile(delete=True) image_temp.write(urllib2.urlopen(info['image_url']).read()) image_temp.flush() book.image.save(info['image'], File(image_temp), save=True) except Exception: logger.error('unknown exception when saving book image from Amazon') # make book price object BookPrices.objects.create(book=book) results.append(book) return results
def search(request): term = request.GET.get('q', '').strip() amazon_info = [] # bookstore_info = [] book_results = [] if term != '': saved_term = request.session.get('term', '') saved_results = request.session.get('books', []) if saved_term == term and saved_results: book_results = saved_results else: if _is_isbn(term): book_results = Book.objects.filter(isbn__contains=term) if not book_results: logger.error('not result found for isbn') amazon_info = get_by_isbn(term) elif _is_course_name(term): book_results = Book.objects.filter(courses__keywords__icontains=term).distinct() # if not book_results: # bookstore_info = bookstore_info+(fetchBooks(quarter='Summer', course=term)) # bookstore_info = bookstore_info+(fetchBooks(quarter='Autumn', course=term)) # bookstore_info = removeBookDuplicates(bookstore_info) else: book_results = Book.objects.filter(title__icontains=term) # if not book_results: # book_results = Book.objects.filter(courses__name__icontains=term).distinct() if not book_results: pass #amazon_info = search_by_keywords(term, settings.AMAZON_QUERY_COUNT) # if bookstore_info: # logger.info('query through bookstore') # # book_results = [] # for info in bookstore_info: # book, created = Book.objects.get_or_create( # isbn=info['isbn'], # ) # # make book price object # if created: # book.title=info['title'] # book.list_price=info['new_price'] # book.save() # BookPrices.objects.create(book=book, ubookstore_new=info['new_price'], ubookstore_used=info['used_price']) # book_results.append(book) if amazon_info: logger.info('query through amazon') book_results = [] for info in amazon_info: try_book = Book.objects.filter(isbn__contains=info['isbn']) if try_book: book_results.extend(try_book) else: book = Book.objects.create( isbn=info['isbn'], title=info['title'], edition=info['edition'], authors=info['authors'], format=info['format'], publisher=info['publisher'], publication_date=info['publication_date'], list_price=info['list_price'] ) book.save() # todo: move it to task queue try: # save book image to file image_temp = NamedTemporaryFile(delete=True) image_temp.write(urllib2.urlopen(info['image_url']).read()) image_temp.flush() book.image.save(info['image'], File(image_temp), save=True) except Exception: logger.error('unknown exception when saving book image from Amazon') # make book price object BookPrices.objects.create(book=book) book_results.append(book) request.session['term'] = term request.session['books'] = book_results paginator = Paginator(book_results, NUM_PER_PAGE) try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: page_results = paginator.page(page) except (EmptyPage, InvalidPage): page_results = paginator.page(paginator.num_pages) args = { 'term': term, 'books': page_results, 'total': paginator.count, } return render_to_response('search_results.html', args, context_instance=RequestContext(request))