Beispiel #1
0
def test_file(the_file, fformat):
    if the_file and the_file.name:
        if fformat == 'epub':
            try:
                book = EPUB(the_file.file)
            except Exception as e:
                logger.exception(e)
                raise ValidationError(
                    _('Are you sure this is an EPUB file?: %s' % e))
        elif fformat == 'mobi':
            try:
                book = Mobi(the_file.file)
                book.parse()
            except Exception as e:
                logger.exception(e)
                #raise ValidationError(_('Are you sure this is a MOBI file?: %s' % e))
                raise e
        elif fformat == 'pdf':
            try:
                PdfFileReader(the_file.file)
            except Exception as e:
                logger.exception(e)
                raise ValidationError(
                    _('%s is not a valid PDF file' % the_file.name))
    return True
Beispiel #2
0
def ungluify(epub_file, campaign):
    output = EPUB(epub_file, "a")
    context = {'campaign': campaign}
    part = StringIO(str(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()
    return output
Beispiel #3
0
def personalize(epub_file, acq):
    output = EPUB(epub_file, "a")
    context = {'acq': acq}
    part = StringIO(
        str(render_to_string('epub/datedcc_license.xhtml', context)))
    output.addpart(part, "datedcc_license.xhtml", "application/xhtml+xml",
                   1)  #after title, we hope
    output.addmetadata(
        'rights', '%s after %s' % (acq.work.last_campaign().license_url,
                                   acq.work.last_campaign().cc_date))
    personalized_epub = BytesIO()
    output.writetodisk(personalized_epub)
    #logger.info("personalized")
    return personalized_epub
Beispiel #4
0
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
Beispiel #5
0
def ask_epub(epub_file, context):
    output = EPUB(epub_file, "a")
    part = StringIO(str(render_to_string('epub/ask.xhtml', context)))
    output.addpart(part, "ask.xhtml", "application/xhtml+xml",
                   1)  #after title, we hope
    asking_epub = BytesIO()
    output.writetodisk(asking_epub)
    return asking_epub
Beispiel #6
0
# -*- coding: utf-8 -*-
import os
import sys
from time import time

from pyepub import EPUB

filename = sys.argv[1]

# 加载ePub文件
epub = EPUB(filename)

# EPUB对象属性
sha1 = epub.sha1  # ePub文件SHA-1(str)
nav = epub.nav  # ePub目录(list)
nav_point = epub.nav_point  # ePub章节(dict)
items = epub.items  # ePub文件(dict)
metadata = epub.metadata  # ePub元数据(dict)

# 遍历ePub元数据(可用epub[name]快速访问)
for name, data in metadata.items():
    print("%s:" % name.capitalize(), data)

# 获取ePub文件内资源
cover = epub.get_file(epub["cover"])

# 转换至mobi格式
t = time()
with open(os.path.splitext(filename)[0] + ".mobi", "wb") as file:
    file.write(epub.convert_to_mobi())
t1 = time() - t
Beispiel #7
0
def add_gitberg_info(epub_file_name):
    epub_file = file(epub_file_name)
    output = EPUB(epub_file, "a")
    output.addpart(make_gitberg_info(), ABOUT, "application/xhtml+xml",
                   1)  #after title, we hope
    output.writetodisk('book.epub')
Beispiel #8
0
def add_gitberg_info(epub_file_name):
    with open(epub_file_name, 'r+b') as epub_from_file:
        epub = EPUB(epub_from_file, mode='a')
        epub.addpart(make_gitberg_info(), ABOUT, "application/xhtml+xml", 1) #after title, we hope
        epub.writetodisk('book.epub')