def _try_fetch_render(self, douban_id): """ Try fetching a book from douban. If no exception raised, render it. """ try: b = douban.get_book_all_by_id(douban_id, self.user) b.merge_into_datastore(self.user) except utils.errors.FetchDataError: # No such book, display its original link self._render_no_such_book(douban_id) except utils.errors.ParseJsonError: # This is something I should care about, some cases are not covered msg = "Error PARSING book information of douban_id: " + douban_id url = "/error?" + urllib.urlencode({'msg': msg}) self.redirect(url) else: self.redirect('/book/%s' % b.book.isbn) return
def _load(self, user, isbn, reload, tjlib=False): """ Load a book from datastore or from douban. @param reload: directly load from douban, no matter whether there is in datastore. @param tjlib: whether to also load Tongji Library status when reloading. @return: a BookRelated object """ if not reload: # everything here return books.BookRelated.get_by_user_isbn(user, isbn) basic_book = douban.get_book_by_isbn(isbn) full = douban.get_book_all_by_id(basic_book.douban_id, user) # douban API may have a bug: not providing summary somehow full.book.summary = basic_book.summary full.merge_into_datastore(user) if tjlib: url, datas = tongji.get_by_isbn(isbn) full.book.set_tongji_info(url, datas) return full
def get(self): action = self.request.get('action') if action == 'clear': # clear all data in db msg = self._clear() user = None elif action == 'load': email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return # load books from douban # these books are for defense only.. ids = ["20381804", "10555435", "1432683"] for douban_id in ids: if Book.get_by_douban_id(douban_id) is None: # load it! b = douban.get_book_all_by_id(douban_id, user) b.merge_into_datastore(user) msg = "DONE" else: # default case email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return msg = self._test(user) template = utils.get_jinja_env().get_template('test.html') context = { 'user': user, 'msg': msg } self.response.out.write(template.render(context)) return