Beispiel #1
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