From feb3b98a091fa9955dfb0483655fb988fb9525b4 Mon Sep 17 00:00:00 2001 From: John Schember Date: Wed, 18 May 2011 21:04:00 -0400 Subject: [PATCH] Store: Add OReilly plugin. --- src/calibre/customize/builtins.py | 8 ++- src/calibre/gui2/store/oreilly_plugin.py | 78 ++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/calibre/gui2/store/oreilly_plugin.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 8e16d4c76c..b2d3a967df 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1199,6 +1199,11 @@ class StoreOpenLibraryStore(StoreBase): description = _('One web page for every book.') actual_plugin = 'calibre.gui2.store.open_library_plugin:OpenLibraryStore' +class StoreOReillyStore(StoreBase): + name = 'OReilly' + description = _('DRM-Free tech ebooks.') + actual_plugin = 'calibre.gui2.store.oreilly_plugin:OReillyStore' + class StoreSmashwordsStore(StoreBase): name = 'Smashwords' description = _('Your ebook. Your way.') @@ -1226,7 +1231,8 @@ plugins += [StoreArchiveOrgStore, StoreAmazonKindleStore, StoreAmazonDEKindleSto StoreEHarlequinStore, StoreFeedbooksStore, StoreFoylesUKStore, StoreGoogleBooksStore, StoreGutenbergStore, StoreKoboStore, StoreManyBooksStore, - StoreMobileReadStore, StoreNextoStore, StoreOpenLibraryStore, StoreSmashwordsStore, + StoreMobileReadStore, StoreNextoStore, StoreOpenLibraryStore, + StoreOReillyStore, StoreSmashwordsStore, StoreWaterstonesUKStore, StoreWeightlessBooksStore, StoreWizardsTowerBooksStore] # }}} diff --git a/src/calibre/gui2/store/oreilly_plugin.py b/src/calibre/gui2/store/oreilly_plugin.py new file mode 100644 index 0000000000..b7e4e040f4 --- /dev/null +++ b/src/calibre/gui2/store/oreilly_plugin.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- + +from __future__ import (unicode_literals, division, absolute_import, print_function) + +__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, 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 OReillyStore(BasicStoreConfig, StorePlugin): + + def open(self, parent=None, detail_item=None, external=False): + url = 'http://oreilly.com/ebooks/' + + if external or self.config.get('open_external', False): + open_url(QUrl(url_slash_cleaner(detail_item if detail_item else url))) + else: + d = WebStoreDialog(self.gui, url, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(self.config.get('tags', '')) + d.exec_() + + def search(self, query, max_results=10, timeout=60): + url = 'http://search.oreilly.com/?t1=Books&t2=Format&t3=Ebook&q=' + urllib.quote_plus(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('//div[@id="results"]/div[@class="result"]'): + if counter <= 0: + break + + id = ''.join(data.xpath('.//div[@class="title"]/a/@href')) + if not id: + continue + + cover_url = ''.join(data.xpath('.//div[@class="bigCover"]//img/@src')) + + title = ''.join(data.xpath('.//div[@class="title"]/a/text()')) + author = ''.join(data.xpath('.//div[@class="author"]/text()')) + author = author.split('By ')[-1].strip() + + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url.strip() + s.title = title.strip() + s.author = author.strip() + s.detail_item = id.strip() + s.drm = SearchResult.DRM_UNLOCKED + + 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 +