Option and implementation to open store in external browser.

This commit is contained in:
John Schember 2011-03-03 07:24:06 -05:00
parent 0ad7222044
commit 8e166a19d6
9 changed files with 104 additions and 35 deletions

View File

@ -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'(?<!http:)/{2,}', '/', url)
def human_readable(size):
""" Convert a size in bytes into a human readable form """

View File

@ -10,6 +10,7 @@ from calibre.gui2 import gprefs
from calibre.gui2.store.basic_config_widget_ui import Ui_Form
def save_settings(config_widget):
gprefs[config_widget.store.name + '_open_external'] = config_widget.open_external.isChecked()
tags = unicode(config_widget.tags.text())
gprefs[config_widget.store.name + '_tags'] = tags
@ -26,6 +27,7 @@ class BasicStoreConfigWidget(QWidget, Ui_Form):
def load_setings(self):
settings = self.store.get_settings()
self.open_external.setChecked(settings.get(self.store.name + '_open_external'))
self.tags.setText(settings.get(self.store.name + '_tags', ''))
class BasicStoreConfig(object):
@ -42,6 +44,7 @@ class BasicStoreConfig(object):
def get_settings(self):
settings = {}
settings[self.name + '_open_external'] = gprefs.get(self.name + '_open_external', False)
settings[self.name + '_tags'] = gprefs.get(self.name + '_tags', self.name + ', store, download')
return settings

View File

@ -6,24 +6,31 @@
<rect>
<x>0</x>
<y>0</y>
<width>501</width>
<height>46</height>
<width>460</width>
<height>69</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Added Tags:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QLineEdit" name="tags"/>
</item>
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="open_external">
<property name="text">
<string>Open store in external web browswer</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

View File

@ -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)

View File

@ -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)

View File

@ -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.

View File

@ -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.

View File

@ -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)

View File

@ -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'(?<!http:)/{2,}', '/', url)
url = http_url_slash_cleaner(url)
self.view.load(QUrl(url))