Add options to disable/prioritize taking URLs from selected stories & clipboard.

This commit is contained in:
Jim Miller 2011-12-25 15:27:25 -06:00
parent 6e52a5b096
commit 74ac6fb348
2 changed files with 114 additions and 49 deletions

View file

@ -21,10 +21,17 @@ from calibre_plugins.fanfictiondownloader_plugin.dialogs import (OVERWRITE, ADDN
# so as to ensure you dont accidentally clobber a calibre config file
prefs = JSONConfig('plugins/fanfictiondownloader_plugin')
# urlsfrompriority values
CLIP = 'Clipboard'
SELECTED = 'Selected Stories'
# Set defaults
prefs.defaults['personal.ini'] = get_resources('example.ini')
prefs.defaults['updatemeta'] = True
prefs.defaults['onlyoverwriteifnewer'] = False
prefs.defaults['urlsfromclip'] = True
prefs.defaults['urlsfromselected'] = True
prefs.defaults['urlsfrompriority'] = SELECTED
prefs.defaults['fileform'] = 'epub'
prefs.defaults['collision'] = OVERWRITE
@ -75,6 +82,29 @@ class ConfigWidget(QWidget):
self.onlyoverwriteifnewer.setChecked(prefs['onlyoverwriteifnewer'])
self.l.addWidget(self.onlyoverwriteifnewer)
self.urlsfromclip = QCheckBox('Take URLs from Clipboard?',self)
self.urlsfromclip.setToolTip('Prefill URLs from valid URLs in Clipboard when starting plugin?')
self.urlsfromclip.setChecked(prefs['urlsfromclip'])
self.l.addWidget(self.urlsfromclip)
self.urlsfromselected = QCheckBox('Take URLs from Selected Stories?',self)
self.urlsfromselected.setToolTip('Prefill URLs from valid URLs in selected stories when starting plugin?')
self.urlsfromselected.setChecked(prefs['urlsfromselected'])
self.l.addWidget(self.urlsfromselected)
horz = QHBoxLayout()
label = QLabel('Take URLs from which first:')
label.setToolTip("If both clipbaord and selected enabled and populated, which is used?")
horz.addWidget(label)
self.urlsfrompriority = QComboBox(self)
self.urlsfrompriority.addItem(SELECTED)
self.urlsfrompriority.addItem(CLIP)
self.urlsfrompriority.setCurrentIndex(self.urlsfrompriority.findText(prefs['urlsfrompriority']))
self.urlsfrompriority.setToolTip('If both clipbaord and selected enabled and populated, which is used?')
label.setBuddy(self.urlsfrompriority)
horz.addWidget(self.urlsfrompriority)
self.l.addLayout(horz)
self.label = QLabel('personal.ini:')
self.l.addWidget(self.label)
@ -87,6 +117,9 @@ class ConfigWidget(QWidget):
prefs['fileform'] = unicode(self.fileform.currentText())
prefs['collision'] = unicode(self.collision.currentText())
prefs['updatemeta'] = self.updatemeta.isChecked()
prefs['urlsfrompriority'] = unicode(self.urlsfrompriority.currentText())
prefs['urlsfromclip'] = self.urlsfromclip.isChecked()
prefs['urlsfromselected'] = self.urlsfromselected.isChecked()
prefs['onlyoverwriteifnewer'] = self.onlyoverwriteifnewer.isChecked()
ini = unicode(self.ini.toPlainText())

View file

@ -25,7 +25,7 @@ from calibre.gui2.threaded_jobs import ThreadedJob
from calibre_plugins.fanfictiondownloader_plugin.fanficdownloader import adapters, writers, exceptions
from calibre_plugins.fanfictiondownloader_plugin.epubmerge import doMerge
from calibre_plugins.fanfictiondownloader_plugin.config import prefs
from calibre_plugins.fanfictiondownloader_plugin.config import (prefs, CLIP, SELECTED)
from calibre_plugins.fanfictiondownloader_plugin.dialogs import (
DownloadDialog, MetadataProgressDialog, UserPassDialog,
OVERWRITE, UPDATE, ADDNEW, SKIP, CALIBREONLY, NotGoingToDownload )
@ -91,55 +91,19 @@ class FanFictionDownLoaderPlugin(InterfaceAction):
# things.
self.db = self.gui.current_db
## if there's rows selected, try to find a source URL from
## either identifier in the metadata, or from the epub
## metadata.
url_list = []
rows = self.gui.library_view.selectionModel().selectedRows()
if rows:
book_ids = self.gui.library_view.get_selected_ids()
print("book_ids: %s"%book_ids)
for book_id in book_ids:
identifiers = self.db.get_identifiers(book_id,index_is_id=True)
if 'url' in identifiers:
# identifiers have :->| in url.
#print("url from book:"+identifiers['url'].replace('|',':'))
url_list.append(identifiers['url'].replace('|',':'))
else:
## only epub has that in it.
if self.db.has_format(book_id,'EPUB',index_is_id=True):
existingepub = self.db.format(book_id,'EPUB',index_is_id=True, as_file=True)
mi = get_metadata(existingepub,'EPUB')
#print("mi:%s"%mi)
identifiers = mi.get_identifiers()
if 'url' in identifiers:
#print("url from epub:"+identifiers['url'].replace('|',':'))
url_list.append(identifiers['url'].replace('|',':'))
else:
# no rows selected, check for valid URLs in the clipboard.
cliptext = unicode(QApplication.instance().clipboard().text())
url_list.extend(cliptext.split())
# pre-pop urls from selected stories or clipboard. but with
# configurable priority and on/off option on each
if prefs['urlsfrompriority'] == SELECTED:
from_first_func = self.get_urls_select
from_second_func = self.get_urls_clip
elif prefs['urlsfrompriority'] == CLIP:
from_first_func = self.get_urls_clip
from_second_func = self.get_urls_select
url_list_text = from_first_func()
if not url_list_text:
url_list_text = from_second_func()
url_list_text = ""
# Check and make sure the URLs are valid ffdl URLs.
if url_list:
dummyconfig = ConfigParser.SafeConfigParser()
alreadyin=[]
for url in url_list:
if url in alreadyin:
continue
alreadyin.append(url)
# pulling up an adapter is pretty low over-head. If
# it fails, it's a bad url.
try:
adapters.getAdapter(dummyconfig,url)
except:
pass
else:
if url_list_text:
url_list_text += "\n"
url_list_text += url
#'''http://test1.com?sid=6
#''')
# http://test1.com?sid=6701
@ -166,6 +130,70 @@ class FanFictionDownLoaderPlugin(InterfaceAction):
)
d.show()
## if there's rows selected, try to find a source URL from
## either identifier in the metadata, or from the epub
## metadata.
def get_urls_select(self):
url_list = []
rows = self.gui.library_view.selectionModel().selectedRows()
if rows and prefs['urlsfromselected']:
book_ids = self.gui.library_view.get_selected_ids()
print("book_ids: %s"%book_ids)
for book_id in book_ids:
identifiers = self.db.get_identifiers(book_id,index_is_id=True)
if 'url' in identifiers:
# identifiers have :->| in url.
#print("url from book:"+identifiers['url'].replace('|',':'))
url_list.append(identifiers['url'].replace('|',':'))
else:
## only epub has that in it.
if self.db.has_format(book_id,'EPUB',index_is_id=True):
existingepub = self.db.format(book_id,'EPUB',index_is_id=True, as_file=True)
mi = get_metadata(existingepub,'EPUB')
#print("mi:%s"%mi)
identifiers = mi.get_identifiers()
if 'url' in identifiers:
#print("url from epub:"+identifiers['url'].replace('|',':'))
url_list.append(identifiers['url'].replace('|',':'))
return self.get_valid_urls(url_list)
def get_urls_clip(self):
url_list = []
if prefs['urlsfromclip']:
# no rows selected, check for valid URLs in the clipboard.
cliptext = unicode(QApplication.instance().clipboard().text())
url_list.extend(cliptext.split())
return self.get_valid_urls(url_list)
def get_valid_urls(self,url_list):
url_list_text = ""
# Check and make sure the URLs are valid ffdl URLs.
if url_list:
# this is the accepted way to 'check for existance'? really?
try:
self.dummyconfig
except AttributeError:
self.dummyconfig = ConfigParser.SafeConfigParser()
alreadyin=[]
for url in url_list:
if url in alreadyin:
continue
alreadyin.append(url)
# pulling up an adapter is pretty low over-head. If
# it fails, it's a bad url.
try:
adapters.getAdapter(self.dummyconfig,url)
except:
pass
else:
if url_list_text:
url_list_text += "\n"
url_list_text += url
return url_list_text
def apply_settings(self):
# No need to do anything with perfs here, but we could.
prefs
@ -419,6 +447,10 @@ class FanFictionDownLoaderPlugin(InterfaceAction):
# Otherwise list of books doesn't update right away.
if addedcount:
self.gui.library_view.model().books_added(addedcount)
self.gui.library_view.model().refresh()
#self.gui.library_view.model().research()
#self.gui.tags_view.recount()
del adapter
del writer