From 5511b4fd28161690746fbec45866e30f1814f92b Mon Sep 17 00:00:00 2001 From: Jim Miller Date: Fri, 16 Dec 2011 23:01:18 -0600 Subject: [PATCH] Store/use personal.ini, is_adult and user/pass dialogs when needed. --- calibre-plugin/about.txt | 5 +- calibre-plugin/config.py | 11 ++- calibre-plugin/plugin.py | 80 +++++++++++++++++++--- fanficdownloader/adapters/adapter_test1.py | 2 +- 4 files changed, 83 insertions(+), 15 deletions(-) diff --git a/calibre-plugin/about.txt b/calibre-plugin/about.txt index ae4d6021..cb9e328a 100644 --- a/calibre-plugin/about.txt +++ b/calibre-plugin/about.txt @@ -1,8 +1,9 @@ FanFictionDownLoader Plugin Demo =========================== -Created by Jim Miller, borrowing heavily from Kovid Goyal's 'The -InterfacePlugin Demo' +http://code.google.com/p/fanficdownloader/ + +Created by Jim Miller, borrowing heavily from Kovid Goyal's 'The InterfacePlugin Demo' Requires calibre >= 0.7.53 diff --git a/calibre-plugin/config.py b/calibre-plugin/config.py index 95374a83..ce30df12 100644 --- a/calibre-plugin/config.py +++ b/calibre-plugin/config.py @@ -7,7 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2011, Fanficdownloader team' __docformat__ = 'restructuredtext en' -from PyQt4.Qt import QWidget, QHBoxLayout, QLabel, QLineEdit +from PyQt4.Qt import QWidget, QVBoxLayout, QLabel, QLineEdit, QTextEdit from calibre.utils.config import JSONConfig @@ -20,12 +20,13 @@ prefs = JSONConfig('plugins/fanfictiondownloader_plugin') # Set defaults prefs.defaults['hello_world_msg'] = 'Hello, World!' +prefs.defaults['personal.ini'] = get_resources('example.ini') class ConfigWidget(QWidget): def __init__(self): QWidget.__init__(self) - self.l = QHBoxLayout() + self.l = QVBoxLayout() self.setLayout(self.l) self.label = QLabel('Hello world &message:') @@ -36,6 +37,12 @@ class ConfigWidget(QWidget): self.l.addWidget(self.msg) self.label.setBuddy(self.msg) + self.ini = QTextEdit(self) + self.ini.setLineWrapMode(QTextEdit.NoWrap) + self.ini.setText(prefs['personal.ini']) + self.l.addWidget(self.ini) + def save_settings(self): prefs['hello_world_msg'] = unicode(self.msg.text()) + prefs['personal.ini'] = unicode(self.ini.toPlainText()) diff --git a/calibre-plugin/plugin.py b/calibre-plugin/plugin.py index d7395c57..3ff5d0e7 100644 --- a/calibre-plugin/plugin.py +++ b/calibre-plugin/plugin.py @@ -15,7 +15,8 @@ if False: from StringIO import StringIO -from PyQt4.Qt import (QDialog, QVBoxLayout, QPushButton, QMessageBox, QLabel, QLineEdit) +from PyQt4.Qt import (QDialog, QVBoxLayout, QGridLayout, QPushButton, QMessageBox, + QLabel, QLineEdit, QInputDialog ) from calibre.ptempfile import PersistentTemporaryFile from calibre.ebooks.metadata.epub import get_metadata @@ -62,13 +63,12 @@ class DemoDialog(QDialog): # self.view_button.clicked.connect(self.view) # self.l.addWidget(self.view_button) - self.label = QLabel('Story URL:') - self.l.addWidget(self.label) + self.l.addWidget(QLabel('Story &URL:')) - self.msg = QLineEdit(self) -# self.msg.setText(prefs['hello_world_msg']) - self.l.addWidget(self.msg) - self.label.setBuddy(self.msg) + self.url = QLineEdit(self) + #self.url.setText('http://test1.com?sid=668') + self.l.addWidget(self.url) + self.label.setBuddy(self.url) self.ffdl_button = QPushButton( 'Download Story', self) @@ -116,14 +116,36 @@ class DemoDialog(QDialog): config = ConfigParser.SafeConfigParser() config.readfp(StringIO(get_resources("defaults.ini"))) - print("URL:"+unicode(self.msg.text())) - adapter = adapters.getAdapter(config,unicode(self.msg.text())) + config.readfp(StringIO(prefs['personal.ini'])) + print("URL:"+unicode(self.url.text())) + adapter = adapters.getAdapter(config,unicode(self.url.text())) # "http://test1.com?sid=6646") # http://www.fanfiction.net/s/6439390/1/All_Hallows_Eve") # + try: + adapter.getStoryMetadataOnly() + except exceptions.FailedToLogin: + print("Login Failed, Need Username/Password.") + userpass = UserPassDialog(self.gui) + userpass.exec_() # exec_ will make it act modal + if userpass.status: + adapter.username = userpass.user.text() + adapter.password = userpass.passwd.text() + except exceptions.AdultCheckRequired: + adult = QMessageBox.warning(self, 'Are You Adult?', + "This story requires that you be an adult. Please confirm you are an adult in your locale:", + QMessageBox.Yes | QMessageBox.No, + QMessageBox.No) + + if adult == QMessageBox.Yes: + adapter.is_adult=True + + adapter.getStoryMetadataOnly() + writer = writers.getWriter("epub",config,adapter) tmp = PersistentTemporaryFile(".epub") - writer.writeStory(tmp) print("tmp: "+tmp.name) + + writer.writeStory(tmp) mi = get_metadata(tmp,extract_cover=False) self.db.add_books([tmp],["EPUB"],[mi]) self.hide() @@ -154,3 +176,41 @@ class DemoDialog(QDialog): # Apply the changes self.label.setText(prefs['hello_world_msg']) +class UserPassDialog(QDialog): + + def __init__(self, gui): + QDialog.__init__(self, gui) + self.gui = gui + self.status=False + self.setWindowTitle('User/Password') + + self.l = QGridLayout() + self.setLayout(self.l) + + self.l.addWidget(QLabel("This site/story requires you to login."),0,0,1,2) + + self.l.addWidget(QLabel("User:"),1,0) + self.user = QLineEdit(self) + self.l.addWidget(self.user,1,1) + + self.l.addWidget(QLabel("Password:"),2,0) + self.passwd = QLineEdit(self) + self.l.addWidget(self.passwd,2,1) + + self.ok_button = QPushButton('OK', self) + self.ok_button.clicked.connect(self.ok) + self.l.addWidget(self.ok_button,3,0) + + self.cancel_button = QPushButton('Cancel', self) + self.cancel_button.clicked.connect(self.cancel) + self.l.addWidget(self.cancel_button,3,1) + + self.resize(self.sizeHint()) + + def ok(self): + self.status=True + self.hide() + + def cancel(self): + self.status=False + self.hide() diff --git a/fanficdownloader/adapters/adapter_test1.py b/fanficdownloader/adapters/adapter_test1.py index 05f6ee5e..699d690f 100644 --- a/fanficdownloader/adapters/adapter_test1.py +++ b/fanficdownloader/adapters/adapter_test1.py @@ -63,7 +63,7 @@ class TestSiteAdapter(BaseSiteAdapter): if self.story.getMetadata('storyId') == '664': self.story.setMetadata(u'title',"Test Story Title "+self.crazystring) else: - self.story.setMetadata(u'title',"Test Story Title") + self.story.setMetadata(u'title',"Test Story Title "+self.story.getMetadata('storyId')) self.story.setMetadata('storyUrl',self.url) self.story.setMetadata('description',u'Description '+self.crazystring+u''' Done