class BookProcessor(): """ methods about book data processing """ def __init__(self, api, key='book'): self.api = api self.api_operation = ApiOperation(key, api) self.service = self.api_operation.getService() self.db = MongoDB.getConnection() def apiProcess(self): api_map = { 'BOOK' : lambda: self.getBook(), 'BOOK_COLLECTION' : lambda: self.getBookCollections(), 'BOOK_REVIEWS' : lambda: self.getBookReviews(), 'BOOK_TAGS' : lambda: self.getBookTags(), } return api_map[self.api.api_type]() def getBook(self, oldbook=None): book = self.service.GetBook(self.api.url) if book: if oldbook: oldbook.summary = book.summary oldbook.common_tags = book.common_tags oldbook.rating = book.rating try: oldbook.save() return oldbook except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s at bid:%s" %(error, error.to_dict(), oldbook.bid)) try: book.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s at bid:%s" %(error, error.to_dict(), book.bid)) book_reviews_api = self.api_operation.apiGenerator('BOOK_REVIEWS', book.bid) api = ReviewApi(url=book_reviews_api, api_id=book.bid, api_type='BOOK_REVIEWS') api.save() return book
class UserProcessor(object): """ methods about user data processing """ def __init__(self, api, key='default'): self.api = api self.api_operation = ApiOperation(key, api) self.service = self.api_operation.getService() self.db = MongoDB.getConnection() def __del__(self): pass def apiProcess(self): api_map = { 'USER' : lambda: self.getUser(), 'USER_CONTACTS' : lambda: self.getUserContacts(), 'USER_FRIENDS' : lambda: self.getUserFriends(), 'USER_NOTES' : lambda: self.getUserNotes(), 'USER_RECOMMENDATIONS' : lambda: self.getUserRecommendations(), 'USER_BOOK_REVIEWS' : lambda: self.getUserBookReviews(), 'USER_BOOK_TAGS' : lambda: self.getUserBookTags(), } return api_map[self.api.api_type]() def getUser(self): user = self.service.GetPeople(self.api.url) if user: exist_user = User.objects(uid=user.uid) if not exist_user: try: user.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s at uid:%s" %(error, error.to_dict(), user.uid)) return False else: return exist_user[0] else: