| from flask import Flask, render_template, request, redirect, url_for, flash, jsonify |
| from flask_sqlalchemy import SQLAlchemy |
| from sqlalchemy import func |
| from bleach import clean |
| from bs4 import BeautifulSoup |
| import os |
| |
| from google import genai |
| from google.genai import types |
|
|
| cle = os.environ.get("TOKEN") |
| |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") |
|
|
|
|
| app = Flask(__name__) |
| app.config['SQLALCHEMY_DATABASE_URI'] = cle |
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| app.config['SECRET_KEY'] = 'une_cle_secrete_tres_longue' |
|
|
| db = SQLAlchemy(app) |
|
|
| |
|
|
| class Matiere(db.Model): |
| """Modèle représentant une matière.""" |
| __tablename__ = 'matiere' |
| id = db.Column(db.Integer, primary_key=True) |
| nom = db.Column(db.String(64), unique=True, nullable=False) |
| sous_categories = db.relationship('SousCategorie', backref='matiere', lazy='dynamic', cascade="all, delete-orphan") |
|
|
| def __repr__(self): |
| return f"<Matiere {self.nom}>" |
|
|
| class SousCategorie(db.Model): |
| """Modèle représentant une sous-catégorie.""" |
| __tablename__ = 'sous_categorie' |
| id = db.Column(db.Integer, primary_key=True) |
| nom = db.Column(db.String(64), nullable=False) |
| matiere_id = db.Column(db.Integer, db.ForeignKey('matiere.id'), nullable=False) |
| textes = db.relationship('Texte', backref='sous_categorie', lazy='dynamic', cascade="all, delete-orphan") |
| __table_args__ = (db.UniqueConstraint('nom', 'matiere_id', name='_nom_matiere_uc'), {}) |
|
|
| def __repr__(self): |
| return f"<SousCategorie {self.nom}>" |
|
|
| class Texte(db.Model): |
| """Modèle représentant un texte.""" |
| __tablename__ = 'texte' |
| id = db.Column(db.Integer, primary_key=True) |
| titre = db.Column(db.String(128), nullable=False) |
| contenu = db.Column(db.Text, nullable=False) |
| sous_categorie_id = db.Column(db.Integer, db.ForeignKey('sous_categorie.id'), nullable=False) |
|
|
| def __repr__(self): |
| return f"<Texte {self.titre}>" |
|
|
|
|
| |
|
|
| |
| try: |
| from bleach.css_sanitizer import CSSSanitizer |
| except ImportError: |
| print("Attention : cssutils n'est pas installé. L'assainissement CSS ne sera pas appliqué.") |
| print("Installez-le avec : pip install cssutils") |
| CSSSanitizer = None |
|
|
| from bleach import clean |
| from bs4 import BeautifulSoup |
|
|
| def sanitize_html(html_content): |
| """Assainit le contenu HTML en autorisant certains styles CSS via CSSSanitizer.""" |
| allowed_tags = [ |
| 'a', 'abbr', 'acronym', 'b', 'blockquote', 'br', 'code', 'div', 'em', |
| 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'li', 'ol', 'p', |
| 'pre', 'span', 'strong', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'ul' |
| ] |
| |
| allowed_attributes = { |
| '*': ['class', 'id', 'style'], |
| 'a': ['href', 'rel', 'target', 'title'], |
| 'img': ['alt', 'src', 'width', 'height'], |
| 'table': ['border', 'cellpadding', 'cellspacing'] |
| } |
|
|
| |
| |
| |
| |
| allowed_css_properties = [ |
| 'color', 'background-color', 'font-weight', 'font-style', |
| 'text-align', 'text-decoration', |
| 'padding', 'padding-left', 'padding-right', 'padding-top', 'padding-bottom', |
| 'margin', 'margin-left', 'margin-right', 'margin-top', 'margin-bottom', |
| 'border', 'border-color', 'border-style', 'border-width', |
| 'list-style-type' |
| |
| ] |
|
|
| |
| css_sanitizer = None |
| if CSSSanitizer: |
| css_sanitizer = CSSSanitizer(allowed_css_properties=allowed_css_properties) |
|
|
| |
| cleaned_html = clean( |
| html_content, |
| tags=allowed_tags, |
| attributes=allowed_attributes, |
| css_sanitizer=css_sanitizer, |
| strip=True |
| ) |
|
|
| |
| soup = BeautifulSoup(cleaned_html, 'html.parser') |
| for tag in soup.find_all(): |
| is_empty_tag = not tag.contents and not tag.get_text(strip=True) |
| if is_empty_tag and tag.name not in ['br', 'hr', 'img']: |
| tag.decompose() |
| return str(soup) |
|
|
| |
| def generate_content_from_youtube(youtube_url, prompt, model_name): |
| """Génère du contenu à partir d'une vidéo YouTube en utilisant l'API Gemini.""" |
| try: |
| |
| |
| client = genai.Client(api_key=GEMINI_API_KEY) |
| |
| |
| contents = types.Content( |
| parts=[ |
| types.Part(text=prompt), |
| types.Part( |
| file_data=types.FileData(file_uri=youtube_url) |
| ) |
| ] |
| ) |
| |
| |
| response = client.models.generate_content( |
| model=model_name, |
| contents=contents |
| ) |
| |
| return {"success": True, "text": response.text} |
| except Exception as e: |
| return {"success": False, "error": str(e)} |
|
|
| |
|
|
| @app.route('/', methods=['GET', 'POST']) |
| def gere(): |
| if request.method == 'POST': |
| |
| action = request.form.get('action') |
|
|
| if action == 'add_matiere': |
| nom_matiere = request.form.get('nom_matiere') |
| if nom_matiere: |
| nouvelle_matiere = Matiere(nom=nom_matiere) |
| db.session.add(nouvelle_matiere) |
| try: |
| db.session.commit() |
| flash(f"Matière '{nom_matiere}' ajoutée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur lors de l'ajout de la matière : {e}", 'danger') |
|
|
| elif action == 'add_sous_categorie': |
| nom_sous_categorie = request.form.get('nom_sous_categorie') |
| matiere_id = request.form.get('matiere_id') |
| if nom_sous_categorie and matiere_id: |
| nouvelle_sous_categorie = SousCategorie(nom=nom_sous_categorie, matiere_id=matiere_id) |
| db.session.add(nouvelle_sous_categorie) |
| try: |
| db.session.commit() |
| flash(f"Sous-catégorie '{nom_sous_categorie}' ajoutée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
| elif action == 'add_texte': |
| titre_texte = request.form.get('titre_texte') |
| contenu_texte = request.form.get('contenu_texte') |
| sous_categorie_id = request.form.get('sous_categorie_id') |
|
|
| if titre_texte and contenu_texte and sous_categorie_id: |
| |
| contenu_texte_sanitized = sanitize_html(contenu_texte) |
| nouveau_texte = Texte(titre=titre_texte, contenu=contenu_texte_sanitized, sous_categorie_id=sous_categorie_id) |
| db.session.add(nouveau_texte) |
| try: |
| db.session.commit() |
| flash(f"Texte '{titre_texte}' ajouté avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur lors de l'ajout : {e}", 'danger') |
|
|
|
|
| elif action.startswith('edit_matiere_'): |
| matiere_id = int(action.split('_')[-1]) |
| matiere = Matiere.query.get_or_404(matiere_id) |
| matiere.nom = request.form.get(f'edit_nom_matiere_{matiere_id}') |
| try: |
| db.session.commit() |
| flash(f"Matière '{matiere.nom}' modifiée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur lors de la modification : {e}", 'danger') |
|
|
| elif action.startswith('edit_sous_categorie_'): |
| sous_categorie_id = int(action.split('_')[-1]) |
| sous_categorie = SousCategorie.query.get_or_404(sous_categorie_id) |
| sous_categorie.nom = request.form.get(f'edit_nom_sous_categorie_{sous_categorie_id}') |
| sous_categorie.matiere_id = request.form.get(f'edit_matiere_id_{sous_categorie_id}') |
| try: |
| db.session.commit() |
| flash(f"Sous-catégorie '{sous_categorie.nom}' modifiée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
|
|
| elif action.startswith('edit_texte_'): |
| texte_id = int(action.split('_')[-1]) |
| texte = Texte.query.get_or_404(texte_id) |
| texte.titre = request.form.get(f'edit_titre_texte_{texte_id}') |
| contenu = request.form.get(f'edit_contenu_texte_{texte_id}') |
| if contenu: |
| texte.contenu = sanitize_html(contenu) |
| texte.sous_categorie_id = request.form.get(f'edit_sous_categorie_id_{texte_id}') |
|
|
| try: |
| db.session.commit() |
| flash(f"Texte '{texte.titre}' modifié avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
|
|
| elif action.startswith('delete_matiere_'): |
| matiere_id = int(action.split('_')[-1]) |
| matiere = Matiere.query.get_or_404(matiere_id) |
| db.session.delete(matiere) |
| try: |
| db.session.commit() |
| flash(f"Matière '{matiere.nom}' supprimée avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
| elif action.startswith('delete_sous_categorie_'): |
| sous_categorie_id = int(action.split('_')[-1]) |
| sous_categorie = SousCategorie.query.get_or_404(sous_categorie_id) |
| db.session.delete(sous_categorie) |
| try: |
| db.session.commit() |
| flash(f"Sous-catégorie '{sous_categorie.nom}' supprimée.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}", 'danger') |
|
|
|
|
| elif action.startswith('delete_texte_'): |
| texte_id = int(action.split('_')[-1]) |
| texte = Texte.query.get_or_404(texte_id) |
| db.session.delete(texte) |
| try: |
| db.session.commit() |
| flash(f"Texte '{texte.titre}' supprimé avec succès.", 'success') |
| except Exception as e: |
| db.session.rollback() |
| flash(f"Erreur : {e}",'danger') |
|
|
| return redirect(url_for('gere')) |
|
|
| |
| matieres = Matiere.query.order_by(func.lower(Matiere.nom)).all() |
| sous_categories = SousCategorie.query.order_by(func.lower(SousCategorie.nom)).all() |
| textes = Texte.query.order_by(Texte.titre).all() |
| return render_template('gere.html', matieres=matieres, sous_categories=sous_categories, textes=textes) |
|
|
| @app.route('/generate-content', methods=['POST']) |
| def generate_content(): |
| """Route API pour générer du contenu avec Gemini.""" |
| data = request.json |
| youtube_url = data.get('youtube_url') |
| prompt = data.get('prompt') |
| model = data.get('model') |
| |
| if not youtube_url or not prompt or not model: |
| return jsonify({'success': False, 'error': 'Paramètres manquants'}) |
| |
| result = generate_content_from_youtube(youtube_url, prompt, model) |
| return jsonify(result) |
|
|
| if __name__ == '__main__': |
| with app.app_context(): |
| db.create_all() |
| app.run(debug=True) |