From 8e166a19d6c9cb4ef9e6ce51fc10e90a862fbd51 Mon Sep 17 00:00:00 2001 From: John Schember Date: Thu, 3 Mar 2011 07:24:06 -0500 Subject: [PATCH] Option and implementation to open store in external browser. --- src/calibre/__init__.py | 6 ++++- src/calibre/gui2/store/basic_config.py | 3 +++ src/calibre/gui2/store/basic_config_widget.ui | 15 ++++++++--- .../gui2/store/diesel_ebooks_plugin.py | 25 +++++++++++++------ src/calibre/gui2/store/feedbooks_plugin.py | 21 ++++++++++++---- src/calibre/gui2/store/gutenberg_plugin.py | 21 ++++++++++++---- src/calibre/gui2/store/manybooks_plugin.py | 20 +++++++++++---- src/calibre/gui2/store/smashwords_plugin.py | 25 +++++++++++++------ src/calibre/gui2/store/web_store_dialog.py | 3 ++- 9 files changed, 104 insertions(+), 35 deletions(-) diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 61b8bd36f8..f324a7e46c 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -473,7 +473,11 @@ def as_unicode(obj, enc=preferred_encoding): obj = repr(obj) return force_unicode(obj, enc=enc) - +def http_url_slash_cleaner(url): + ''' + Removes redundant /'s from url's. + ''' + return re.sub(r'(? 0 0 - 501 - 46 + 460 + 69 Form - + Added Tags: - + + + + + Open store in external web browswer + + + diff --git a/src/calibre/gui2/store/diesel_ebooks_plugin.py b/src/calibre/gui2/store/diesel_ebooks_plugin.py index 87d1f8afab..a213ffd5c4 100644 --- a/src/calibre/gui2/store/diesel_ebooks_plugin.py +++ b/src/calibre/gui2/store/diesel_ebooks_plugin.py @@ -10,7 +10,10 @@ from contextlib import closing from lxml import html -from calibre import browser +from PyQt4.Qt import QUrl + +from calibre import browser, http_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 @@ -20,14 +23,22 @@ class DieselEbooksStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): settings = self.get_settings() - aff_id = '2049' + url = 'http://www.diesel-ebooks.com/' + + aff_id = '?aid=2049' # Use Kovid's affiliate id 30% of the time. if random.randint(1, 10) in (1, 2, 3): - aff_id = '2053' - d = WebStoreDialog(self.gui, 'http://www.diesel-ebooks.com/?aid=%s' % aff_id, parent, detail_item) - d.setWindowTitle(self.name) - d.set_tags(settings.get(self.name + '_tags', '')) - d = d.exec_() + aff_id = '?aid=2053' + + if external or settings.get(self.name + '_open_external', False): + if detail_item: + url = url + detail_item + open_url(QUrl(http_url_slash_cleaner(url + aff_id))) + else: + d = WebStoreDialog(self.gui, url + aff_id, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(settings.get(self.name + '_tags', '')) + d = d.exec_() def search(self, query, max_results=10, timeout=60): url = 'http://www.diesel-ebooks.com/index.php?page=seek&id[m]=&id[c]=scope%253Dinventory&id[q]=' + urllib2.quote(query) diff --git a/src/calibre/gui2/store/feedbooks_plugin.py b/src/calibre/gui2/store/feedbooks_plugin.py index e07920fc4d..ea27e0ca17 100644 --- a/src/calibre/gui2/store/feedbooks_plugin.py +++ b/src/calibre/gui2/store/feedbooks_plugin.py @@ -9,7 +9,10 @@ from contextlib import closing from lxml import html -from calibre import browser +from PyQt4.Qt import QUrl + +from calibre import browser, http_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 @@ -19,10 +22,18 @@ class FeedbooksStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): settings = self.get_settings() - d = WebStoreDialog(self.gui, 'http://m.feedbooks.com/', parent, detail_item) - d.setWindowTitle(self.name) - d.set_tags(settings.get(self.name + '_tags', '')) - d = d.exec_() + url = 'http://m.feedbooks.com/' + ext_url = 'http://feedbooks.com/' + + if external or settings.get(self.name + '_open_external', False): + if detail_item: + ext_url = ext_url + detail_item + open_url(QUrl(http_url_slash_cleaner(ext_url))) + else: + d = WebStoreDialog(self.gui, url, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(settings.get(self.name + '_tags', '')) + d = d.exec_() def search(self, query, max_results=10, timeout=60): url = 'http://m.feedbooks.com/search?query=' + urllib2.quote(query) diff --git a/src/calibre/gui2/store/gutenberg_plugin.py b/src/calibre/gui2/store/gutenberg_plugin.py index c263d544ea..69df15c238 100644 --- a/src/calibre/gui2/store/gutenberg_plugin.py +++ b/src/calibre/gui2/store/gutenberg_plugin.py @@ -9,7 +9,10 @@ from contextlib import closing from lxml import html -from calibre import browser +from PyQt4.Qt import QUrl + +from calibre import browser, http_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 @@ -19,10 +22,18 @@ class GutenbergStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): settings = self.get_settings() - d = WebStoreDialog(self.gui, 'http://m.gutenberg.org/', parent, detail_item) - d.setWindowTitle(self.name) - d.set_tags(settings.get(self.name + '_tags', '')) - d = d.exec_() + url = 'http://m.gutenberg.org/' + ext_url = 'http://gutenberg.org/' + + if external or settings.get(self.name + '_open_external', False): + if detail_item: + ext_url = ext_url + detail_item + open_url(QUrl(http_url_slash_cleaner(ext_url))) + else: + d = WebStoreDialog(self.gui, url, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(settings.get(self.name + '_tags', '')) + d = d.exec_() def search(self, query, max_results=10, timeout=60): # Gutenberg's website does not allow searching both author and title. diff --git a/src/calibre/gui2/store/manybooks_plugin.py b/src/calibre/gui2/store/manybooks_plugin.py index 4ca7018d00..94c9a38863 100644 --- a/src/calibre/gui2/store/manybooks_plugin.py +++ b/src/calibre/gui2/store/manybooks_plugin.py @@ -10,7 +10,10 @@ from contextlib import closing from lxml import html -from calibre import browser +from PyQt4.Qt import QUrl + +from calibre import browser, http_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 @@ -20,10 +23,17 @@ class ManyBooksStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): settings = self.get_settings() - d = WebStoreDialog(self.gui, 'http://manybooks.net/', parent, detail_item) - d.setWindowTitle(self.name) - d.set_tags(settings.get(self.name + '_tags', '')) - d = d.exec_() + url = 'http://manybooks.net/' + + if external or settings.get(self.name + '_open_external', False): + if detail_item: + url = url + detail_item + open_url(QUrl(http_url_slash_cleaner(url))) + else: + d = WebStoreDialog(self.gui, url, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(settings.get(self.name + '_tags', '')) + d = d.exec_() def search(self, query, max_results=10, timeout=60): # ManyBooks website separates results for title and author. diff --git a/src/calibre/gui2/store/smashwords_plugin.py b/src/calibre/gui2/store/smashwords_plugin.py index 3d27d39c04..5307cb0072 100644 --- a/src/calibre/gui2/store/smashwords_plugin.py +++ b/src/calibre/gui2/store/smashwords_plugin.py @@ -11,7 +11,10 @@ from contextlib import closing from lxml import html -from calibre import browser +from PyQt4.Qt import QUrl + +from calibre import browser, http_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 @@ -21,14 +24,22 @@ class SmashwordsStore(BasicStoreConfig, StorePlugin): def open(self, parent=None, detail_item=None, external=False): settings = self.get_settings() - aff_id = 'usernone' + url = 'http://www.smashwords.com/' + + aff_id = '?ref=usernone' # Use Kovid's affiliate id 30% of the time. if random.randint(1, 10) in (1, 2, 3): - aff_id = 'kovidgoyal' - d = WebStoreDialog(self.gui, 'http://www.smashwords.com/?ref=%s' % aff_id, parent, detail_item) - d.setWindowTitle(self.name) - d.set_tags(settings.get(self.name + '_tags', '')) - d = d.exec_() + aff_id = '?ref=kovidgoyal' + + if external or settings.get(self.name + '_open_external', False): + if detail_item: + url = url + detail_item + open_url(QUrl(http_url_slash_cleaner(url + aff_id))) + else: + d = WebStoreDialog(self.gui, url + aff_id, parent, detail_item) + d.setWindowTitle(self.name) + d.set_tags(settings.get(self.name + '_tags', '')) + d = d.exec_() def search(self, query, max_results=10, timeout=60): url = 'http://www.smashwords.com/books/search?query=' + urllib2.quote(query) diff --git a/src/calibre/gui2/store/web_store_dialog.py b/src/calibre/gui2/store/web_store_dialog.py index 0c032e26e0..a651cf3085 100644 --- a/src/calibre/gui2/store/web_store_dialog.py +++ b/src/calibre/gui2/store/web_store_dialog.py @@ -9,6 +9,7 @@ import urllib from PyQt4.Qt import QDialog, QUrl +from calibre import http_url_slash_cleaner from calibre.gui2.store.web_store_dialog_ui import Ui_Dialog class WebStoreDialog(QDialog, Ui_Dialog): @@ -51,5 +52,5 @@ class WebStoreDialog(QDialog, Ui_Dialog): # Reduce redundant /'s because some stores # (Feedbooks) and server frameworks (cherrypy) # choke on them. - url = re.sub(r'(?