diff --git a/downloader.py b/downloader.py index 91d599a0..fabec221 100644 --- a/downloader.py +++ b/downloader.py @@ -55,10 +55,13 @@ def main(): help="set an option NAME=VALUE", metavar="NAME=VALUE") parser.add_option("-m", "--meta-only", action="store_true", dest="metaonly", - help="Retrieve metadata and stop. Write title_page only epub if epub.",) + help="Retrieve metadata and stop. Or, if --update-epub, update metadata title page only.",) parser.add_option("-u", "--update-epub", action="store_true", dest="update", help="Update an existing epub with new chapter, give epub filename instead of storyurl. Not compatible with inserted TOC.",) + parser.add_option("--force", + action="store_true", dest="force", + help="Force update of an existing epub, download and overwrite all chapters.",) (options, args) = parser.parse_args() @@ -94,6 +97,8 @@ def main(): striptitletoc=True, forceunique=False) print "Updating %s, URL: %s" % (args[0],url) + filename = args[0] + config.set("overrides","output_filename",args[0]) else: url = args[0] @@ -114,10 +119,10 @@ def main(): adapter.is_adult=True adapter.getStoryMetadataOnly() - if options.update: + if options.update and not options.force: urlchaptercount = int(adapter.getStoryMetadataOnly().getMetadata('numChapters')) - if chaptercount == urlchaptercount: + if chaptercount == urlchaptercount and not options.metaonly: print "%s already contains %d chapters." % (args[0],chaptercount) elif chaptercount > urlchaptercount: print "%s contains %d chapters, more than source: %d." % (args[0],chaptercount,urlchaptercount) @@ -127,13 +132,15 @@ def main(): ## Even if the title page isn't included, this carries the metadata. titleio = StringIO() writeStory(config,adapter,"epub",metaonly=True,outstream=titleio) - - ## Go get the new chapters only in another epub. - newchaptersio = StringIO() - adapter.setChaptersRange(chaptercount+1,urlchaptercount) - config.set("overrides",'include_tocpage','false') - config.set("overrides",'include_titlepage','false') - writeStory(config,adapter,"epub",outstream=newchaptersio) + + newchaptersio = None + if not options.metaonly: + ## Go get the new chapters only in another epub. + newchaptersio = StringIO() + adapter.setChaptersRange(chaptercount+1,urlchaptercount) + config.set("overrides",'include_tocpage','false') + config.set("overrides",'include_titlepage','false') + writeStory(config,adapter,"epub",outstream=newchaptersio) # out = open("testing/titleio.epub","wb") # out.write(titleio.getvalue()) diff --git a/epubmerge.py b/epubmerge.py index f72e7add..a9c51933 100644 --- a/epubmerge.py +++ b/epubmerge.py @@ -150,6 +150,8 @@ def doMerge(outputio,files,authoropts=[],titleopt=None,descopt=None, booknum=1 firstmetadom = None for file in files: + if file == None : continue + book = "%d" % booknum bookdir = "" bookid = "" @@ -174,7 +176,10 @@ def doMerge(outputio,files,authoropts=[],titleopt=None,descopt=None, metadom = parseString(epub.read(rootfilename)) if booknum==1: firstmetadom = metadom.getElementsByTagName("metadata")[0] - source=firstmetadom.getElementsByTagName("dc:source")[0].firstChild.data.encode("utf-8") + try: + source=firstmetadom.getElementsByTagName("dc:source")[0].firstChild.data.encode("utf-8") + except: + source="" #print "Source:%s"%source ## Save indiv book title @@ -211,7 +216,7 @@ def doMerge(outputio,files,authoropts=[],titleopt=None,descopt=None, try: outputepub.writestr(href, epub.read(relpath+item.getAttribute("href"))) - if re.match(r'.*/file\d+\.xhtml',href): + if re.match(r'.*/(file|chapter)\d+\.xhtml',href): filecount+=1 items.append((id,href,item.getAttribute("media-type"))) filelist.append(href) diff --git a/fanficdownloader/writers/base_writer.py b/fanficdownloader/writers/base_writer.py index 32c742a3..ebf416bd 100644 --- a/fanficdownloader/writers/base_writer.py +++ b/fanficdownloader/writers/base_writer.py @@ -110,18 +110,14 @@ class BaseStoryWriter(Configurable): return self.formatFileName(self.getConfig('output_filename')) def getZipFileName(self): - return self.formatFileName(self.getConfig('zip_filename'),extension=".zip") + return self.formatFileName(self.getConfig('zip_filename')) - def formatFileName(self,template,extension="${formatext}"): + def formatFileName(self,template): values = self.story.metadata # fall back default: if not template: template="${title}-${siteabbrev}_${storyId}${formatext}" - # Add extension if not already included. - if extension not in template: - template+=extension - if not self.getConfig('allow_unsafe_filename'): values={} pattern = re.compile(r"[^a-zA-Z0-9_\. \[\]\(\)&'-]+")