From b04eed00121f98fee1316cc3bff761e4f993c438 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 27 May 2011 13:00:31 +0100 Subject: [PATCH] New store: EBookShoppeUKStore. small correction to FoylesUKStore. fix to threading problem in search causing range errors. Disable waterstones. --- src/calibre/customize/builtins.py | 13 ++- .../gui2/store/ebookshoppe_uk_plugin.py | 97 +++++++++++++++++++ src/calibre/gui2/store/foyles_uk_plugin.py | 9 +- src/calibre/gui2/store/search/models.py | 2 + 4 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/calibre/gui2/store/ebookshoppe_uk_plugin.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 4a970b4661..150ad269bb 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1393,6 +1393,16 @@ class StoreWoblinkStore(StoreBase): headquarters = 'PL' formats = ['EPUB'] +class StoreEBookShoppeUKStore(StoreBase): + name = 'ebookShoppe UK' + author = u'Charles Haley' + description = u'We made this website in an attempt to offer the widest range of UK eBooks possible across and as many formats as we could manage.' + actual_plugin = 'calibre.gui2.store.ebookshoppe_uk_plugin:EBookShoppeUKStore' + + drm_free_only = False + headquarters = 'UK' + formats = ['EPUB', 'PDF'] + plugins += [ StoreArchiveOrgStore, StoreAmazonKindleStore, @@ -1404,6 +1414,7 @@ plugins += [ StoreBeWriteStore, StoreDieselEbooksStore, StoreEbookscomStore, + StoreEBookShoppeUKStore, StoreEPubBuyDEStore, StoreEHarlequinStore, StoreFeedbooksStore, @@ -1421,7 +1432,7 @@ plugins += [ StorePragmaticBookshelfStore, StoreSmashwordsStore, StoreVirtualoStore, - StoreWaterstonesUKStore, + # StoreWaterstonesUKStore, StoreWeightlessBooksStore, StoreWizardsTowerBooksStore, StoreWoblinkStore diff --git a/src/calibre/gui2/store/ebookshoppe_uk_plugin.py b/src/calibre/gui2/store/ebookshoppe_uk_plugin.py new file mode 100644 index 0000000000..de5304da86 --- /dev/null +++ b/src/calibre/gui2/store/ebookshoppe_uk_plugin.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- + +from __future__ import (unicode_literals, division, absolute_import, print_function) + +__license__ = 'GPL 3' +__copyright__ = '2011, John Schember ' +__docformat__ = 'restructuredtext en' + +import urllib2 +from contextlib import closing + +from lxml import html + +from PyQt4.Qt import QUrl + +from calibre import browser, url_slash_cleaner +from calibre.gui2 import open_url +from calibre.gui2.store import StorePlugin +from calibre.gui2.store.basic_config import BasicStoreConfig +from calibre.gui2.store.search_result import SearchResult +from calibre.gui2.store.web_store_dialog import WebStoreDialog + +class EBookShoppeUKStore(BasicStoreConfig, StorePlugin): + + def open(self, parent=None, detail_item=None, external=False): + url_details = 'http://www.awin1.com/cread.php?awinmid=1414&awinaffid=120917&clickref=&p={0}' + url = 'http://www.awin1.com/awclick.php?mid=2666&id=120917' + + if external or self.config.get('open_external', False): + if detail_item: + url = url_details.format(detail_item) + open_url(QUrl(url)) + else: + detail_url = None + if detail_item: + detail_url = url_details.format(detail_item) + d = WebStoreDialog(self.gui, url, parent, detail_url) + d.setWindowTitle(self.name) + d.set_tags(self.config.get('tags', '')) + d.exec_() + + # reduce max_results because the filter will match everything. See the + # setting of 'author' below for more details + + def search(self, query, max_results=5, timeout=60): + url = 'http://www.ebookshoppe.com/search.php?search_query=' + urllib2.quote(query) + br = browser() + + counter = max_results + with closing(br.open(url, timeout=timeout)) as f: + doc = html.fromstring(f.read()) + for data in doc.xpath('//ul[@class="ProductList"]/li'): + if counter <= 0: + break + + id = ''.join(data.xpath('./div[@class="ProductDetails"]/' + 'strong/a/@href')).strip() + if not id: + continue + cover_url = ''.join(data.xpath('./div[@class="ProductImage"]/a/img/@src')) + title = ''.join(data.xpath('./div[@class="ProductDetails"]/strong/a/text()')) + price = ''.join(data.xpath('./div[@class="ProductPriceRating"]/em/text()')) + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url + s.title = title.strip() + # Set the author to the query terms to ensure that author + # queries match something when pruning searches. Of course, this + # means that all books will match. Sigh... + s.author = query + s.price = price + s.drm = SearchResult.DRM_UNLOCKED + s.detail_item = id + s.formats = '' + + # Call this here instead of later. Reason: painting then + # removing matches looks very strange. There are also issues + # with threading. Yes, this makes things take longer, but we + # will do the work anyway. + self.my_get_details(s, timeout) + + yield s + + def my_get_details(self, search_result, timeout): + br = browser() + with closing(br.open(search_result.detail_item, timeout=timeout)) as nf: + idata = html.fromstring(nf.read()) + author = ''.join(idata.xpath('//div[@id="ProductOtherDetails"]/dl/dd[1]/text()')) + if author: + search_result.author = author + formats = idata.xpath('//dl[@class="ProductAddToCart"]/dd/' + 'ul[@class="ProductOptionList"]/li/label/text()') + if formats: + search_result.formats = ', '.join(formats) + search_result.drm = SearchResult.DRM_UNKNOWN + return True \ No newline at end of file diff --git a/src/calibre/gui2/store/foyles_uk_plugin.py b/src/calibre/gui2/store/foyles_uk_plugin.py index 1a997cd671..fd670d2d85 100644 --- a/src/calibre/gui2/store/foyles_uk_plugin.py +++ b/src/calibre/gui2/store/foyles_uk_plugin.py @@ -23,12 +23,13 @@ from calibre.gui2.store.web_store_dialog import WebStoreDialog class FoylesUKStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): - url = 'http://www.awin1.com/cread.php?awinmid=1414&awinaffid=120917&clickref=&p=' + url = 'http://www.awin1.com/awclick.php?mid=1414&id=120917' + detail_url = 'http://www.awin1.com/cread.php?awinmid=1414&awinaffid=120917&clickref=&p=' url_redirect = 'http://www.foyles.co.uk' if external or self.config.get('open_external', False): if detail_item: - url = url + url_redirect + detail_item + url = detail_url + url_redirect + detail_item open_url(QUrl(url_slash_cleaner(url))) else: detail_url = None @@ -54,6 +55,10 @@ class FoylesUKStore(BasicStoreConfig, StorePlugin): if not id: continue + # filter out the audio books + if not data.xpath('boolean(.//div[@class="Relative"]/ul/li[contains(text(), "ePub")])'): + continue + cover_url = ''.join(data.xpath('.//a[@class="Jacket"]/img/@src')) if cover_url: cover_url = 'http://www.foyles.co.uk' + cover_url diff --git a/src/calibre/gui2/store/search/models.py b/src/calibre/gui2/store/search/models.py index d7941480cc..64724be6aa 100644 --- a/src/calibre/gui2/store/search/models.py +++ b/src/calibre/gui2/store/search/models.py @@ -150,6 +150,8 @@ class Matches(QAbstractItemModel): def data(self, index, role): row, col = index.row(), index.column() + if row >= len(self.matches): + return NONE result = self.matches[row] if role == Qt.DisplayRole: if col == 1: