Beispiel #1
0
    def getBookCollections(self):
        results = self.service.GetBookCollections(self.api.url)
        if results:
            collections, count, user = results
        else:
            return False

        if not user or not collections:
            return False
        collection_datas = []
        for collection in collections:
            collection_data = collection.__dict__.get('_data')
            for key, value in collection_data.items():
                if value is None:
                    collection_data.pop(key)
            collection_data['author'] = DBRef('user', user.id)
            if collection.book:
                #添加该书到book_tag集合
                db = MongoDB.getConnection('mining')
                for tag in collection.tags:
                    db.book_tag.update({'title':tag},
                                {'$addToSet':{'books':collection.book.bid},
                                 '$inc':{'count':1}},
                                 upsert=True)
                collection_data['book'] = DBRef('book', collection.book.id)
            collection_datas.append(collection_data)
        
        if collection_datas:
            ids = self.db.book_collection.insert(collection_datas, check_keys=False)
            for index, object_id in enumerate(ids):
                collections[index].id = object_id
            
            try:
                user_book_related = None
                user_book_related, created = UserBookRelated.objects.get_or_create(\
                        uid=self.api.api_id, auto_save=False, defaults=\
                        {'uid':self.api.api_id, 'book_collections':collections})
                if not created:
                    user_book_related.book_collections.extend(collections)
                try:
                    user_book_related.save()
                except mongoengine.base.ValidationError, error:
                    logger.debug("[ERROR]%s: %s when save %s at api:%s" %(error, \
                            error.to_dict(), user_book_related, self.api.url))
            except Exception, error:
                logger.debug("[ERROR]%s: UserBookRelated.objects.get_or_create(uid=%s ..."\
                        %(error, self.api.api_id))
        
            if user_book_related and created:
                try:
                    user.user_book_related = user_book_related
                    user.save()
                except mongoengine.base.ValidationError, error:
                    logger.debug("[ERROR]%s: %s when save %s at api:%s" \
                                 %(error, error.to_dict(), user, self.api.url))
Beispiel #2
0
 def getBookTags(self):
     results = self.service.GetBookTags(self.api.url)
     if results:
         book_tags, count = results
     else:
         return False
     
     book = None
     books = Book.objects(bid=self.api.api_id)
     if books:
         book = books[0]
     else:
         return False
     
     if not book_tags:
         return False
     else:
         db = MongoDB.getConnection('mining')
         for tag in book_tags:
             db.book_tag.update({'title':tag.title},
                                {'$addToSet':{'books':book.bid},
                                 '$inc':{'count':tag.count}},
                                upsert=True)
     try:
         book_related = None
         book_related, created = BookRelated.objects.get_or_create(\
                 bid=book.bid, auto_save=False, defaults=\
                 {'bid':book.bid, 'book':book, 'tags':book_tags})
         if not created:
             book_related.tags.extend(book_tags)
         try:
             book_related.save()
         except mongoengine.base.ValidationError, error:
             logger.debug("[ERROR]%s: %s when save %s at api:%s" %(error, \
                     error.to_dict(), book_related, self.api.url))
     except Exception, error:
         logger.debug("[ERROR]%s: BookRelated.objects.get_or_create(uid=%s ..."\
                 %(error, self.api.api_id))
Beispiel #3
0
 def __init__(self, api_type):
     self.thread_stop = False
     self.sleep_count = 0
     self.db = MongoDB.getConnection()
     self.api_type = api_type
Beispiel #4
0
 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()
Beispiel #5
0
 def __init__(self, uid):
     threading.Thread.__init__(self)
     self.uid = uid
     self.mining_db = MongoDB.getConnection('mining')
     self.recommend_db = MongoDB.getConnection('recommend')