From 3a93e4561ce20dd76bfb1f6af3593b9989818f2b Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sat, 28 May 2011 18:59:30 +0100 Subject: [PATCH] Fix amazon UK grid. Add tooltip to heart on menu. --- src/calibre/gui2/store/amazon_uk_plugin.py | 84 ++++++++++++++++++++++ src/calibre/gui2/store/search/search.py | 1 + 2 files changed, 85 insertions(+) diff --git a/src/calibre/gui2/store/amazon_uk_plugin.py b/src/calibre/gui2/store/amazon_uk_plugin.py index 9544add17c..1448e1548a 100644 --- a/src/calibre/gui2/store/amazon_uk_plugin.py +++ b/src/calibre/gui2/store/amazon_uk_plugin.py @@ -6,11 +6,17 @@ __license__ = 'GPL 3' __copyright__ = '2011, John Schember ' __docformat__ = 'restructuredtext en' +import urllib +from contextlib import closing + +from lxml import html from PyQt4.Qt import QUrl +from calibre import browser from calibre.gui2 import open_url from calibre.gui2.store.amazon_plugin import AmazonKindleStore +from calibre.gui2.store.search_result import SearchResult class AmazonUKKindleStore(AmazonKindleStore): ''' @@ -28,3 +34,81 @@ class AmazonUKKindleStore(AmazonKindleStore): aff_id['asin'] = detail_item store_link = 'http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&location=http://www.amazon.co.uk/dp/%(asin)s&tag=%(tag)s&linkCode=ur2&camp=1634&creative=6738' % aff_id open_url(QUrl(store_link)) + + def search(self, query, max_results=10, timeout=60): + url = self.search_url + urllib.quote_plus(query) + br = browser() + + counter = max_results + with closing(br.open(url, timeout=timeout)) as f: + doc = html.fromstring(f.read()) + + # Amazon has two results pages. + is_shot = doc.xpath('boolean(//div[@id="shotgunMainResults"])') + # Horizontal grid of books. + if is_shot: + data_xpath = '//div[contains(@class, "result")]' + cover_xpath = './/div[@class="productTitle"]//img/@src' + # Vertical list of books. + else: + data_xpath = '//div[contains(@class, "product")]' + cover_xpath = './div[@class="productImage"]/a/img/@src' + + for data in doc.xpath(data_xpath): + if counter <= 0: + break + + # We must have an asin otherwise we can't easily reference the + # book later. + asin = ''.join(data.xpath('./@name')) + if not asin: + continue + cover_url = ''.join(data.xpath(cover_xpath)) + + title = ''.join(data.xpath('.//div[@class="productTitle"]/a/text()')) + price = ''.join(data.xpath('.//div[@class="newPrice"]/span/text()')) + + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url.strip() + s.title = title.strip() + s.price = price.strip() + s.detail_item = asin.strip() + s.formats = 'Kindle' + + if is_shot: + # Amazon UK does not include the author on the grid layout + s.author = '' + self.get_details(s, timeout) + else: + author = ''.join(data.xpath('.//div[@class="productTitle"]/span[@class="ptBrand"]/text()')) + s.author = author.split(' by ')[-1].strip() + + yield s + + def get_details(self, search_result, timeout): + # We might already have been called. + if search_result.drm: + return + + url = self.details_url + + br = browser() + with closing(br.open(url + search_result.detail_item, timeout=timeout)) as nf: + idata = html.fromstring(nf.read()) + if not search_result.author: + search_result.author = ''.join(idata.xpath('//div[@class="buying" and contains(., "Author")]/a/text()')) + if idata.xpath('boolean(//div[@class="content"]//li/b[contains(text(), "' + + self.drm_search_text + '")])'): + if idata.xpath('boolean(//div[@class="content"]//li[contains(., "' + + self.drm_free_text + '") and contains(b, "' + + self.drm_search_text + '")])'): + search_result.drm = SearchResult.DRM_UNLOCKED + else: + search_result.drm = SearchResult.DRM_UNKNOWN + else: + search_result.drm = SearchResult.DRM_LOCKED + return True + + diff --git a/src/calibre/gui2/store/search/search.py b/src/calibre/gui2/store/search/search.py index 7ce6c93c68..9e60223a3d 100644 --- a/src/calibre/gui2/store/search/search.py +++ b/src/calibre/gui2/store/search/search.py @@ -103,6 +103,7 @@ class SearchDialog(QDialog, Ui_Dialog): store_list_layout.addWidget(cbox, i, 0, 1, 1) if self.gui.istores[x].base_plugin.affiliate: iw = QLabel(self) + iw.setToolTip(_('Buying from this store supports a calibre developer')) iw.setPixmap(icon.pixmap(16, 16)) store_list_layout.addWidget(iw, i, 1, 1, 1) self.store_checks[x] = cbox