Exemplo n.º 1
0
    def download(self, lastNchapter=-1):
        if self.url is None:
            print("Error, no url loaded")
            exit(1)
        else:
            self.bookTitle = wp.getBookTitle(self.url)

        if 'html' in self.url:
            print('Menu URL error')
            exit(0)

        self.updatePath()

        chList = wp.getChapterList(self.url)
        currentLength = len(chList)

        if lastNchapter == -1:
            # 检查上次更新长度
            lastTimeLength = self.checkLog()
            # 检查结束
        else:
            lastTimeLength = int(currentLength) - int(lastNchapter) - 1

        if lastTimeLength > currentLength:
            print('Unexpected list length, check lastTimeLength')
            exit(1)
        elif lastTimeLength == currentLength:
            print(self.bookTitle + ' --> 未更新' + str(currentLength))
            exit(0)

        fileName = str(lastTimeLength +
                       1) + '-' + str(currentLength) + ' ' + self.bookTitle

        f = open(self.path + fileName + '.txt', 'a')

        for i in range(lastTimeLength, currentLength):
            if i == 0:
                f.write(self.bookTitle + '\n\n')
            f.write(wp.downloadFromPage(wp.rootUrl + chList[i]['href']) + '\n')
            print("\rDownloading " + self.bookTitle + ":" + str(
                int((i - lastTimeLength) * 100.0 /
                    (currentLength - lastTimeLength - 1))) + ' %',
                  end='',
                  flush=True)
            #sys.stdout.flush()
        sys.stdout.write('\n\n 下载完成:' + self.bookTitle + '\n\n')
        f.close()
        # 记录当前长度
        self.log(currentLength)
        return fileName + '.txt', self.path + fileName + '.txt'