def _import_worker(user_key, list_type): """ Called in Task Queue, for importing from douban. Since there may be lots of information to import, it may take some time. Doing this in Task Queue is more proper. @param user_key: DO NOT pass the user, otherwise it would raise exceptions (due to cache??) @param list_type: for subclasses to reuse """ user = auth.user.User.get(user_key) try: all_book_related = douban.get_book_list(user, list_type) except utils.errors.ParseJsonError as err: logging.error("ERROR while importing from Douban, user_key: " + user_key + " list_type: " + list_type) logging.error(err) else: helper = books.SortHelper(user) bl = booklist.BookList.get_or_create(user, list_type) bl.start_importing(len(all_book_related)) # also clear those in memcache helper.clear(list_type) for related in all_book_related: # also added into memcache in merge_into_datastore() b = related.merge_into_datastore(user, update_book=False) if b: # when already such book there, b will be None url, datas = tongji.get_by_isbn(b.isbn) b.set_tongji_info(url, datas) # has to re-get this instance, for it is retrieved inside merge_into_datastore() # the current instance may not be up-to-date bl = booklist.BookList.get_or_create(user, list_type) bl.finish_importing()
def _edit_tongji(self): # no need to set self.edited to True, because this doesn't need sync to douban try: url, datas = tongji.get_by_isbn(self.isbn) b = Book.get_by_isbn(self.isbn) b.set_tongji_info(url, datas) except Exception: # there may be errors, like this book 9787544717731, it also has e-version in TJ Lib.. pass
def post(self): isbn = self.request.get('isbn') if not isbn: return url, datas = tongji.get_by_isbn(isbn) b = Book.get_by_isbn(isbn) if b: b.set_tongji_info(url, datas) return
def _refresh_tj_worker(user_key, list_type): """ Called in Task Queue to refresh all the status of the books in a list. @param user_key: DO NOT pass the user, otherwise it would raise exceptions (due to cache??) @param list_type: for subclasses to reuse """ user = auth.user.User.get(user_key) bl = booklist.BookList.get_by_user_name(user, list_type) for isbn in bl.isbns(): url, datas = tongji.get_by_isbn(isbn) b = books.book.Book.get_by_isbn(isbn) b.set_tongji_info(url, datas)
def _fetch_parse(self, user, list_type=None): """ It may cause problems to fetch and then parse. Do it together.. @param list_type: which booklist to import. """ if list_type is None: list_type = self.request.get('list_type') if not list_type: return try: datas = douban.get_book_list(user, list_type) except utils.errors.ParseJsonError as err: logging.error("ERROR while importing from Douban, user_key: " + user.key() + " list_type: " + list_type) logging.error(err) self._log(err) return bl = BookList.get_or_create(user, list_type) bl.start_importing(len(datas)) # also clear those in memcache helper = SortHelper(user) helper.clear(list_type) for related in datas: # also added into memcache in merge_into_datastore() b = related.merge_into_datastore(user, update_book=False) if b: # when already such book there, b will be None try: url, datas = tongji.get_by_isbn(b.isbn) b.set_tongji_info(url, datas) except Exception as err: logging.error("ERROR while saving TJ info, isbn: " + b.isbn) logging.error(err) self._log(err) # after all, finish importing bl = BookList.get_or_create(user, list_type) bl.douban_amount = None bl.put() 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