Quantcast
Channel: MobileRead Forums - Kindle Developer's Corner
Viewing all articles
Browse latest Browse all 4457

Tools Crude way to generate mobi file in python (should run on kindle directly)

$
0
0
Hi,

I did some work to be able to generate a mobi file in python. The lib in theory should be able to parse the mobi file as well, but it was not well tested.

Code could use a lot of cleanup, but if somebody is looking for such lib, maybe he/she will want to put in that work.

Hope it's going to be usefull for at least a single person. A bonus is that in theory (didn't test that yet) it could work on jailbroken kindle with python extension installed.

https://gitlab.com/jmikucki/mobicreator/

This is how I use it in my code:

Code:

from mobicreator.mobipublication import Publication
import glob
import os

class OutputMobi():
    def __init__(self, **kwargs):
        print(kwargs)
        self._path = kwargs.get('path', './output')
        self._file_name = kwargs.get('file_name', 'articles.mobi')
        self._img_path = kwargs.get('img_path', './img')
        self._cover_img_path = kwargs.get('cover_img_path', None)
        self._thumb_img_path = kwargs.get('thumb_img_path', None)
        self._title = kwargs.get('title', 'Unknown title')

    def output_articles(self, articles):
        SECTION_CUT_SIZE = 4000

        print(f'Creating mobi file.')
        mobi = Publication()
        mobi.setTitle(self._title)
        print(f'\tAdding text sections.')
        for a in articles:
            mobi.addText(data=a.encode('utf-8'), section_bytes=SECTION_CUT_SIZE)
        print(f'\tAdding image sections.')
        img_files = glob.glob(f'{self._img_path}/img*')
        for img_file_path in img_files:
            print(f'Adding image: {img_file_path}')
            mobi.addImage(file=img_file_path)

        if self._cover_img_path is not None:
            mobi.setCover(file=self._cover_img_path)

        if self._thumb_img_path is not None:
            mobi.setThumb(file=self._thumb_img_path)
       
        print(f'\tWriting mobi file.')
        mobi.write(f'{self._path}/{self._file_name}')
        print(f'Done.')


Viewing all articles
Browse latest Browse all 4457

Trending Articles