Update app.py
Browse files
app.py
CHANGED
|
@@ -58,25 +58,69 @@ class Texte(db.Model):
|
|
| 58 |
|
| 59 |
# --- Fonctions utilitaires ---
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def sanitize_html(html_content):
|
| 62 |
-
"""Assainit le contenu HTML
|
| 63 |
allowed_tags = [
|
| 64 |
'a', 'abbr', 'acronym', 'b', 'blockquote', 'br', 'code', 'div', 'em',
|
| 65 |
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'li', 'ol', 'p',
|
| 66 |
'pre', 'span', 'strong', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'ul'
|
| 67 |
]
|
|
|
|
| 68 |
allowed_attributes = {
|
| 69 |
'*': ['class', 'id', 'style'],
|
| 70 |
'a': ['href', 'rel', 'target', 'title'],
|
| 71 |
'img': ['alt', 'src', 'width', 'height'],
|
| 72 |
'table': ['border', 'cellpadding', 'cellspacing']
|
| 73 |
}
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
soup = BeautifulSoup(cleaned_html, 'html.parser')
|
| 76 |
for tag in soup.find_all():
|
| 77 |
-
|
|
|
|
| 78 |
tag.decompose()
|
| 79 |
return str(soup)
|
|
|
|
|
|
|
| 80 |
def generate_content_from_youtube(youtube_url, prompt, model_name):
|
| 81 |
"""Génère du contenu à partir d'une vidéo YouTube en utilisant l'API Gemini."""
|
| 82 |
try:
|
|
|
|
| 58 |
|
| 59 |
# --- Fonctions utilitaires ---
|
| 60 |
|
| 61 |
+
# Nécessite d'installer cssutils: pip install cssutils
|
| 62 |
+
try:
|
| 63 |
+
from bleach.css_sanitizer import CSSSanitizer
|
| 64 |
+
except ImportError:
|
| 65 |
+
print("Attention : cssutils n'est pas installé. L'assainissement CSS ne sera pas appliqué.")
|
| 66 |
+
print("Installez-le avec : pip install cssutils")
|
| 67 |
+
CSSSanitizer = None # Pour éviter une erreur si l'import échoue
|
| 68 |
+
|
| 69 |
+
from bleach import clean
|
| 70 |
+
from bs4 import BeautifulSoup
|
| 71 |
+
|
| 72 |
def sanitize_html(html_content):
|
| 73 |
+
"""Assainit le contenu HTML en autorisant certains styles CSS via CSSSanitizer."""
|
| 74 |
allowed_tags = [
|
| 75 |
'a', 'abbr', 'acronym', 'b', 'blockquote', 'br', 'code', 'div', 'em',
|
| 76 |
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'li', 'ol', 'p',
|
| 77 |
'pre', 'span', 'strong', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'ul'
|
| 78 |
]
|
| 79 |
+
# On garde 'style' ici
|
| 80 |
allowed_attributes = {
|
| 81 |
'*': ['class', 'id', 'style'],
|
| 82 |
'a': ['href', 'rel', 'target', 'title'],
|
| 83 |
'img': ['alt', 'src', 'width', 'height'],
|
| 84 |
'table': ['border', 'cellpadding', 'cellspacing']
|
| 85 |
}
|
| 86 |
+
|
| 87 |
+
# Configurez ici les propriétés CSS que vous souhaitez autoriser.
|
| 88 |
+
# SOYEZ RESTRICTIF ! N'autorisez que ce qui est absolument nécessaire.
|
| 89 |
+
# Exemples : 'color', 'background-color', 'font-weight', 'text-align', etc.
|
| 90 |
+
# Évitez les propriétés comme 'position', 'display', 'float', 'behavior', etc.
|
| 91 |
+
allowed_css_properties = [
|
| 92 |
+
'color', 'background-color', 'font-weight', 'font-style',
|
| 93 |
+
'text-align', 'text-decoration',
|
| 94 |
+
'padding', 'padding-left', 'padding-right', 'padding-top', 'padding-bottom',
|
| 95 |
+
'margin', 'margin-left', 'margin-right', 'margin-top', 'margin-bottom',
|
| 96 |
+
'border', 'border-color', 'border-style', 'border-width', # Soyez prudent avec les bordures
|
| 97 |
+
'list-style-type'
|
| 98 |
+
# Ajoutez d'autres propriétés sûres si nécessaire
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
# Créez l'instance de CSSSanitizer SEULEMENT si cssutils est disponible
|
| 102 |
+
css_sanitizer = None
|
| 103 |
+
if CSSSanitizer:
|
| 104 |
+
css_sanitizer = CSSSanitizer(allowed_css_properties=allowed_css_properties)
|
| 105 |
+
|
| 106 |
+
# Passez l'assainisseur CSS à clean
|
| 107 |
+
cleaned_html = clean(
|
| 108 |
+
html_content,
|
| 109 |
+
tags=allowed_tags,
|
| 110 |
+
attributes=allowed_attributes,
|
| 111 |
+
css_sanitizer=css_sanitizer, # Utilise l'assainisseur CSS configuré
|
| 112 |
+
strip=True
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Optionnel : Nettoyer les balises potentiellement vides
|
| 116 |
soup = BeautifulSoup(cleaned_html, 'html.parser')
|
| 117 |
for tag in soup.find_all():
|
| 118 |
+
is_empty_tag = not tag.contents and not tag.get_text(strip=True)
|
| 119 |
+
if is_empty_tag and tag.name not in ['br', 'hr', 'img']:
|
| 120 |
tag.decompose()
|
| 121 |
return str(soup)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
def generate_content_from_youtube(youtube_url, prompt, model_name):
|
| 125 |
"""Génère du contenu à partir d'une vidéo YouTube en utilisant l'API Gemini."""
|
| 126 |
try:
|