Allow one-shot fics for ffnet.

Write epub files as mode 'b' binary so it works on Windows.
Allow hr tags in epub output.
Don't compress file mimetype in epub file--required by standard.
This commit is contained in:
retiefjimm 2010-09-24 23:36:05 -05:00
parent 2b2e7d52ec
commit b22d961e73
4 changed files with 23 additions and 10 deletions

0
books/place holder.txt Normal file
View file

View file

@ -40,7 +40,7 @@ class FFNet(FanfictionSiteAdapter):
self.path = parsedUrl.path
self.storyName = 'FF.Net story'
self.storyName = 'FF.Net author'
self.authorName = 'FF.Net author'
spl = self.path.split('/')
if len(spl) == 5:
@ -108,7 +108,9 @@ class FFNet(FanfictionSiteAdapter):
title = o.string
logging.debug('URL = `%s`, Title = `%s`' % (url, title))
urls.append((url,title))
if len(urls) == 0:
# no chapters found, try url by itself.
urls.append((self.url,self.storyName))
return urls
def getText(self, url):

View file

@ -109,7 +109,7 @@ class EPubFanficWriter(FanficWriter):
files = {}
def _writeFile(self, fileName, data):
logging.debug('_writeFile(`%s`, data)' % fileName)
#logging.debug('_writeFile(`%s`, data)' % fileName)
if fileName in self.files:
try:
d = data.decode('utf-8')
@ -189,20 +189,22 @@ class EPubFanficWriter(FanficWriter):
text = self._removeEntities(text)
self.soup = bs.BeautifulStoneSoup(text.decode('utf-8'))
# BeautifulStoneSoup doesn't have any selfClosingTags by default.
# hr needs to be if it's going to work.
self.soup = bs.BeautifulStoneSoup(text.decode('utf-8'), selfClosingTags=('hr'))
allTags = self.soup.findAll(recursive=True)
for t in allTags:
for attr in t._getAttrMap().keys():
if attr not in acceptable_attributes:
del t[attr]
allPs = self.soup.findAll(recursive=True)
for p in allPs:
if p.string != None and (len(p.string.strip()) == 0 or p.string.strip() == ' ' ) :
p.extract()
allBrs = self.soup.findAll(recursive=True, name = ["br", "hr", 'div'])
allBrs = self.soup.findAll(recursive=True, name = ["br", 'div'])
for br in allBrs:
if (br.string != None and len(br.string.strip()) != 0) or (br.contents != None):
br.name = 'p'
@ -270,7 +272,7 @@ class EPubFanficWriter(FanficWriter):
zipdata = zipdir.inMemoryZip(self.files)
if self.writeToFile:
f = open(filename, 'w')
f = open(filename, 'wb')
f.write(zipdata.getvalue())
f.close()
else:

View file

@ -17,7 +17,11 @@ def toZip(filename, directory):
if os.path.isfile(each):
print(each)
zippedHelp.write(each, arcname=entity)
# epub standard requires mimetype to be uncompressed and first file.
if entity == 'mimetype':
zippedHelp.write(each, arcname=entity, compress_type=zipfile.ZIP_STORED)
else:
zippedHelp.write(each, arcname=entity)
else:
addFolderToZip(zippedHelp,entity, each)
@ -51,10 +55,15 @@ def inMemoryZip(files):
# logging.debug(data)
logging.debug("Writing ZIP path %s" % path)
# epub standard requires mimetype to be uncompressed and first file.
if path == 'mimetype':
compress=zipfile.ZIP_STORED
else:
compress=zipfile.ZIP_DEFLATED
try:
memzip.writestr(path, data.encode('utf-8'))
memzip.writestr(path, data.encode('utf-8'), compress_type=compress)
except UnicodeDecodeError, e:
memzip.writestr(path.encode('utf-8'), data.encode('utf-8'))
memzip.writestr(path.encode('utf-8'), data.encode('utf-8'), compress_type=compress)
for zf in memzip.filelist:
zf.create_system = 0