-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_worker.py
116 lines (91 loc) · 3.64 KB
/
check_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
__copyright__ = '2018, BookFusion <[email protected]>'
__license__ = 'GPL v3'
from PyQt5.Qt import QObject, pyqtSignal, QNetworkAccessManager, QNetworkReply
from os.path import getsize
import json
from calibre_plugins.bookfusion.config import prefs
from calibre_plugins.bookfusion import api
from calibre_plugins.bookfusion.book_format import BookFormat
class CheckWorker(QObject):
finished = pyqtSignal()
aborted = pyqtSignal(str)
readyToRunCheck = pyqtSignal()
progress = pyqtSignal(int)
limitsAvailable = pyqtSignal(dict)
resultsAvailable = pyqtSignal(int, list)
def __init__(self, db, logger, book_ids):
QObject.__init__(self)
self.db = db
self.logger = logger
self.book_ids = book_ids
self.api_key = prefs['api_key']
self.reply = None
self.canceled = False
def start(self):
self.network = QNetworkAccessManager()
self.network.authenticationRequired.connect(self.auth)
self.readyToRunCheck.connect(self.run_check)
self.pending_book_ids = self.book_ids
self.count = 0
self.books_count = 0
self.valid_ids = []
self.fetch_limits()
def cancel(self):
self.canceled = True
if self.reply:
self.reply.abort()
self.finished.emit()
def auth(self, reply, authenticator):
if not authenticator.user():
authenticator.setUser(self.api_key)
authenticator.setPassword('')
def fetch_limits(self):
self.req = api.build_request('/limits')
self.reply = self.network.get(self.req)
self.reply.finished.connect(self.finish_fetch_limits)
def finish_fetch_limits(self):
if self.canceled:
return
abort = False
error = self.reply.error()
if error == QNetworkReply.NetworkError.AuthenticationRequiredError:
abort = True
self.aborted.emit('Invalid API key.')
self.logger.info('Fetch limits: AuthenticationRequiredError')
elif error == QNetworkReply.NetworkError.NoError:
resp = self.reply.readAll()
self.logger.info('Fetch limits response: {}'.format(resp))
self.limits = json.loads(resp.data())
self.limitsAvailable.emit(self.limits)
elif error == QNetworkReply.NetworkError.OperationCanceledError:
abort = True
self.logger.info('Fetch limits: OperationCanceledError')
else:
abort = True
self.aborted.emit('Error {}.'.format(error))
self.logger.info('Fetch limits error: {}'.format(error))
self.reply.deleteLater()
self.reply = None
if abort:
self.finished.emit()
else:
self.readyToRunCheck.emit()
def run_check(self):
for book_id in self.pending_book_ids:
if self.canceled:
return
self.progress.emit(self.count)
self.count += 1
self.logger.info('File: book_id={}'.format(book_id))
book_format = BookFormat(self.db, book_id)
if book_format.file_path:
self.books_count += 1
if getsize(book_format.file_path) <= self.limits['filesize']:
self.valid_ids.append(book_id)
self.logger.info('File ok: book_id={}'.format(book_id))
else:
self.logger.info('Filesize exceeded: book_id={}'.format(book_id))
else:
self.logger.info('Unsupported format: book_id={}'.format(book_id))
self.resultsAvailable.emit(self.books_count, self.valid_ids)
self.finished.emit()