From 93f4ee8021b72a8630cba403d8efa92eec6109ed Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 19 May 2011 19:44:46 -0400 Subject: [PATCH] Store: Add calibre coupon code for 10% off purchases. --- src/calibre/gui2/store/oreilly_plugin.py | 35 +++++++++++++++--------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/calibre/gui2/store/oreilly_plugin.py b/src/calibre/gui2/store/oreilly_plugin.py index b7e4e040f4..602a98c68e 100644 --- a/src/calibre/gui2/store/oreilly_plugin.py +++ b/src/calibre/gui2/store/oreilly_plugin.py @@ -6,6 +6,7 @@ __license__ = 'GPL 3' __copyright__ = '2011, John Schember ' __docformat__ = 'restructuredtext en' +import re import urllib from contextlib import closing @@ -25,6 +26,9 @@ class OReillyStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): url = 'http://oreilly.com/ebooks/' + if detail_item: + detail_item = 'https://epoch.oreilly.com/shop/cart.orm?prod=%s.EBOOK&p=CALIBRE' % detail_item + if external or self.config.get('open_external', False): open_url(QUrl(url_slash_cleaner(detail_item if detail_item else url))) else: @@ -45,9 +49,11 @@ class OReillyStore(BasicStoreConfig, StorePlugin): if counter <= 0: break - id = ''.join(data.xpath('.//div[@class="title"]/a/@href')) - if not id: + full_id = ''.join(data.xpath('.//div[@class="title"]/a/@href')) + mo = re.search('\d+', full_id) + if not mo: continue + id = mo.group() cover_url = ''.join(data.xpath('.//div[@class="bigCover"]//img/@src')) @@ -55,6 +61,18 @@ class OReillyStore(BasicStoreConfig, StorePlugin): author = ''.join(data.xpath('.//div[@class="author"]/text()')) author = author.split('By ')[-1].strip() + # Get the detail here because we need to get the ebook id for the detail_item. + with closing(br.open(full_id, timeout=timeout)) as nf: + idoc = html.fromstring(nf.read()) + + price = ''.join(idoc.xpath('(//span[@class="price"])[1]/span//text()')) + formats = ', '.join(idoc.xpath('//div[@class="ebook_formats"]//a/text()')) + + eid = ''.join(idoc.xpath('(//a[@class="product_buy_link" and contains(@href, ".EBOOK")])[1]/@href')).strip() + mo = re.search('\d+', eid) + if mo: + id = mo.group() + counter -= 1 s = SearchResult() @@ -62,17 +80,8 @@ class OReillyStore(BasicStoreConfig, StorePlugin): s.title = title.strip() s.author = author.strip() s.detail_item = id.strip() + s.price = price.strip() s.drm = SearchResult.DRM_UNLOCKED + s.formats = formats.upper() yield s - - def get_details(self, search_result, timeout): - br = browser() - with closing(br.open(search_result.detail_item, timeout=timeout)) as nf: - doc = html.fromstring(nf.read()) - - search_result.price = ''.join(doc.xpath('(//span[@class="price"])[1]/span//text()')).strip() - search_result.formats = ', '.join(doc.xpath('//div[@class="ebook_formats"]//a/text()')).upper() - - return True -