Esempio n. 1
0
class Ebook:
    def __init__(self, ebook_name):
        self.opts = getOpts()
        self.oeb = CreateOeb(log, None, self.opts)
        self.ebook_name = ebook_name

        # 防止发出无内容的空书
        self.has_content = False

        setMetaData(self.oeb, title=ebook_name)

        GENERATE_HTML_TOC = True
        GENERATE_TOC_THUMBNAIL = True

        self.insertHtmlToc = GENERATE_HTML_TOC
        self.insertThumbnail = GENERATE_TOC_THUMBNAIL

        self.sections = OrderedDict()
        self.book = MOBIOutput()

    def as_epub(self):
        self.book = EPUBOutput()
        return self

    def as_mobi(self):
        self.book = MOBIOutput()
        return self

    def add_section(self, book_name, chapter, content):
        self.sections.setdefault(book_name, [])
        self.sections[book_name].append((chapter, "b", "c", "<body>" + content + "</body>"))
        return self

    def add_sections(self, bookname, sections):
        if not sections:
            return

        try:
            for title, content in sections:
                self.add_section(bookname, title, content)
        except Exception as e:
            print(e)

        self.has_content = True

    def get_byte_book(self):
        if not self.has_content:
            return None

        toc_thumbnails = {"c": "https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"}

        InsertToc(self.oeb, self.sections, toc_thumbnails, self.insertHtmlToc, self.insertThumbnail)

        byte_book = byteStringIO()

        self.book.convert(self.oeb, byte_book, self.opts, log)

        return byte_book.getvalue()
Esempio n. 2
0
	def make(self):
		self._oeb = self._create_oeb()
	
		itemcnt, imgindex = 0, 0
		sections = OrderedDict()

		sections.setdefault('test', [])
		sections['test'].append(('test title', 'test brief', 'article content'))
		sections.setdefault('test2', [])
		sections['test2'].append(('test2 title', 'test2 brief', 'article2 content'))

		for k in self._metadata:
			self._oeb.metadata.add(k, self._metadata[k])

		self._toc(sections)

		io = byteStringIO()
		o = MOBIOutput()

		o.convert(self._oeb, io, self._opts, log)

		f = open('test.mobi', 'wb')
		f.write(str(io.getvalue()))
		f.close()
Esempio n. 3
0
class Ebook:
    def __init__(self, ebook_name):
        self.opts = getOpts()
        self.oeb = CreateOeb(log, None, self.opts)
        self.ebook_name = ebook_name

        # 防止发出无内容的空书
        self.has_content = False

        setMetaData(self.oeb, title=ebook_name)

        GENERATE_HTML_TOC = True
        GENERATE_TOC_THUMBNAIL = True

        self.insertHtmlToc = GENERATE_HTML_TOC
        self.insertThumbnail = GENERATE_TOC_THUMBNAIL

        self.sections = OrderedDict()
        self.book = MOBIOutput()

    def as_epub(self):
        self.book = EPUBOutput()
        return self

    def as_mobi(self):
        self.book = MOBIOutput()
        return self

    def add_section(self, book_name, chapter, content):
        self.sections.setdefault(book_name, [])
        self.sections[book_name].append(
            (chapter, "b", "c", "<body>" + content + "</body>"))
        return self

    def add_sections(self, bookname, sections):
        if not sections:
            return

        try:
            for title, content in sections:
                self.add_section(bookname, title, content)
        except Exception as e:
            print(e)

        self.has_content = True

    def get_byte_book(self):
        if not self.has_content:
            return None

        toc_thumbnails = {
            "c":
            "https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
        }

        InsertToc(self.oeb, self.sections, toc_thumbnails, self.insertHtmlToc,
                  self.insertThumbnail)

        byte_book = byteStringIO()

        self.book.convert(self.oeb, byte_book, self.opts, log)

        return byte_book.getvalue()
def createMobi(bk):
    opts = makeoeb.getOpts('kindlepw')
    oeb = makeoeb.CreateOeb(default_log, None, opts)

    title = bk.title

    makeoeb.setMetaData(oeb, title)
    oeb.container = makeoeb.ServerContainer()

    # cover
    mhfile = bk.masthead_path
    coverfile = bk.cover_path

    if mhfile:
        id_, href = oeb.manifest.generate('masthead', mhfile)  # size:600*60
        oeb.manifest.add(id_, href, makeoeb.MimeFromFilename(mhfile))
        oeb.guide.add('masthead', 'Masthead Image', href)

    if coverfile:
        id_, href = oeb.manifest.generate('cover', coverfile)
        item = oeb.manifest.add(id_, href, makeoeb.MimeFromFilename(coverfile))
        oeb.guide.add('cover', 'Cover', href)
        oeb.metadata.add('cover', id_)

    itemcnt, imgindex = 0, 0
    from collections import OrderedDict
    sections = OrderedDict()
    toc_thumbnails = {}  # map img-url -> manifest-href
    if not bk:
        default_log.warn('not exist book <%s>' % bk.title)


    book = base.BaseFeedBook(imgindex=imgindex)
    book.title = bk.title
    book.description = bk.desc
    book.language = bk.lang
    book.oldest_article = bk.oldest_article
    book.fulltext_by_readability = True
    feeds = bk.feeds
    book.feeds = [(feed["title"], feed["url"], feed["fulltext"]) for feed in feeds]

    try:
        for sec_or_media, url, title, content, brief, thumbnail in book.Items(opts):
            if not sec_or_media or not title or not content:
                continue

            if sec_or_media.startswith(r'image/'):
                id_, href = oeb.manifest.generate(id='img', href=title)
                item = oeb.manifest.add(id_, href, sec_or_media, data=content)
                if thumbnail:
                    toc_thumbnails[url] = href
                imgindex += 1
            else:
                # id, href = oeb.manifest.generate(id='feed', href='feed%d.html'%itemcnt)
                # item = oeb.manifest.add(id, href, 'application/xhtml+xml', data=content)
                # oeb.spine.add(item, True)
                sections.setdefault(sec_or_media, [])
                sections[sec_or_media].append((title, brief, thumbnail, content))
                itemcnt += 1
    except Exception as e:
        default_log.warn("Failure in pushing book '%s' : %s" % (book.title, str(e)))
    print itemcnt
    if itemcnt > 0:
        from utils import InsertToc
        InsertToc(oeb, sections, toc_thumbnails)
        oIO = byteStringIO()
        o = MOBIOutput()
        o.convert(oeb, oIO, opts, default_log)

        of_name = "/tmp/"+ time.strftime("%Y-%m-%d_%H%M%S", time.localtime()) + ".mobi"
        with open(of_name, "w") as f:
            f.write(str(oIO.getvalue()))
        return of_name
    return ""
def createMobi(bk):
    opts = makeoeb.getOpts('kindlepw')
    oeb = makeoeb.CreateOeb(default_log, None, opts)

    title = bk.title

    makeoeb.setMetaData(oeb, title)
    oeb.container = makeoeb.ServerContainer()

    # cover
    mhfile = bk.masthead_path
    coverfile = bk.cover_path

    if mhfile:
        id_, href = oeb.manifest.generate('masthead', mhfile)  # size:600*60
        oeb.manifest.add(id_, href, makeoeb.MimeFromFilename(mhfile))
        oeb.guide.add('masthead', 'Masthead Image', href)

    if coverfile:
        id_, href = oeb.manifest.generate('cover', coverfile)
        item = oeb.manifest.add(id_, href, makeoeb.MimeFromFilename(coverfile))
        oeb.guide.add('cover', 'Cover', href)
        oeb.metadata.add('cover', id_)

    itemcnt, imgindex = 0, 0
    from collections import OrderedDict
    sections = OrderedDict()
    toc_thumbnails = {}  # map img-url -> manifest-href
    if not bk:
        default_log.warn('not exist book <%s>' % bk.title)

    book = base.BaseFeedBook(imgindex=imgindex)
    book.title = bk.title
    book.description = bk.desc
    book.language = bk.lang
    book.oldest_article = bk.oldest_article
    book.fulltext_by_readability = True
    feeds = bk.feeds
    book.feeds = [(feed["title"], feed["url"], feed["fulltext"])
                  for feed in feeds]

    try:
        for sec_or_media, url, title, content, brief, thumbnail in book.Items(
                opts):
            if not sec_or_media or not title or not content:
                continue

            if sec_or_media.startswith(r'image/'):
                id_, href = oeb.manifest.generate(id='img', href=title)
                item = oeb.manifest.add(id_, href, sec_or_media, data=content)
                if thumbnail:
                    toc_thumbnails[url] = href
                imgindex += 1
            else:
                # id, href = oeb.manifest.generate(id='feed', href='feed%d.html'%itemcnt)
                # item = oeb.manifest.add(id, href, 'application/xhtml+xml', data=content)
                # oeb.spine.add(item, True)
                sections.setdefault(sec_or_media, [])
                sections[sec_or_media].append(
                    (title, brief, thumbnail, content))
                itemcnt += 1
    except Exception as e:
        default_log.warn("Failure in pushing book '%s' : %s" %
                         (book.title, str(e)))
    print itemcnt
    if itemcnt > 0:
        from utils import InsertToc
        InsertToc(oeb, sections, toc_thumbnails)
        oIO = byteStringIO()
        o = MOBIOutput()
        o.convert(oeb, oIO, opts, default_log)

        of_name = "/tmp/" + time.strftime("%Y-%m-%d_%H%M%S",
                                          time.localtime()) + ".mobi"
        with open(of_name, "w") as f:
            f.write(str(oIO.getvalue()))
        return of_name
    return ""