Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
try to fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
megachweng committed Dec 6, 2018
1 parent 02c0b03 commit ef6b585
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
46 changes: 23 additions & 23 deletions src/addonWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import time
import json

__VERSION__ = 'v5.0.0'
__VERSION__ = 'v5.0.1'
MODELNAME = 'Dict2Anki_NEW'
DICTIONARYLIST = ['Youdao', 'Eudict']

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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:
Expand Down
40 changes: 32 additions & 8 deletions src/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from bs4 import BeautifulSoup
from .signals import DictSIG

from math import ceil
from PyQt5.QtCore import QObject, pyqtSlot


Expand Down Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit ef6b585

Please sign in to comment.