コード例 #1
0
def make_mobi(book_id):
    kindlegen = os.path.join(config.MAIN_DIR, "vendor", "kindlegen")
    if not os.path.exists(kindlegen):
        app.logger.error("make_mobi: kindlegen binary not found in: %s" %
                         kindlegen)
        return None
    book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
    data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(
        db.Data.format == 'EPUB').first()
    if not data:
        app.logger.error("make_mobi: epub format not found for book id: %d" %
                         book_id)
        return None

    file_path = os.path.join(config.DB_ROOT, book.path, data.name)

    if os.path.exists(file_path + ".epub"):
        check = subprocess.call([kindlegen, file_path + ".epub"],
                                stdout=subprocess.PIPE)
        if not check or check < 2:
            book.data.append(
                db.Data(name=book.data[0].name,
                        format="MOBI",
                        book=book.id,
                        uncompressed_size=os.path.getsize(file_path +
                                                          ".mobi")))
            db.session.commit()
            return file_path + ".mobi"
        else:
            app.logger.error(
                "make_mobi: kindlegen failed with error while converting book")
            return None
    else:
        app.logger.error("make_mobie: epub not found: %s.epub" % file_path)
        return None
コード例 #2
0
def make_mobi(book_id):
    if sys.platform =="win32":
        kindlegen = os.path.join(config.MAIN_DIR, "vendor", u"kindlegen.exe")
    else:
        kindlegen = os.path.join(config.MAIN_DIR, "vendor", u"kindlegen")
    if not os.path.exists(kindlegen):
        app.logger.error("make_mobi: kindlegen binary not found in: %s" % kindlegen)
        return None
    book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
    data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == 'EPUB').first()
    if not data:
        app.logger.error("make_mobi: epub format not found for book id: %d" % book_id)
        return None

    file_path = os.path.join(config.DB_ROOT, book.path, data.name)
    if os.path.exists(file_path + u".epub"):
        p = subprocess.Popen((kindlegen + " \"" + file_path + u".epub\" ").encode(sys.getfilesystemencoding()), shell=True, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        check = p.wait()
        if not check or check < 2:
            book.data.append(db.Data(
                    name=book.data[0].name,
                    format="MOBI",
                    book=book.id,
                    uncompressed_size=os.path.getsize(file_path + ".mobi")
                ))
            db.session.commit()
            return file_path + ".mobi"
        else:
            app.logger.error("make_mobi: kindlegen failed with error while converting book")
            return None
    else:
        app.logger.error("make_mobie: epub not found: %s.epub" % file_path)
        return None
コード例 #3
0
def convert_kindlegen(file_path, book):
    error_message = None
    if not os.path.exists(ub.config.config_converterpath):
        error_message = _(u"kindlegen binary %(kindlepath)s not found",
                          kindlepath=ub.config.config_converterpath)
        web.app.logger.error("convert_kindlegen: " + error_message)
        return error_message, RET_FAIL
    try:
        p = subprocess.Popen(
            (ub.config.config_converterpath + " \"" + file_path +
             u".epub\"").encode(sys.getfilesystemencoding()),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            shell=True)
    except Exception as e:
        error_message = _(u"kindlegen failed, no execution permissions")
        web.app.logger.error("convert_kindlegen: " + error_message)
        return error_message, RET_FAIL
    # Poll process for new output until finished
    while True:
        nextline = p.stdout.readline()
        if nextline == '' and p.poll() is not None:
            break
        if nextline != "\r\n":
            # Format of error message (kindlegen translates its output texts):
            # Error(prcgen):E23006: Language not recognized in metadata.The dc:Language field is mandatory.Aborting.
            conv_error = re.search(".*\(.*\):(E\d+):\s(.*)", nextline)
            # If error occoures, log in every case
            if conv_error:
                error_message = _(
                    u"Kindlegen failed with Error %(error)s. Message: %(message)s",
                    error=conv_error.group(1),
                    message=conv_error.group(2).decode('utf-8'))
                web.app.logger.info("convert_kindlegen: " + error_message)
                web.app.logger.info(nextline.strip('\r\n'))
            else:
                web.app.logger.debug(nextline.strip('\r\n'))

    check = p.returncode
    if not check or check < 2:
        book.data.append(
            db.Data(name=book.data[0].name,
                    book_format="MOBI",
                    book=book.id,
                    uncompressed_size=os.path.getsize(file_path + ".mobi")))
        db.session.commit()
        if ub.config.config_use_google_drive:
            os.remove(file_path + u".epub")
        return file_path + ".mobi", RET_SUCCESS
    else:
        web.app.logger.info(
            "convert_kindlegen: kindlegen failed with error while converting book"
        )
        if not error_message:
            error_message = 'kindlegen failed, no excecution permissions'
        return error_message, RET_FAIL
コード例 #4
0
ファイル: helper.py プロジェクト: wukongtime/calibre-web
def make_mobi(book_id, calibrepath):
    vendorpath = os.path.join(
        os.path.normpath(
            os.path.dirname(os.path.realpath(__file__)) + os.sep +
            "../vendor" + os.sep))
    if sys.platform == "win32":
        kindlegen = (os.path.join(vendorpath, u"kindlegen.exe")).encode(
            sys.getfilesystemencoding())
    else:
        kindlegen = (os.path.join(vendorpath, u"kindlegen")).encode(
            sys.getfilesystemencoding())
    if not os.path.exists(kindlegen):
        app.logger.error("make_mobi: kindlegen binary not found in: %s" %
                         kindlegen)
        return None
    book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
    data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(
        db.Data.format == 'EPUB').first()
    if not data:
        app.logger.error("make_mobi: epub format not found for book id: %d" %
                         book_id)
        return None

    file_path = os.path.join(calibrepath, book.path, data.name)
    if os.path.exists(file_path + u".epub"):
        p = subprocess.Popen((kindlegen + " \"" + file_path +
                              u".epub\"").encode(sys.getfilesystemencoding()),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             shell=True)
        # Poll process for new output until finished
        while True:
            nextline = p.stdout.readline()
            if nextline == '' and p.poll() is not None:
                break
            if nextline != "\r\n":
                app.logger.debug(nextline.strip('\r\n'))

        check = p.returncode
        if not check or check < 2:
            book.data.append(
                db.Data(name=book.data[0].name,
                        book_format="MOBI",
                        book=book.id,
                        uncompressed_size=os.path.getsize(file_path +
                                                          ".mobi")))
            db.session.commit()
            return file_path + ".mobi"
        else:
            app.logger.error(
                "make_mobi: kindlegen failed with error while converting book")
            return None
    else:
        app.logger.error("make_mobie: epub not found: %s.epub" % file_path)
        return None
コード例 #5
0
def convert_calibre(file_path, book):
    error_message = None
    if not os.path.exists(ub.config.config_converterpath):
        error_message = _(u"Ebook-convert binary %(converterpath)s not found",
                          converterpath=ub.config.config_converterpath)
        web.app.logger.error("convert_calibre: " + error_message)
        return error_message, RET_FAIL
    try:
        command = ("\"" + ub.config.config_converterpath + "\" \"" +
                   file_path + u".epub\" \"" + file_path + u".mobi\" " +
                   ub.config.config_calibre).encode(
                       sys.getfilesystemencoding())
        p = subprocess.Popen(command,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             shell=True)
    except Exception as e:
        error_message = _(u"Ebook-convert failed, no execution permissions")
        web.app.logger.error("convert_calibre: " + error_message)
        return error_message, RET_FAIL
    # Poll process for new output until finished
    while True:
        nextline = p.stdout.readline()
        if nextline == '' and p.poll() is not None:
            break
        web.app.logger.debug(
            nextline.strip('\r\n').decode(sys.getfilesystemencoding()))

    check = p.returncode
    if check == 0:
        book.data.append(
            db.Data(name=book.data[0].name,
                    book_format="MOBI",
                    book=book.id,
                    uncompressed_size=os.path.getsize(file_path + ".mobi")))
        db.session.commit()
        if ub.config.config_use_google_drive:
            os.remove(file_path + u".epub")

        return file_path + ".mobi", RET_SUCCESS
    else:
        web.app.logger.info(
            "convert_calibre: Ebook-convert failed with error while converting book"
        )
        if not error_message:
            error_message = 'Ebook-convert failed, no excecution permissions'
        return error_message, RET_FAIL
コード例 #6
0
def make_mobi(book_id, calibrepath):
    error_message = None
    vendorpath = os.path.join(
        os.path.normpath(
            os.path.dirname(os.path.realpath(__file__)) + os.sep +
            "../vendor" + os.sep))
    if sys.platform == "win32":
        kindlegen = (os.path.join(vendorpath, u"kindlegen.exe")).encode(
            sys.getfilesystemencoding())
    else:
        kindlegen = (os.path.join(vendorpath, u"kindlegen")).encode(
            sys.getfilesystemencoding())
    if not os.path.exists(kindlegen):
        error_message = _(u"kindlegen binary %(kindlepath)s not found",
                          kindlepath=kindlegen)
        app.logger.error("make_mobi: " + error_message)
        return error_message, RET_FAIL
    book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
    data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(
        db.Data.format == 'EPUB').first()
    if not data:
        error_message = _(u"epub format not found for book id: %(book)d",
                          book=book_id)
        app.logger.error("make_mobi: " + error_message)
        return error_message, RET_FAIL

    file_path = os.path.join(calibrepath, book.path, data.name)
    if os.path.exists(file_path + u".epub"):
        try:
            p = subprocess.Popen(
                (kindlegen + " \"" + file_path + u".epub\"").encode(
                    sys.getfilesystemencoding()),
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                shell=True)
        except Exception:
            error_message = _(u"kindlegen failed, no execution permissions")
            app.logger.error("make_mobi: " + error_message)
            return error_message, RET_FAIL
        # Poll process for new output until finished
        while True:
            nextline = p.stdout.readline()
            if nextline == '' and p.poll() is not None:
                break
            if nextline != "\r\n":
                # Format of error message (kindlegen translates its output texts):
                # Error(prcgen):E23006: Language not recognized in metadata.The dc:Language field is mandatory.Aborting.
                conv_error = re.search(".*\(.*\):(E\d+):\s(.*)", nextline)
                # If error occoures, log in every case
                if conv_error:
                    error_message = _(
                        u"Kindlegen failed with Error %(error)s. Message: %(message)s",
                        error=conv_error.group(1),
                        message=conv_error.group(2).decode('utf-8'))
                    app.logger.info("make_mobi: " + error_message)
                    app.logger.info(nextline.strip('\r\n'))
                app.logger.debug(nextline.strip('\r\n'))

        check = p.returncode
        if not check or check < 2:
            book.data.append(
                db.Data(name=book.data[0].name,
                        book_format="MOBI",
                        book=book.id,
                        uncompressed_size=os.path.getsize(file_path +
                                                          ".mobi")))
            db.session.commit()
            return file_path + ".mobi", RET_SUCCESS
        else:
            app.logger.info(
                "make_mobi: kindlegen failed with error while converting book")
            if not error_message:
                error_message = 'kindlegen failed, no excecution permissions'
            return error_message, RET_FAIL
    else:
        error_message = "make_mobi: epub not found: %s.epub" % file_path
        return error_message, RET_FAIL
コード例 #7
0
ファイル: main.py プロジェクト: eyafjallajokull/lAiNo
import os
import db
import notes

if __name__ == "__main__":
    if os.name == "nt":
        hl = "Неправильная команда"
        hp = "Слишком много ошибок"
        flag = True
        inter = db.Data()
        while flag:
            for i in range(3):
                try:
                    print("->Главное меню:\n->\t1) Войти в аккаунт")
                    print("->\t2) Создать аккаунт")
                    print("->\t3) Выход из программы")
                    choice = input("Введите 1, 2 или 3: ")
                    choice = choice[0]
                    if choice == "1":
                        log = input("Введите логин: ")
                        pas = input("Введите пароль: ")
                        if inter.Enter(log, pas) is True:
                            print("Вы вошли в систему")
                            Flag = True
                            ntwork = notes.Notes(inter.log)
                            while Flag:
                                try:
                                    print("->Выберите действие:")
                                    print("->\t1) Работа с заметками")
                                    print("->\t2) Управление аккаунтом")
                                    print("->\t3) Выход из программы")