Exemplo n.º 1
0
def suppr_fichiers_suspects(monepub):
    list_fichiers_suspects = ('license', 'licence', 'rights', 'right', 'iTunesMetadata.plist')
    for root, dir, file in os.walk(monepub.chemin):
        for name in file :
            if name in list_fichiers_suspects or name[0:7] == 'license' or name[0:5] == 'right':
                job.logtat('suppression du fichier : ' + name)
                os.remove(os.path.join(root, name))
    return
Exemplo n.º 2
0
def metas_xhtml(monepub, copyr):
    XHTML_NS = 'http://www.w3.org/1999/xhtml'
    XMLNS_NS = 'http://www.w3.org/2000/xmlns/'
    META_XP = XPath('/h:html/h:head/h:meta[@http-equiv="Content-Type"]')
    TITLE_XP = XPath('/h:html/h:head/h:title')
    METACONT_XP = XPath('/h:html/h:head/h:meta[@content]')
    METAADEPT1_XP = XPath('//h:meta[@name="Adept.resource"]')
    METAADEPT2_XP = XPath('//h:meta[@name="Adept.expected.resource"]')
    copyfind = None

    for path in monepub.liste_fichiers :
        if path.endswith(('html', 'htm')):
            if not os.path.isfile(path):
                job.log('Fichier non présent dans l\'epub : ' + path)
                continue
            data = parse_file(path)
            if data is None:
                job.log('\t Fichier ' + os.path.basename(path) + ' malformé, impossible à parser')
                continue
            try:
                for elem in data.iter(tag=etree.Comment):
                    if elem.text:
                        tail = elem.tail
                        parent = elem.getparent()
                        parent.remove(elem)
                        parent.text = tail
                for meta in METACONT_XP(data):
                    if (meta.get('content') != 'text/html; charset=utf-8') and (meta.get('content') != 'text/html; charset=UTF-8'):
                        job.logtat('suppression du meta : "' + meta.get('name') + ' : ' + meta.get('content') + '"')
                        meta.getparent().remove(meta)

                for title in TITLE_XP(data):
                    title.getparent().remove(title)

                for meta in METAADEPT1_XP(data):
                    job.logtat('suppression du meta : ' + meta.get('name'))
                    meta.getparent().remove(meta)
                for meta in METAADEPT2_XP(data):
                    job.logtat('suppression du meta : ' + meta.get('name'))
                    meta.getparent().remove(meta)
                texte = data.xpath("string()")
                copyfind = re.findall('\u00A9\s*?.*?([0-9]{4})', texte)
                if copyfind:
                    copyright = copyfind
                with open(path, mode='wb',):
                    data.write(path, encoding='utf-8', xml_declaration=True, pretty_print=True)
            except:
                job.log('\t metas des html non modifiées')
                continue

        if path.endswith(('opf', 'ncx')):
            data = parse_file(path)
            if data is None:
                job.log('\t Fichier ' + os.path.basename(path) + ' malformé, impossible à parser')
                continue
            for elem in data.iter(tag=etree.Comment):
                if elem.text:
                    elem.getparent().remove(elem)
            try:
                for meta in METAADEPT1_XP(data):
                    meta.getparent().remove(meta)
                for meta in METAADEPT2_XP(data):
                    meta.getparent().remove(meta)
            except:
                job.log('Fichier ' + path + ' non modifié')
                continue
            with open(path, mode='wb',):
                data.write(path, encoding='utf-8', xml_declaration=True, pretty_print=True)
    if copyr and copyfind:
        return maj_publication(monepub, copyright)
    else :
        return
Exemplo n.º 3
0
def modif_metas_tatoo(monepub):
    # crée un nouvel identifiant uuid
    monepub.uid = 'urn:uuid:' + '%s' % uuid.uuid4()
    parser = etree.XMLParser(attribute_defaults=False, load_dtd=False, remove_comments=False, ns_clean=True)  # , recover=True)

    # traitement de toc.ncx
    mytoc = os.path.normpath(monepub.chemin + '\\' + monepub.content_path + '\\' + monepub.item_toc.href)
    tree = etree.parse(mytoc, parser)
    # on remplace l'identifiant par le nouveau
    meta_uid = XPath('//dt:meta[@name="dtb:uid"]')
    for key, value in enumerate(meta_uid(tree)):
        value.set('content', monepub.uid)
    texte = etree.tostring(tree, encoding='utf-8', xml_declaration=True, pretty_print=True)
    with open(mytoc, mode='wb') as f:
        f.write(texte)

    # traitement du .opf
    myopf = os.path.join(monepub.chemin, monepub.opf_path)
    tree = etree.parse(myopf, parser)
    # on récupère l'id de l'identifiant unique
    pack = XPath('//opf:package')
    unique = str(pack(tree)[0].get('unique-identifier'))
    # on remplace l'identifiant par le nouveau
    ident = XPath('//dc:identifier[@id="{}"]'.format(unique))
    if len(ident(tree)):
        ident(tree)[0].text = monepub.uid
        # puis on change l'id et le opf:scheme
        ident(tree)[0].set('id', 'BookId')
    pack(tree)[0].set('unique-identifier', 'BookId')
    job.logtat('remplacement de l\'identifiant unique par : ' + monepub.uid)
    ident_scheme = xpath(tree, '//dc:identifier')
    for child in ident_scheme:
        child.set("{http://www.idpf.org/2007/opf}scheme", 'uuid')
    # on supprime les autres identifiants
    autres_ident = XPath('//dc:identifier')
    for id in autres_ident(tree):
        if id.get('id') != 'BookId':
            if (id.get('id') == 'ean') :  # or (id.get('opf:scheme') in ('ISBN', 'isbn')) :
                id.text = id.text[0:13]
            else :
                job.logtat('suppression de l\'identifiant : "' + id.text + '"')
                id.getparent().remove(id)
    # supprimer les dc:rights
    rights = XPath('//dc:rights')
    for right in rights(tree):
        if right.text is not None:
            job.logtat('suppression du dc:rights : "' + str(right.text) + '" ')
            right.getparent().remove(right)
    # supprimer les description contenant code et date de la version
    desc = XPath('//dc:description')
    for com in desc(tree):
        if re.search('Version ([0-9]{1,6})', str(com.text)):
            job.logtat('suppression de la description : "' + str(com.text) + '"')
            com.text = str(com.text).replace(str(com.text), '')
    # supprimer les commentaires
    for elem in tree.iter(tag=etree.Comment):
        if elem.text:
            job.logtat('suppression des commentaires dans le opf : "' + str(elem.text) + '"')
            elem.getparent().remove(elem)
    # réécrire le fichier
    texte = etree.tostring(tree, encoding='utf-8', xml_declaration=True, pretty_print=True)
    with open(myopf, mode='wb') as f:
        f.write(texte)
    opf.parse_opf(myopf)
    return
Exemplo n.º 4
0
def modif_tatoo_html(monepub):
    for identifier in monepub.opf.manifest:
        item = monepub.opf.manifest[identifier]
        path = os.path.join(monepub.chemin, monepub.content_path) + '\\' + item.href
        path = os.path.normpath(urllib.parse.unquote(path))
        if item.href.endswith(('html', 'htm', 'opf')):
            data = fonctions.parse_file(path)
            if data is None:
                job.log('\t Fichier ' + os.path.basename(path) + ' malformé, impossible à parser')
                continue
            try:
                # suppression des commentaires
                for elem in data.iter(tag=etree.Comment):
                    if elem.text:
                        job.logtat('suppression des commentaires html :' + str(elem.text))
                        tail = elem.tail
                        parent = elem.getparent()
                        parent.remove(elem)
                        parent.text = tail
                para = XPath('.//h:p')
                # suppression des paragraphes contenant un mail
                for child in para(data):
                    if child.text is not None and re.search('[@].*[.][a-z]{2,3}', child.text) and len(child.text) < 100 :
                        job.logtat('suppression du paragraphe : ' + str(child.text))
                        child.getparent().remove(child)
                # suppression du blabla
                for child in para(data):
                    children = child.getchildren()
                    if (child.text is not None) and (re.search('filigrane', child.text)) and (re.search('e-book', child.text)):
                        job.logtat('modification de ces paragraphes : "' + str(child.text) + str(children[0].tail) + '" dans ' + os.path.basename(path))
                        child.text = 'Cet e-book contenait un filigrane (watermark) et une identification qui ont été supprimés pour votre agrément'
                        children[0].tail = 'par PersonnaLiseur'
                # suppression des watermark
                waterm = XPath('.//h:img')
                for src in waterm(data):
                    for  value in src.values():
                        if 'base64'  in value:
                            job.logtat('suppression du watermark dans ' + os.path.basename(path))
                            div = src.getparent()
                            div.getparent().remove(div)
                # suppression des body id
                body_id = XPath('.//h:body')
                for body in body_id(data):
                    if body.get('id'):
                        job.logtat('suppression du body id "' + body.get('id') + '" dans ' + os.path.basename(path))
                        body.set('id', '')
            except:
                job.log('html non modifiés')
                return
            with open(path, mode='wb'):
                data.write(path, encoding='utf-8', xml_declaration=True, pretty_print=True)
            # suppression des commentaires hors html
            f = open(path, mode='r', encoding='utf-8')
            texte = f.read(-1)
            if re.search(r"<!--[\d\D]*?-->", texte, re.DOTALL):
                job.logtat('suppression des commentaires hors html dans ' + os.path.basename(path))
                texte = re.sub(r"<!--[\d\D]*?-->", '', texte, re.DOTALL)
            with open(path, mode='w', encoding='utf-8') as f:
                f.write(texte)
    return