Example #1
0
    def add_chapters_content(self, novel, task=None):
        """
        同步章节内容
        """
        rows = self.db.query(Chapter.id).filter(Chapter.novel_id==novel.id).all() #节约内存,只取ID
        if not rows:
            return False, u'没有可用章节'
            
        count = len(rows)
        p_count = 0
        
        for id in [r[0] for r in rows]:
            chapter = self.db.query(Chapter).get(id)
            if not chapter:
                continue
            if chapter.content: #已同步的跳过
                continue
            parser = ChapterParser(chapter)
            try:
                result, chapter = parser.execute()
                if not result:
                    continue
                self.db.add(chapter)
            except:
                self.logger.error('get chapter content error|%s|%s|%s|' %(novel.id, chapter.id, chapter.pageid), exc_info=1)

            # 设置进度
            p_count += 1
            if task:
                task.update_state(state='PROGRESS', meta={'percent': min(p_count/float(count), 1.0) * 100})

        self.db.commit()
        return True, ''
Example #2
0
 def test_execute_2(self):
     """
     """
     novel = self.db_add_novel(name=u'则添加', rule=u'')
     chapter = self.db_add_chapter(title=u'测试', pageid=u'3848313003', novel_id=novel.id)
     parser = ChapterParser(chapter)
     result, chapter = parser.execute()
     self.assertTrue(result)
     self.assertTrue(chapter.content)
Example #3
0
 def test_execute(self):
     """
     """
     novel = self.db_add_novel(name=u'则添加', rule=u'')
     chapter = self.db_add_chapter(title=u'【择天记】第一卷 第二百四十章 杂物间的大老鼠', pageid=u'3406030297', novel_id=novel.id)
     parser = ChapterParser(chapter)
     result, chapter = parser.execute()
     self.assertTrue(result)
     self.assertTrue(chapter.content)