From 93c2231f26df103c6c2e8231ca86b5cecc5bf9ec Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 26 Feb 2011 13:29:52 -0500 Subject: [PATCH] Basic Feedbooks store plugin. --- src/calibre/customize/builtins.py | 3 +- src/calibre/gui2/store/feedbooks_plugin.py | 63 ++++++++++++++++++++++ src/calibre/gui2/store/gutenberg_plugin.py | 2 +- src/calibre/gui2/store/manybooks_plugin.py | 2 +- src/calibre/gui2/store/web_store_dialog.py | 7 ++- 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/calibre/gui2/store/feedbooks_plugin.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 78fc0a4b9b..76aaf3a8ce 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1043,8 +1043,9 @@ plugins += [GoogleBooks] # Store plugins {{{ from calibre.gui2.store.amazon_plugin import AmazonKindleStore from calibre.gui2.store.gutenberg_plugin import GutenbergStore +from calibre.gui2.store.feedbooks_plugin import FeedbooksStore from calibre.gui2.store.manybooks_plugin import ManyBooksStore -plugins += [AmazonKindleStore, GutenbergStore, ManyBooksStore] +plugins += [AmazonKindleStore, GutenbergStore, FeedbooksStore, ManyBooksStore] # }}} diff --git a/src/calibre/gui2/store/feedbooks_plugin.py b/src/calibre/gui2/store/feedbooks_plugin.py new file mode 100644 index 0000000000..e1668a22a3 --- /dev/null +++ b/src/calibre/gui2/store/feedbooks_plugin.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- + +__license__ = 'GPL 3' +__copyright__ = '2011, John Schember ' +__docformat__ = 'restructuredtext en' + +import urllib2 +from contextlib import closing + +from lxml import html + +from calibre import browser +from calibre.customize import StorePlugin + +class FeedbooksStore(StorePlugin): + + name = 'Feedbooks' + description = _('Read anywhere.') + + + def open(self, gui, parent=None, start_item=None): + from calibre.gui2.store.web_store_dialog import WebStoreDialog + d = WebStoreDialog(gui, 'http://m.feedbooks.com/', parent, start_item) + d.setWindowTitle('Feedbooks') + d = d.exec_() + + def search(self, query, max_results=10, timeout=60): + url = 'http://m.feedbooks.com/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="m-list"]//li'): + if counter <= 0: + break + data = html.fromstring(html.tostring(data)) + + id = '' + id_a = data.xpath('//a[@class="buy"]') + if id_a: + id = id_a[0].get('href', None) + id = id.split('/')[-2] + id = '/item/' + id + else: + id_a = data.xpath('//a[@class="download"]') + if id_a: + id = id_a[0].get('href', None) + id = id.split('/')[-1] + id = id.split('.')[0] + id = '/book/' + id + if not id: + continue + + title = ''.join(data.xpath('//h5/a/text()')) + author = ''.join(data.xpath('//h6/a/text()')) + price = ''.join(data.xpath('//a[@class="buy"]/text()')) + if not price: + price = '$0.00' + + counter -= 1 + yield ('', title.strip(), author.strip(), price.replace(' ', '').strip(), id.strip()) diff --git a/src/calibre/gui2/store/gutenberg_plugin.py b/src/calibre/gui2/store/gutenberg_plugin.py index 56edb4588e..38b9af8012 100644 --- a/src/calibre/gui2/store/gutenberg_plugin.py +++ b/src/calibre/gui2/store/gutenberg_plugin.py @@ -21,7 +21,7 @@ class GutenbergStore(StorePlugin): def open(self, gui, parent=None, start_item=None): from calibre.gui2.store.web_store_dialog import WebStoreDialog d = WebStoreDialog(gui, 'http://m.gutenberg.org/', parent, start_item) - d.setWindowTitle('Free eBooks by Project Gutenberg') + d.setWindowTitle('Project Gutenberg') d = d.exec_() def search(self, query, max_results=10, timeout=60): diff --git a/src/calibre/gui2/store/manybooks_plugin.py b/src/calibre/gui2/store/manybooks_plugin.py index 4afceeeb63..1ec6eeb500 100644 --- a/src/calibre/gui2/store/manybooks_plugin.py +++ b/src/calibre/gui2/store/manybooks_plugin.py @@ -21,7 +21,7 @@ class ManyBooksStore(StorePlugin): def open(self, gui, parent=None, start_item=None): from calibre.gui2.store.web_store_dialog import WebStoreDialog d = WebStoreDialog(gui, 'http://manybooks.net/', parent, start_item) - d.setWindowTitle('Ad-free eBooks for your eBook reader') + d.setWindowTitle('ManyBooks') d = d.exec_() def search(self, query, max_results=10, timeout=60): diff --git a/src/calibre/gui2/store/web_store_dialog.py b/src/calibre/gui2/store/web_store_dialog.py index 818a5ec1b5..3910ca6912 100644 --- a/src/calibre/gui2/store/web_store_dialog.py +++ b/src/calibre/gui2/store/web_store_dialog.py @@ -4,6 +4,7 @@ __license__ = 'GPL 3' __copyright__ = '2011, John Schember ' __docformat__ = 'restructuredtext en' +import re import urllib from PyQt4.Qt import QDialog, QUrl @@ -37,11 +38,13 @@ class WebStoreDialog(QDialog, Ui_Dialog): def load_finished(self, ok=True): self.progress.setValue(100) - #if not ok: - # print 'Error' def go_home(self, checked=False, detail_item=None): url = self.base_url if detail_item: url += '/' + urllib.quote(detail_item) + # Reduce redundant /'s because some stores + # (Feedbooks) and server frameworks (cherrypy) + # choke on them. + url = re.sub(r'(?