diff --git a/fanficdownloader/exceptions.py b/fanficdownloader/exceptions.py index 525c64e0..08ce1f2d 100644 --- a/fanficdownloader/exceptions.py +++ b/fanficdownloader/exceptions.py @@ -68,3 +68,10 @@ class UnknownSite(Exception): def __str__(self): return "Unknown Site(%s). Supported sites: (%s)" % (self.url, ", ".join(self.supported_sites_list)) +class FailedToWriteOutput(Exception): + def __init__(self,error): + self.error=error + + def __str__(self): + return self.error + diff --git a/fanficdownloader/html.py b/fanficdownloader/html.py index e1ca7db5..22fb40af 100644 --- a/fanficdownloader/html.py +++ b/fanficdownloader/html.py @@ -17,7 +17,7 @@ class HtmlProcessor: self.unfill = unfill html = self._ProcessRawHtml(html) self._soup = BeautifulSoup(html) - if self._soup.title: + if self._soup.title.contents: self.title = self._soup.title.contents[0] else: self.title = None diff --git a/fanficdownloader/mobi.py b/fanficdownloader/mobi.py index 4748e202..7a527154 100644 --- a/fanficdownloader/mobi.py +++ b/fanficdownloader/mobi.py @@ -8,6 +8,8 @@ import time import random import logging +logger = logging.getLogger(__name__) + from html import HtmlProcessor # http://wiki.mobileread.com/wiki/MOBI @@ -124,8 +126,8 @@ class Converter: tmp = self.MakeOneHTML(html_strs) self._ConvertStringToFile(tmp, out_file) except Exception, e: - logging.error('Error %s', e) - logging.debug('Details: %s' % html_strs) + logger.error('Error %s', e) + #logger.debug('Details: %s' % html_strs) def _ConvertStringToFile(self, html_data, out): html = HtmlProcessor(html_data) diff --git a/fanficdownloader/writers/writer_mobi.py b/fanficdownloader/writers/writer_mobi.py index 6b5d8e65..5da8743b 100644 --- a/fanficdownloader/writers/writer_mobi.py +++ b/fanficdownloader/writers/writer_mobi.py @@ -22,6 +22,9 @@ import StringIO from base_writer import * from ..htmlcleanup import stripHTML from ..mobi import Converter +from ..exceptions import FailedToWriteOutput + +logger = logging.getLogger(__name__) class MobiWriter(BaseStoryWriter): @@ -160,7 +163,7 @@ ${value}
for index, (title,html) in enumerate(self.story.getChapters()): if html: - logging.debug('Writing chapter text for: %s' % title) + logger.debug('Writing chapter text for: %s' % title) vals={'chapter':title, 'index':"%04d"%(index+1), 'number':index+1} fullhtml = CHAPTER_START.substitute(vals) + html + CHAPTER_END.substitute(vals) # ffnet(& maybe others) gives the whole chapter text @@ -175,6 +178,8 @@ ${value}
author=self.getMetadata('author'), publisher=self.getMetadata('site')) mobidata = c.ConvertStrings(files) + if len(mobidata) < 1: + raise FailedToWriteOutput("Zero length mobi output") out.write(mobidata) del files