From bd781f047ce716bdf2d045c310f2d4cf9bb185c7 Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 19 May 2011 18:43:14 -0400 Subject: [PATCH] Store: Add Pragmatic Bookshelf store. --- src/calibre/customize/builtins.py | 46 +++++++--- .../gui2/store/pragmatic_bookshelf_plugin.py | 84 +++++++++++++++++++ 2 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 src/calibre/gui2/store/pragmatic_bookshelf_plugin.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 5c93711e7d..ac26544207 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1211,6 +1211,11 @@ class StoreOReillyStore(StoreBase): description = _('DRM-Free tech ebooks.') actual_plugin = 'calibre.gui2.store.oreilly_plugin:OReillyStore' +class StorePragmaticBookshelfStore(StoreBase): + name = 'Pragmatic Bookshelf' + description = _('The Pragmatic Bookshelf') + actual_plugin = 'calibre.gui2.store.pragmatic_bookshelf_plugin:PragmaticBookshelfStore' + class StoreSmashwordsStore(StoreBase): name = 'Smashwords' description = _('Your ebook. Your way.') @@ -1231,16 +1236,35 @@ class StoreWizardsTowerBooksStore(StoreBase): description = 'Wizard\'s Tower Press.' actual_plugin = 'calibre.gui2.store.wizards_tower_books_plugin:WizardsTowerBooksStore' -plugins += [StoreArchiveOrgStore, StoreAmazonKindleStore, StoreAmazonDEKindleStore, - StoreAmazonUKKindleStore, StoreBaenWebScriptionStore, StoreBNStore, - StoreBeamEBooksDEStore, StoreBeWriteStore, - StoreDieselEbooksStore, StoreEbookscomStore, StoreEPubBuyDEStore, - StoreEHarlequinStore, StoreFeedbooksStore, - StoreFoylesUKStore, StoreGandalfStore, - StoreGoogleBooksStore, StoreGutenbergStore, - StoreKoboStore, StoreManyBooksStore, - StoreMobileReadStore, StoreNextoStore, StoreOpenLibraryStore, - StoreOReillyStore, StoreSmashwordsStore, - StoreWaterstonesUKStore, StoreWeightlessBooksStore, StoreWizardsTowerBooksStore] +plugins += [ + StoreArchiveOrgStore, + StoreAmazonKindleStore, + StoreAmazonDEKindleStore, + StoreAmazonUKKindleStore, + StoreBaenWebScriptionStore, + StoreBNStore, + StoreBeamEBooksDEStore, + StoreBeWriteStore, + StoreDieselEbooksStore, + StoreEbookscomStore, + StoreEPubBuyDEStore, + StoreEHarlequinStore, + StoreFeedbooksStore, + StoreFoylesUKStore, + StoreGandalfStore, + StoreGoogleBooksStore, + StoreGutenbergStore, + StoreKoboStore, + StoreManyBooksStore, + StoreMobileReadStore, + StoreNextoStore, + StoreOpenLibraryStore, + StoreOReillyStore, + StorePragmaticBookshelfStore, + StoreSmashwordsStore, + StoreWaterstonesUKStore, + StoreWeightlessBooksStore, + StoreWizardsTowerBooksStore +] # }}} diff --git a/src/calibre/gui2/store/pragmatic_bookshelf_plugin.py b/src/calibre/gui2/store/pragmatic_bookshelf_plugin.py new file mode 100644 index 0000000000..9521fbdb91 --- /dev/null +++ b/src/calibre/gui2/store/pragmatic_bookshelf_plugin.py @@ -0,0 +1,84 @@ +# -*- 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 PragmaticBookshelfStore(BasicStoreConfig, StorePlugin): + + def open(self, parent=None, detail_item=None, external=False): + url = 'http://weightlessbooks.com/' + + 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): + ''' + OPDS based search. + + We really should get the catelog from http://pragprog.com/catalog.opds + and look for the application/opensearchdescription+xml entry. + Then get the opensearch description to get the search url and + format. However, we are going to be lazy and hard code it. + ''' + url = 'http://pragprog.com/catalog/search?q=' + urllib.quote_plus(query) + + br = browser() + + counter = max_results + with closing(br.open(url, timeout=timeout)) as f: + # Use html instead of etree as html allows us + # to ignore the namespace easily. + doc = html.fromstring(f.read()) + for data in doc.xpath('//entry'): + if counter <= 0: + break + + id = ''.join(data.xpath('.//link[@rel="http://opds-spec.org/acquisition/buy"]/@href')) + if not id: + continue + + price = ''.join(data.xpath('.//price/@currencycode')).strip() + price += ' ' + price += ''.join(data.xpath('.//price/text()')).strip() + if not price.strip(): + continue + + cover_url = ''.join(data.xpath('.//link[@rel="http://opds-spec.org/cover"]/@href')) + + title = ''.join(data.xpath('.//title/text()')) + author = ''.join(data.xpath('.//author//text()')) + + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url + s.title = title.strip() + s.author = author.strip() + s.price = price.strip() + s.detail_item = id.strip() + s.drm = SearchResult.DRM_UNLOCKED + s.formats = 'EPUB, PDF, MOBI' + + yield s