def StartLoadBookInfo(self, msg="", bookId="", isBack=True): info = self.downloadDict.get(bookId) if not info: return assert isinstance(info, DownloadInfo) if not isBack: self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookById(bookId, x), self.StartLoadBookInfo, bookId) elif msg != Status.Ok: info.resetCnt += 1 if info.resetCnt >= config.ResetCnt: info.status = DownloadStatus.Error else: self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookById(bookId, x), self.StartLoadBookInfo, bookId) else: info.resetCnt = 0 book = BookMgr().books.get(bookId) info.status = DownloadStatus.ReadingEps info.title = book.title info.savePath = os.path.join( os.path.join(config.SavePath, config.SavePathDir), info.title.replace("/", "").replace("|", "").replace( "*", "").replace("\\", "").replace("?", "").replace( ":", "").replace("*", "").replace("<", "").replace( ">", "").replace("\"", "")) info.pagesCount = book.pagesCount info.epsCount = book.epsCount self.StartLoadEpsInfo(bookId=bookId, isBack=False) self.UpdateTableItem(info)
def OpenBookBack(self, msg): self.loadingForm.close() self.listWidget.UpdatePage(1, 1) self.childrenListWidget.UpdatePage(1, 1) self.childrenListWidget.UpdateState() self.listWidget.UpdateState() self.categoriesList.clear() self.tagsList.clear() self.autorList.clear() info = BookMgr().books.get(self.bookId) if msg == Status.Ok and info: # self.autor.setText(info.author) self.autorList.AddItem(info.author) self.title.setText(info.title) self.bookName = info.title self.description.setText(info.description) self.isFinished.setText("完本" if info.finished else "未完本") for name in info.categories: self.categoriesList.AddItem(name) # self.categories.setText(','.join(info.categories)) # self.tags.setText(','.join(info.tags)) for name in info.tags: self.tagsList.AddItem(name) self.likes.setText(str(info.totalLikes)) self.views.setText(str(info.totalViews)) if info.isFavourite: self.favorites.setEnabled(False) else: self.favorites.setEnabled(True) self.picture.setText("图片加载中...") fileServer = info.thumb.get("fileServer") path = info.thumb.get("path") name = info.thumb.get("originalName") self.url = fileServer self.path = path timeArray, day = ToolUtil.GetDateStr(info.updated_at) self.updateTick.setText(str(day) + "天前更新") if config.IsLoadingPicture: self.owner().qtTask.AddDownloadTask( fileServer, path, completeCallBack=self.UpdatePicture, cleanFlag=self.closeFlag) self.owner().qtTask.AddHttpTask(lambda x: Server().Send( req.GetComments(self.bookId), bakParam=x), self.GetCommnetBack, cleanFlag=self.closeFlag) self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookEpsInfo(self.bookId, x), self.GetEpsBack, cleanFlag=self.closeFlag) self.startRead.setEnabled(False) else: # QtWidgets.QMessageBox.information(self, '加载失败', msg, QtWidgets.QMessageBox.Yes) self.msgForm.ShowError(msg) self.hide() return
def StartLoadEpsInfo(self, msg="", bookId="", isBack=True): info = self.downloadDict.get(bookId) if not info: return assert isinstance(info, DownloadInfo) if not isBack: self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookEpsInfo(bookId, x), self.StartLoadEpsInfo, bookId) elif msg != Status.Ok: info.resetCnt += 1 if info.resetCnt >= config.ResetCnt: info.status = DownloadStatus.Error else: self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookEpsInfo(bookId, x), self.StartLoadEpsInfo, bookId) else: info.resetCnt = 0 book = BookMgr().books.get(bookId) info.title = book.title info.pagesCount = book.pagesCount info.epsCount = book.epsCount info.status = DownloadStatus.ReadingPicture # 先准备好第一章 self.StartLoadPicUrl(bookId=bookId, isBack=False) self.HandlerDownloadList() self.UpdateTableItem(info)
def OpenEpsInfo(self, bookId): self.show() self.loadingForm.show() self.bookId = bookId self.epsListWidget.clear() if bookId not in BookMgr().books: self.owner().qtTask.AddHttpTask(lambda x: BookMgr().AddBookById(self.bookId, x), self.OpenBookInfoBack, cleanFlag=self.closeFlag) else: self.owner().qtTask.AddHttpTask(lambda x: BookMgr().AddBookEpsInfo(self.bookId, x), self.OpenEpsInfoBack, cleanFlag=self.closeFlag)
def CheckLoadPicture(self): i = 0 for i in range(self.curIndex, self.curIndex + self.maxPreLoad): if i >= self.maxPic: continue if i < self.curPreLoadIndex: continue bookInfo = BookMgr().books.get(self.bookId) epsInfo = bookInfo.eps[self.epsId] picInfo = epsInfo.pics[i] if i not in self.pictureData: # 防止重复请求 if i not in self.waitPicData: QtTask().AddDownloadTask(picInfo.fileServer, picInfo.path, completeCallBack=self.CompleteDownloadPic, backParam=i, isSaveCache=True, cleanFlag=self.closeFlag) self.waitPicData.add(i) elif config.IsOpenWaifu and i not in self.waitWaifuPicData: if self.indexToWaifu2xId.get(self.curIndex) not in self.convertData: data = self.pictureData[i] QtTask().AddConvertTask(picInfo.fileServer, picInfo.path, data, self.Waifu2xBack, i, self.closeFlag) self.waitWaifuPicData.add(i) self.curPreLoadIndex = max(i, self.curPreLoadIndex) pass
def AddBookEpsInfosBack(self, msg): if msg != Status.Ok: self.SetStatu(self.Error) return else: book = BookMgr().books.get(self.bookId) assert isinstance(book, Book) for epsId in self.downloadEpsIds: if epsId >= len(book.eps): continue epsInfo = book.eps[epsId] if epsId not in self.epsInfo: info = DownloadEpsInfo(self) info.bookId = self.bookId info.epsId = epsId info.epsTitle = epsInfo.title self.epsInfo[epsId] = info self.parent.db.AddDownloadEpsDB(info) if not self.epsInfo: self.SetStatu(self.Error) return if self.curDownloadEpsId < 0 or self.curDownloadEpsId not in self.epsInfo: self.curDownloadEpsId = min(self.downloadEpsIds) self.curDownloadEpsInfo = self.epsInfo.get(self.curDownloadEpsId) self.curDownloadEpsInfo.AddBookEpsPicInfos() return
def OpenPage(self, bookId, epsId, name): if not bookId: return self.Clear() info = BookMgr().books.get(bookId) if info: self.category = info.tags[:] self.qtTool.checkBox.setChecked(config.IsOpenWaifu) self.qtTool.SetData(isInit=True) self.graphicsItem.setPixmap(QPixmap()) self.qtTool.SetData() self.qtTool.show() self.bookId = bookId self.epsId = epsId self.graphicsItem.setPos(0, 0) # historyInfo = self.owner().historyForm.GetHistory(bookId) # if historyInfo and historyInfo.epsId == epsId: # self.curIndex = historyInfo.picIndex # else: # self.AddHistory() # self.AddHistory() self.loadingForm.show() self.StartLoadPicUrl() self.setWindowTitle(name) self.show()
def __call__(self, backData): from src.index.book import BookMgr st = BookMgr().AddBookEpsPicInfoBack(backData) if st == Status.WaitLoad: return if backData.bakParam: QtTask().taskBack.emit(backData.bakParam, st)
def StartLoadPicUrlBack(self, msg, bookId): if msg != Status.Ok: self.StartLoadPicUrl() else: bookInfo = BookMgr().books.get(self.bookId) epsInfo = bookInfo.eps[self.epsId] self.maxPic = len(epsInfo.pics) self.CheckLoadPicture() return
def StartLoadPicUrl(self, msg="", bookId="", isBack=True): info = self.downloadDict.get(bookId) if not info: return assert isinstance(info, DownloadInfo) if not isBack: # 如果已经加载好了 if info.preDownloadEps > info.epsCount: if info.status not in [ DownloadStatus.Success, DownloadStatus.Downloading ]: info.status = DownloadStatus.Waiting return self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookEpsPicInfo( bookId, info.preDownloadEps, x), self.StartLoadPicUrl, bookId) elif msg != Status.Ok: info.resetCnt += 1 if info.resetCnt >= config.ResetCnt: info.status = DownloadStatus.Error else: self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookEpsPicInfo( bookId, info.preDownloadEps, x), self.StartLoadPicUrl, bookId) else: info.resetCnt = 0 info.preDownloadEps += 1 if info.status == DownloadStatus.Downloading: # 加载完成了 if info.preDownloadEps > info.epsCount: return # 加载下一章 self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookEpsPicInfo( bookId, info.preDownloadEps, x), self.StartLoadPicUrl, bookId) elif info.status == DownloadStatus.ReadingPicture: info.status = DownloadStatus.Waiting self.HandlerDownloadList() self.UpdateTableItem(info)
def StartDownload(self): bookInfo = BookMgr().books.get(self.parent.bookId) epsInfo = bookInfo.eps[self.epsId] if self.curPreDownloadIndex >= len(epsInfo.pics): self.status = DownloadInfo.Success self.curPreDownloadIndex = 0 self.parent.db.AddDownloadEpsDB(self) self.parent.SwithNextDownload() else: self.parent.UpdateTableItem() self.AddDownload() return
def OpenBook(self, bookId): self.bookId = bookId self.setWindowTitle(self.bookId) # if self.bookId in self.owner().downloadForm.downloadDict: # self.download.setEnabled(False) # else: # self.download.setEnabled(True) self.Clear() self.show() self.loadingForm.show() self.owner().qtTask.AddHttpTask( lambda x: BookMgr().AddBookById(bookId, x), self.OpenBookBack)
def AddDownload(self): bookInfo = BookMgr().books.get(self.parent.bookId) epsInfo = bookInfo.eps[self.epsId] picInfo = epsInfo.pics[self.curPreDownloadIndex] self.picCnt = len(epsInfo.pics) if os.path.isfile(self.curSavePath): self.curPreDownloadIndex += 1 return self.StartDownload() QtTask().AddDownloadTask(picInfo.fileServer, picInfo.path, downloadCallBack=self.AddDownloadBack, completeCallBack=self.AddDownloadCompleteBack, isSaveCache=False, cleanFlag="download_".format(self.parent.bookId))
def AddBookInfosBack(self, msg=""): if msg != Status.Ok: self.SetStatu(self.Error) return else: book = BookMgr().books.get(self.bookId) self.title = book.title self.savePath = os.path.join(os.path.join(config.SavePath, config.SavePathDir), ToolUtil.GetCanSaveName(self.title)) self.savePath = os.path.join(self.savePath, "原图") self.convertPath = os.path.join(os.path.join(config.SavePath, config.SavePathDir), ToolUtil.GetCanSaveName(self.title)) self.convertPath = os.path.join(self.convertPath, "waifu2x") self.AddBookEpsInfos() return
def AddDownload(self): bookInfo = BookMgr().books.get(self.parent.bookId) epsInfo = bookInfo.eps[self.epsId] isDownloadNext = True while self.curPreDownloadIndex < len(epsInfo.pics): bookInfo = BookMgr().books.get(self.parent.bookId) epsInfo = bookInfo.eps[self.epsId] picInfo = epsInfo.pics[self.curPreDownloadIndex] self.picCnt = len(epsInfo.pics) if os.path.isfile(self.curSavePath): self.curPreDownloadIndex += 1 else: isDownloadNext = False QtTask().AddDownloadTask( picInfo.fileServer, picInfo.path, downloadCallBack=self.AddDownloadBack, completeCallBack=self.AddDownloadCompleteBack, isSaveCache=False, cleanFlag="download_{}".format(self.parent.bookId)) break self.parent.UpdateTableItem() if isDownloadNext: self.StartDownload()
def UpdateEpsInfo(self): self.epsListWidget.clear() info = BookMgr().books.get(self.bookId) if not info: return downloadEpsId = self.owner().downloadForm.GetDownloadEpsId(self.bookId) for index, epsInfo in enumerate(info.eps): label = QLabel(epsInfo.title) label.setContentsMargins(20, 10, 20, 10) item = QListWidgetItem(self.epsListWidget) item.setSizeHint(label.sizeHint()) if index in downloadEpsId: item.setBackground(self.greed) else: item.setBackground(self.white) self.epsListWidget.setItemWidget(item, label)
def OpenNextEps(self): epsId = self.readImg.epsId bookId = self.readImg.bookId bookInfo = BookMgr().books.get(bookId) epsId += 1 if epsId >= len(bookInfo.eps): QtBubbleLabel.ShowMsgEx(self.readImg, "已经是最后一章") return if epsId >= len(bookInfo.eps): return epsInfo = bookInfo.eps[epsId] self.readImg.AddHistory() self.readImg.owner().bookInfoForm.LoadHistory() self.readImg.OpenPage(bookId, epsId, epsInfo.title) return
def StartDownload(self, task): assert isinstance(task, DownloadInfo) bookInfo = BookMgr().books.get(task.bookId) picInfo = None st = None while task.downloadEps <= task.epsCount: epsInfo = bookInfo.eps[task.downloadEps - 1] # 图片路径还没有加载好 if not epsInfo.pics: st = Status.WaitLoad break if task.downloadCount > len(epsInfo.pics): task.downloadCount = 1 task.downloadEps += 1 else: picInfo = epsInfo.pics[task.downloadCount - 1] st = Status.Ok break if st == Status.Ok: task.saveName = picInfo.originalName self.owner().qtTask.AddDownloadTask( picInfo.fileServer, picInfo.path, downloadCallBack=self.DownloadCallBack, completeCallBack=self.CompleteDownloadPic, backParam=task.bookId, isSaveCache=False) task.allDownloadCount += 1 else: if st == Status.WaitLoad: task.status = DownloadStatus.ReadingPicture self.StartLoadPicUrl(bookId=task.bookId, isBack=False) self.downloadList.append(task) self.HandlerDownloadList() else: task.status = DownloadStatus.Success self.UpdateTable() self.downloadingList.remove(task) self.SaveSetting(task.bookId, task) self.UpdateTableItem(task)
def CheckLoadPicture(self): i = 0 for i in range(self.curIndex, self.curIndex + self.maxPreLoad): if i >= self.maxPic: continue bookInfo = BookMgr().books.get(self.bookId) epsInfo = bookInfo.eps[self.epsId] picInfo = epsInfo.pics[i] if i not in self.pictureData: # 防止重复请求 if i not in self.waitPicData: self.AddDownloadTask(i, picInfo) elif config.IsOpenWaifu and i not in self.waitWaifuPicData: if not self.pictureData[i].data: continue if not self.pictureData[i].waifuData: self.AddCovertData(picInfo, i) pass
def UpdateEpsData(self): self.epsListWidget.clear() info = BookMgr().books.get(self.bookId) if not info: return self.startRead.setEnabled(True) downloadIds = self.owner().downloadForm.GetDownloadCompleteEpsId( self.bookId) for index, epsInfo in enumerate(info.eps): label = QLabel(epsInfo.title) label.setContentsMargins(20, 10, 20, 10) item = QListWidgetItem(self.epsListWidget) if index in downloadIds: item.setBackground(QColor(18, 161, 130)) else: item.setBackground(QColor(0, 0, 0, 0)) item.setSizeHint(label.sizeHint()) self.epsListWidget.setItemWidget(item, label) self.tabWidget.setTabText(0, "章节({})".format(str(len(info.eps)))) return
def CompleteDownloadPic(self, data, st, index): self.loadingForm.close() p = self.pictureData.get(index) if not p: p = QtFileData() self.pictureData[index] = p bookInfo = BookMgr().books.get(self.bookId) epsInfo = bookInfo.eps[self.epsId] picInfo = epsInfo.pics[index] self.waitPicData.discard(index) if st != Status.Ok: p.state = p.DownloadReset self.AddDownloadTask(index, picInfo) else: p.SetData(data, self.category) if config.IsOpenWaifu: self.AddCovertData(picInfo, index) if index == self.curIndex: self.ShowImg() return
def CompleteDownloadPic(self, data, st, index): self.loadingForm.close() p = FileData() bookInfo = BookMgr().books.get(self.bookId) epsInfo = bookInfo.eps[self.epsId] picInfo = epsInfo.pics[index] self.waitPicData.discard(index) if st != Status.Ok: p.state = p.DownloadReset QtTask().AddDownloadTask(picInfo.fileServer, picInfo.path, completeCallBack=self.CompleteDownloadPic, backParam=index, isSaveCache=True, cleanFlag=self.closeFlag) self.waitPicData.add(index) else: p.SetData(data) p.state = p.DownloadSuc self.pictureData[index] = p if config.IsOpenWaifu: waifu2xId = QtTask().AddConvertTask(picInfo.fileServer, picInfo.path, data, self.Waifu2xBack, index, self.closeFlag) self.waitWaifuPicData.add(index) if index == self.curIndex: self.ShowImg() return
def StartLoadPicUrl(self): QtTask().AddHttpTask(lambda x: BookMgr().AddBookEpsPicInfo( self.bookId, self.epsId + 1, x), self.StartLoadPicUrlBack, self.bookId, cleanFlag=self.closeFlag)
def AddBookEpsInfos(self): self.SetStatu(self.ReadingEps) QtTask().AddHttpTask(lambda x: BookMgr().AddBookEpsInfo(self.bookId, x), self.AddBookEpsInfosBack)
def OpenBookInfoBack(self, msg): if msg == Status.Ok: self.owner().qtTask.AddHttpTask(lambda x: BookMgr().AddBookEpsInfo(self.bookId, x), self.OpenEpsInfoBack, cleanFlag=self.closeFlag) else: self.loadingForm.close()
def AddBookEpsPicInfos(self): self.parent.SetStatu(DownloadInfo.ReadingPicture) QtTask().AddHttpTask(lambda x: BookMgr().AddBookEpsPicInfo(self.parent.bookId, self.epsId+1, x), self.AddBookEpsPicInfosBack)
def __call__(self, backData): from src.index.book import BookMgr st = BookMgr().AddBookByIdBack(backData) if backData.bakParam: QtTask().taskBack.emit(backData.bakParam, st)