From a01e0e2ed6e61a9ef8c6bd064aebac8b458c9a44 Mon Sep 17 00:00:00 2001 From: sigizmund Date: Thu, 17 Dec 2009 14:37:31 +0000 Subject: [PATCH] Fixed few problems, including the slash in filename --- downaloder.py | 4 ++++ output.py | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/downaloder.py b/downaloder.py index 58c375a3..10dde292 100644 --- a/downaloder.py +++ b/downaloder.py @@ -53,6 +53,8 @@ if __name__ == '__main__': adapter = ffa.FFA(url) elif url.find('fictionalley') != -1: adapter = fictionalley.FictionAlley(url) + print >> sys.stderr, "FictionAlley adapter is broken, try to find this fic on fanfiction.net or fanficauthors" + sys.exit(0) elif url.find('ficwad') != -1: adapter = ficwad.FicWad(url) elif url.find('fanfiction.net') != -1: @@ -63,6 +65,8 @@ if __name__ == '__main__': if format == 'epub': writerClass = output.EPubFanficWriter + elif format == 'html': + writerClass = output.HTMLWriter if adapter.requiresLogin(url): print("Meow, URL %s requires you to haz been logged in! Please can I haz this datas?" % url) diff --git a/output.py b/output.py index 3159fe78..222618c9 100644 --- a/output.py +++ b/output.py @@ -7,6 +7,7 @@ import cgi import uuid import codecs import shutil +import string import base64 import os.path import zipfile @@ -17,9 +18,11 @@ import urlparse as up import BeautifulSoup as bs import htmlentitydefs as hdefs +import zipdir +import html_constants from constants import * -import zipdir + class FanficWriter: def __init__(self): @@ -32,14 +35,36 @@ class FanficWriter: pass class HTMLWriter(FanficWriter): + body = '' + def __init__(self, base, name, author): - pass - + self.basePath = base + self.name = name.replace(" ", "_") + self.storyTitle = name + self.fileName = self.basePath + '/' + self.name + '.html' + self.authorName = author + + if os.path.exists(self.fileName): + os.remove(self.fileName) + + + self.xhtmlTemplate = string.Template(html_constants.XHTML_START) + self.chapterStartTemplate = string.Template(html_constants.XHTML_CHAPTER_START) + def writeChapter(self, title, text): - pass + title = title.decode('utf-8') + text = text.decode('utf-8') + self.body = self.body + '\n' + self.chapterStartTemplate.substitute({'chapter' : title}) + self.body = self.body + '\n' + text def finalise(self): - pass + html = self.xhtmlTemplate.substitute({'title' : self.storyTitle, 'author' : self.authorName, 'body' : self.body}) + soup = bs.BeautifulSoup(html) + result = soup.prettify() + + f = open(self.fileName, 'w') + f.write(result) + f.close() class EPubFanficWriter(FanficWriter): chapters = [] @@ -74,7 +99,7 @@ class EPubFanficWriter(FanficWriter): return text def writeChapter(self, title, text): - fileName = base64.b64encode(title) + ".xhtml" + fileName = base64.b64encode(title).replace('/', '_') + ".xhtml" filePath = self.directory + "/OEBPS/" + fileName f = open(filePath, 'w')