Example #1
0
def beautify_book(root, f):
    from lib.epubqfix import pack_epub
    from lib.epubqfix import unpack_epub
    from lib.epubqfix import clean_temp
    from lib.epubqfix import find_roots
    f = f.replace('.epub', '_moh.epub')
    print('START beautify for: ' + f.decode(SFENC))
    tempdir = unpack_epub(os.path.join(root, f))
    opf_dir, opf_file, is_fixed = find_roots(tempdir)
    epub_dir = os.path.join(tempdir, opf_dir)
    opf_path = os.path.join(tempdir, opf_file)
    parser = etree.XMLParser(remove_blank_text=True)
    opftree = etree.parse(opf_path, parser)
    ncxfile = etree.XPath(
        '//opf:item[@media-type="application/x-dtbncx+xml"]',
        namespaces=OPFNS
    )(opftree)[0].get('href')
    ncx_path = os.path.join(epub_dir, ncxfile)
    ncxtree = etree.parse(ncx_path, parser)

    rename_calibre_cover(opftree, ncxtree, epub_dir)
    rename_cover_img(opftree, ncxtree, epub_dir)
    fix_body_id_links(opftree, epub_dir, ncxtree)
    make_cover_item_first(opftree)
    cont_src_list = make_content_src_list(ncxtree)
    fix_display_none(opftree, epub_dir, cont_src_list)

    # replace_fonts(epub_dir, ncxtree, opftree, 'TeXGyreSchola', 'Bookerly')
    write_file_changes_back(opftree, opf_path)
    write_file_changes_back(ncxtree, ncx_path)
    pack_epub(os.path.join(root, f), tempdir)
    clean_temp(tempdir)
    print('FINISH beautify for: ' + f.decode(SFENC))
Example #2
0
def fix_name_author(root, f, author, title):
    print('START work for: ' + f.decode(SFENC))
    try:
        tempdir = unpack_epub(os.path.join(root, f))
    except zipfile.BadZipfile:
        print('Unable to process corrupted file...')
        return 0
    opfd, opff, is_fixed = find_roots(tempdir)
    opff_abs = os.path.join(tempdir, opff)
    parser = etree.XMLParser(remove_blank_text=True)
    opftree = etree.parse(opff_abs, parser)
    if author != 'no_author' and author is not None:
        set_author(opftree, author)
    if title != 'no_title' and title is not None:
        set_title(opftree, title)
    with open(opff_abs, 'w') as file:
        file.write(
            etree.tostring(opftree.getroot(),
                           pretty_print=True,
                           standalone=False,
                           xml_declaration=True,
                           encoding='utf-8').decode('utf-8'))
    pack_epub(os.path.join(root, f), tempdir)
    clean_temp(tempdir)
    print('FINISH work for: ' + f.decode(SFENC))
Example #3
0
def beautify_book(root, f, user_font_dir, pair_family):
    from lib.epubqfix import pack_epub
    from lib.epubqfix import unpack_epub
    from lib.epubqfix import clean_temp
    from lib.epubqfix import find_roots
    f = f.replace('.epub', '_moh.epub')
    print('START beautify for: ' + f)
    tempdir = unpack_epub(os.path.join(root, f))
    opf_dir, opf_file, is_fixed = find_roots(tempdir)
    epub_dir = os.path.join(tempdir, opf_dir)
    opf_path = os.path.join(tempdir, opf_file)
    parser = etree.XMLParser(remove_blank_text=True, encoding='utf-8')
    opftree = etree.parse(opf_path, parser)
    ncxfile = etree.XPath('//opf:item[@media-type="application/x-dtbncx+xml"]',
                          namespaces=OPFNS)(opftree)[0].get('href')
    ncx_path = os.path.join(epub_dir, ncxfile)
    ncxtree = etree.parse(ncx_path, parser)

    rename_calibre_cover(opftree, ncxtree, epub_dir)
    rename_cover_img(opftree, ncxtree, epub_dir)
    fix_body_id_links(opftree, epub_dir, ncxtree)
    make_cover_item_first(opftree)
    cont_src_list = make_content_src_list(ncxtree)
    fix_display_none(opftree, epub_dir, cont_src_list)
    replace_fonts(user_font_dir, epub_dir, ncxtree, opftree, pair_family)
    clean_meta_tags(opftree)
    # temprorary disabled due critical problems
    # update_css_font_families(epub_dir, opftree)

    write_file_changes_back(opftree, opf_path)
    write_file_changes_back(ncxtree, ncx_path)
    pack_epub(os.path.join(root, f), tempdir)
    clean_temp(tempdir)
    print('FINISH beautify for: ' + f)
def fix_name_author(root, f, author, title):
    print('START work for: ' + f.decode(SFENC))
    try:
        tempdir = unpack_epub(os.path.join(root, f))
    except zipfile.BadZipfile:
        print('Unable to process corrupted file...')
        return 0
    opfd, opff, is_fixed = find_roots(tempdir)
    opff_abs = os.path.join(tempdir, opff)
    parser = etree.XMLParser(remove_blank_text=True)
    opftree = etree.parse(opff_abs, parser)
    if author != 'no_author' and author is not None:
        set_author(opftree, author)
    if title != 'no_title' and title is not None:
        set_title(opftree, title)
    with open(opff_abs, 'w') as file:
        file.write(etree.tostring(opftree.getroot(), pretty_print=True,
                   standalone=False, xml_declaration=True, encoding='utf-8'))
    pack_epub(os.path.join(root, f), tempdir)
    clean_temp(tempdir)
    print('FINISH work for: ' + f.decode(SFENC))
Example #5
0
def beautify_book(root, f, user_font_dir, pair_family):
    from lib.epubqfix import pack_epub
    from lib.epubqfix import unpack_epub
    from lib.epubqfix import clean_temp
    from lib.epubqfix import find_roots
    f = f.replace('.epub', '_moh.epub')
    print('START beautify for: ' + f)
    tempdir = unpack_epub(os.path.join(root, f))
    opf_dir, opf_file, is_fixed = find_roots(tempdir)
    epub_dir = os.path.join(tempdir, opf_dir)
    opf_path = os.path.join(tempdir, opf_file)
    parser = etree.XMLParser(remove_blank_text=True)
    opftree = etree.parse(opf_path, parser)
    ncxfile = etree.XPath(
        '//opf:item[@media-type="application/x-dtbncx+xml"]',
        namespaces=OPFNS
    )(opftree)[0].get('href')
    ncx_path = os.path.join(epub_dir, ncxfile)
    ncxtree = etree.parse(ncx_path, parser)

    rename_calibre_cover(opftree, ncxtree, epub_dir)
    rename_cover_img(opftree, ncxtree, epub_dir)
    fix_body_id_links(opftree, epub_dir, ncxtree)
    make_cover_item_first(opftree)
    cont_src_list = make_content_src_list(ncxtree)
    fix_display_none(opftree, epub_dir, cont_src_list)
    replace_fonts(user_font_dir, epub_dir, ncxtree, opftree, pair_family)
    clean_meta_tags(opftree)
    # temprorary disabled due critical problems
    # update_css_font_families(epub_dir, opftree)

    write_file_changes_back(opftree, opf_path)
    write_file_changes_back(ncxtree, ncx_path)
    pack_epub(os.path.join(root, f), tempdir)
    clean_temp(tempdir)
    print('FINISH beautify for: ' + f)