2013-09-16 01:43:58 +00:00
|
|
|
"""
|
|
|
|
Utilities that manipulate epub files
|
|
|
|
"""
|
|
|
|
|
2014-01-15 13:32:55 +00:00
|
|
|
from regluit.pyepub import EPUB, InvalidEpub
|
2013-09-16 01:43:58 +00:00
|
|
|
from StringIO import StringIO
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
|
|
|
def personalize(epub_file, acq):
|
|
|
|
output = EPUB(epub_file, "a")
|
|
|
|
context={'acq':acq}
|
2013-11-12 23:48:43 +00:00
|
|
|
part = StringIO(unicode(render_to_string('epub/datedcc_license.xhtml', context)))
|
2013-09-16 01:43:58 +00:00
|
|
|
output.addpart(part, "datedcc_license.xhtml", "application/xhtml+xml", 1) #after title, we hope
|
2013-09-20 21:44:37 +00:00
|
|
|
output.addmetadata('rights','%s after %s'%(acq.work.last_campaign().license_url,acq.work.last_campaign().cc_date))
|
2014-04-30 19:17:10 +00:00
|
|
|
personalized_epub= StringIO()
|
|
|
|
output.writetodisk(personalized_epub)
|
|
|
|
#logger.info("personalized")
|
|
|
|
return personalized_epub
|
2014-09-05 20:35:57 +00:00
|
|
|
|
|
|
|
def ask_epub(epub_file, context):
|
|
|
|
output = EPUB(epub_file, "a")
|
|
|
|
part = StringIO(unicode(render_to_string('epub/ask.xhtml', context)))
|
|
|
|
output.addpart(part, "ask.xhtml", "application/xhtml+xml", 1) #after title, we hope
|
|
|
|
asking_epub= StringIO()
|
|
|
|
output.writetodisk(asking_epub)
|
|
|
|
return asking_epub
|
|
|
|
|
2013-11-15 04:16:55 +00:00
|
|
|
|
|
|
|
def ungluify(epub_file, campaign):
|
|
|
|
output = EPUB(epub_file, "a")
|
|
|
|
context={'campaign':campaign}
|
|
|
|
part = StringIO(unicode(render_to_string('epub/cc_license.xhtml', context)))
|
|
|
|
output.addpart(part, "cc_license.xhtml", "application/xhtml+xml", 1) #after title, we hope
|
|
|
|
output.addmetadata('rights', campaign.license_url)
|
|
|
|
output.close()
|
2014-01-15 13:32:55 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
from regluit.booxtream import BooXtream
|
|
|
|
watermarker = BooXtream()
|
|
|
|
|
|
|
|
def test_epub(epub_file):
|
|
|
|
try:
|
|
|
|
output = EPUB(epub_file, "a")
|
|
|
|
output.close()
|
|
|
|
#just use Booxtream to run epubcheck
|
|
|
|
params={
|
|
|
|
'customername': 'epubcheck',
|
|
|
|
'languagecode':'1033',
|
|
|
|
'expirydays': 1,
|
|
|
|
'downloadlimit': 1,
|
|
|
|
'exlibris':0,
|
|
|
|
'chapterfooter': 0,
|
|
|
|
'disclaimer':0,
|
|
|
|
'referenceid': 'N/A',
|
|
|
|
'kf8mobi': False,
|
|
|
|
'epub': True,
|
|
|
|
}
|
|
|
|
output.filename.seek(0)
|
|
|
|
boox = watermarker.platform(epubfile=output.filename, **params)
|
|
|
|
return None
|
|
|
|
except Exception as e:
|
|
|
|
return "error:%s" % e
|