diff --git a/src/addonWindow.py b/src/addonWindow.py index 4983281..9eae2c8 100644 --- a/src/addonWindow.py +++ b/src/addonWindow.py @@ -12,7 +12,7 @@ import time import json -__VERSION__ = 'v5.0.0' +__VERSION__ = 'v5.0.1' MODELNAME = 'Dict2Anki_NEW' DICTIONARYLIST = ['Youdao', 'Eudict'] @@ -160,6 +160,28 @@ def OnClick(self): dictWorker.moveToThread(dictWorkerThread) dictWorkerThread.start() + @pyqtSlot(object) + def query(self, remoteWords): + self.log(f"远程单词本:{remoteWords}") + _, t = self.threadList[0] + while not t.isFinished(): + t.wait(1) + t.quit() + mw.app.processEvents() + + needToQueryWords = self.compare(remoteWords) + + queryThread = QThread() + queryWorker = api.YoudaoAPI(needToQueryWords, api.YoudaoParser) + self.threadList.append((queryWorker, queryThread)) + queryWorker.SIG.progress.connect(self.updateProgress) + queryWorker.SIG.totalTasks.connect(self.ui.progressBar.setMaximum) + queryWorker.SIG.wordsReady.connect(self.addNote) + queryWorker.SIG.log.connect(self.log) + queryWorker.moveToThread(queryThread) + queryThread.started.connect(queryWorker.run) + queryThread.start() + def compare(self, remoteWordList): localWordList = cardManager.getDeckWordList( deckName=self.ui.deckComboBox.currentText(), @@ -192,28 +214,6 @@ def compare(self, remoteWordList): return needToAddWords - @pyqtSlot(object) - def query(self, remoteWords): - self.log(f"远程单词本:{remoteWords}") - _, t = self.threadList[0] - while not t.isFinished(): - t.wait(1) - t.quit() - mw.app.processEvents() - - needToQueryWords = self.compare(remoteWords) - - queryThread = QThread() - queryWorker = api.YoudaoAPI(needToQueryWords, api.YoudaoParser) - self.threadList.append((queryWorker, queryThread)) - queryWorker.SIG.progress.connect(self.updateProgress) - queryWorker.SIG.totalTasks.connect(self.ui.progressBar.setMaximum) - queryWorker.SIG.wordsReady.connect(self.addNote) - queryWorker.SIG.log.connect(self.log) - queryWorker.moveToThread(queryThread) - queryThread.started.connect(queryWorker.run) - queryThread.start() - @pyqtSlot(object) def addNote(self, words): if words: diff --git a/src/dictionary.py b/src/dictionary.py index 9b20d1b..bdc479f 100644 --- a/src/dictionary.py +++ b/src/dictionary.py @@ -6,7 +6,7 @@ import logging from bs4 import BeautifulSoup from .signals import DictSIG - +from math import ceil from PyQt5.QtCore import QObject, pyqtSlot @@ -167,17 +167,37 @@ def login(self): self.SIG.log.emit(f'网络异常:{e}') return False - def getWordList(self): + def getTotalPage(self): + try: + r = requests.get( + url='https://my.eudic.net/StudyList/WordsDataSource', + timeout=self.timeout, + cookies=self.cookie, + data={'categoryid': -1} + ) + records = r.json()['recordsTotal'] + print(records) + total = ceil(records / 100) + print('record', records) + print('total', total) + self.SIG.totalTasks.emit(total) + self.SIG.log.emit(f"总页数:{total}") + return total + except Exception as e: + self.SIG.exceptionOccurred.emit(e) + self.SIG.log.emit(f'网络异常{e}') + + def getWordPerPage(self, pageNumber): wordList = [] - self.SIG.log.emit('获取单词本') data = { 'columns[2][data]': 'word', - 'start': 0, - 'length': 1000000, + 'start': pageNumber * 100, + 'length': 100, 'categoryid': -1, '_': int(time.time()) * 1000, } try: + self.SIG.log.emit(f'获取单词本第:{pageNumber}页') r = requests.get( url='https://my.eudic.net/StudyList/WordsDataSource', timeout=self.timeout, @@ -186,14 +206,18 @@ def getWordList(self): cookies=self.cookie) wl = r.json() wordList = list(set(word['uuid'] for word in wl['data'])) + print(wordList, pageNumber) except Exception as e: self.SIG.exceptionOccurred.emit(e) - self.SIG.log.emit('网络异常') + self.SIG.log.emit(f'网络异常{e}') finally: + self.SIG.progress.emit() return wordList @pyqtSlot() def run(self): if self.login(): - words = self.getWordList() - self.SIG.wordsReady.emit(words) + words = [self.getWordPerPage(n) for n in range(self.getTotalPage())] + chained_words = list(chain(*words)) + print('一共', len(chained_words)) + self.SIG.wordsReady.emit(chained_words)