mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Move abort and add timeout.
This commit is contained in:
parent
83ed9911c4
commit
6d5d638fe3
@ -596,7 +596,7 @@ class StorePlugin(Plugin): # {{{
|
|||||||
'''
|
'''
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def search(self, query, max_results=10):
|
def search(self, query, max_results=10, timeout=60):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
@ -23,12 +23,12 @@ class AmazonKindleStore(StorePlugin):
|
|||||||
d = AmazonKindleDialog(parent, start_item)
|
d = AmazonKindleDialog(parent, start_item)
|
||||||
d = d.exec_()
|
d = d.exec_()
|
||||||
|
|
||||||
def search(self, query, max_results=10):
|
def search(self, query, max_results=10, timeout=60):
|
||||||
url = 'http://www.amazon.com/s/url=search-alias%3Ddigital-text&field-keywords=' + urllib2.quote(query)
|
url = 'http://www.amazon.com/s/url=search-alias%3Ddigital-text&field-keywords=' + urllib2.quote(query)
|
||||||
br = browser()
|
br = browser()
|
||||||
|
|
||||||
counter = max_results
|
counter = max_results
|
||||||
with closing(br.open(url)) as f:
|
with closing(br.open(url, timeout=timeout)) as f:
|
||||||
doc = html.fromstring(f.read())
|
doc = html.fromstring(f.read())
|
||||||
for data in doc.xpath('//div[@class="productData"]'):
|
for data in doc.xpath('//div[@class="productData"]'):
|
||||||
if counter <= 0:
|
if counter <= 0:
|
||||||
|
@ -14,6 +14,9 @@ from calibre.gui2.store.search_ui import Ui_Dialog
|
|||||||
|
|
||||||
class SearchDialog(QDialog, Ui_Dialog):
|
class SearchDialog(QDialog, Ui_Dialog):
|
||||||
|
|
||||||
|
HANG_TIME = 75000 # milliseconds seconds
|
||||||
|
TIMEOUT = 75 # seconds
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
QDialog.__init__(self, *args)
|
QDialog.__init__(self, *args)
|
||||||
self.setupUi(self)
|
self.setupUi(self)
|
||||||
@ -23,6 +26,7 @@ class SearchDialog(QDialog, Ui_Dialog):
|
|||||||
self.results = Queue()
|
self.results = Queue()
|
||||||
self.abort = Event()
|
self.abort = Event()
|
||||||
self.checker = QTimer()
|
self.checker = QTimer()
|
||||||
|
self.hang_check = 0
|
||||||
|
|
||||||
for x in store_plugins():
|
for x in store_plugins():
|
||||||
self.store_plugins[x.name] = x
|
self.store_plugins[x.name] = x
|
||||||
@ -38,19 +42,28 @@ class SearchDialog(QDialog, Ui_Dialog):
|
|||||||
self.results = Queue()
|
self.results = Queue()
|
||||||
self.abort = Event()
|
self.abort = Event()
|
||||||
for n in self.store_plugins:
|
for n in self.store_plugins:
|
||||||
t = SearchThread(unicode(self.search_edit.text()), (n, self.store_plugins[n]), self.results, self.abort)
|
t = SearchThread(unicode(self.search_edit.text()), (n, self.store_plugins[n]), self.results, self.abort, self.TIMEOUT)
|
||||||
self.running_threads.append(t)
|
self.running_threads.append(t)
|
||||||
t.start()
|
t.start()
|
||||||
if self.running_threads:
|
if self.running_threads:
|
||||||
|
self.hang_check = 0
|
||||||
self.checker.start(100)
|
self.checker.start(100)
|
||||||
|
|
||||||
def get_results(self):
|
def get_results(self):
|
||||||
running = False
|
# We only want the search plugins to run
|
||||||
for t in self.running_threads:
|
# a maximum set amount of time before giving up.
|
||||||
if t.is_alive():
|
self.hang_check += 1
|
||||||
running = True
|
if self.hang_check >= self.HANG_TIME:
|
||||||
if not running:
|
self.abort.set()
|
||||||
self.checker.stop()
|
self.checker.stop()
|
||||||
|
else:
|
||||||
|
# Stop the checker if not threads are running.
|
||||||
|
running = False
|
||||||
|
for t in self.running_threads:
|
||||||
|
if t.is_alive():
|
||||||
|
running = True
|
||||||
|
if not running:
|
||||||
|
self.checker.stop()
|
||||||
|
|
||||||
while not self.results.empty():
|
while not self.results.empty():
|
||||||
print self.results.get_nowait()
|
print self.results.get_nowait()
|
||||||
@ -58,7 +71,7 @@ class SearchDialog(QDialog, Ui_Dialog):
|
|||||||
|
|
||||||
class SearchThread(Thread):
|
class SearchThread(Thread):
|
||||||
|
|
||||||
def __init__(self, query, store, results, abort):
|
def __init__(self, query, store, results, abort, timeout):
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.query = query
|
self.query = query
|
||||||
@ -66,9 +79,19 @@ class SearchThread(Thread):
|
|||||||
self.store_plugin = store[1]
|
self.store_plugin = store[1]
|
||||||
self.results = results
|
self.results = results
|
||||||
self.abort = abort
|
self.abort = abort
|
||||||
|
self.timeout = timeout
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.abort.is_set():
|
for res in self.store_plugin.search(self.query, timeout=self.timeout):
|
||||||
return
|
if self.abort.is_set():
|
||||||
for res in self.store_plugin.search(self.query):
|
return
|
||||||
self.results.put((self.store_name, res))
|
self.results.put((self.store_name, res))
|
||||||
|
|
||||||
|
class SearchResult(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.title = ''
|
||||||
|
self.author = ''
|
||||||
|
self.price = ''
|
||||||
|
self.item_data = ''
|
||||||
|
self.plugin_name = ''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user