Repository Public
1#!/bin/bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : FB Ka Automation8# File : reels/fb_reel.sh9# Module : reels10# Repository : https://git.spboucher.ai/fb-ka-automation11# Created : 2026-12-0112# Last Modified : 2026-12-0113#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940# Publie un Reel (vidéo + musique Minimal Tech + légende) sur la page Groupe KA41set -u42SITE="$1"43WIN="${2:-13939}"44VID="/tmp/ka_reels/$SITE.mp4"45CAP="/tmp/ka_reels/captions/$SITE.txt"46[ -f "$VID" ] || { echo "no video $VID"; exit 1; }47[ -f "$CAP" ] || { echo "no caption $CAP"; exit 1; }4849js() {50 osascript <<EOF51tell application "Safari"52 do JavaScript "$1" in tab 1 of window id $WIN53end tell54EOF55}5657click_label() { # clic sur bouton par aria-label exact dans les dialogues58 js "59(function(){60 var btn=null;61 document.querySelectorAll('div[role=dialog] [role=button]').forEach(function(b){62 if((b.getAttribute('aria-label')||'')==='$1') btn=b;63 });64 if(!btn) return 'NO_BTN';65 if(btn.getAttribute('aria-disabled')==='true') return 'DISABLED';66 ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});67 return 'clicked';68})();"69}7071# 0) accueil page72osascript -e "tell application \"Safari\" to set URL of tab 1 of window id $WIN to \"https://www.facebook.com/profile.php?id=61593422723708\""73sleep 127475# 1) bouton Reel (poll)76R=""77for i in 1 2 3 4 5 6; do78 R=$(js "79(function(){80 var btn=null;81 document.querySelectorAll('[role=button]').forEach(function(b){82 if((b.innerText||'').trim()==='Reel' && !btn) btn=b;83 });84 if(!btn) return 'NO_REEL_BTN';85 ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});86 return 'clicked';87})();")88 echo "reel btn (essai $i): $R"89 [ "$R" = "clicked" ] && break90 sleep 591done92[ "$R" = "clicked" ] || exit 193sleep 89495# 2) importer la vidéo (poll)96R=""97for i in 1 2 3 4 5 6 7; do98 R=$(click_label "Importer une vidéo pour le reel")99 echo "import (essai $i): $R"100 [ "$R" = "clicked" ] && break101 sleep 4102done103[ "$R" = "clicked" ] || exit 1104sleep 3105SHEETS=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)106echo "sheets=$SHEETS"107[ "$SHEETS" = "1" ] || { echo "no file panel"; exit 1; }108osascript <<EOF109tell application "Safari" to activate110delay 0.5111tell application "System Events"112 tell process "Safari"113 keystroke "g" using {command down, shift down}114 delay 1.2115 keystroke "$VID"116 delay 0.8117 key code 36118 delay 1.5119 key code 36120 end tell121end tell122EOF123echo "vidéo sélectionnée"124sleep 14125126# 3) Suivant (étape 1)127R=""128for i in 1 2 3 4 5; do129 R=$(click_label "Suivant")130 echo "suivant1 (essai $i): $R"131 [ "$R" = "clicked" ] && break132 sleep 5133done134[ "$R" = "clicked" ] || exit 1135sleep 5136137# 4) Musique : rechercher et sélectionner Minimal Tech138R=$(js "139(function(){140 var btn=null;141 document.querySelectorAll('div[role=dialog] [role=button]').forEach(function(b){142 if((b.innerText||'').trim()==='Musique' && !btn) btn=b;143 });144 if(!btn) return 'NO_MUSIQUE';145 ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});146 return 'clicked';147})();")148echo "musique: $R"149[ "$R" = "clicked" ] || exit 1150sleep 4151R=$(js "152(function(){153 var inp=null;154 document.querySelectorAll('div[role=dialog] input').forEach(function(i){155 if((i.getAttribute('placeholder')||'')==='Rechercher du contenu audio') inp=i;156 });157 if(!inp) return 'NO_INPUT';158 inp.focus();159 var setter=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set;160 setter.call(inp,'electronic minimal');161 inp.dispatchEvent(new Event('input',{bubbles:true}));162 return 'searched';163})();")164echo "recherche: $R"165sleep 5166R=$(js "167(function(){168 var row=null;169 document.querySelectorAll('div[role=dialog] [role=button]').forEach(function(e){170 var t=(e.innerText||'').trim();171 if(!row && t.indexOf('Minimal Tech')===0 && t.length<80) row=e;172 });173 if(!row) return 'NO_ROW';174 var btn=null;175 row.parentElement.querySelectorAll('[role=button]').forEach(function(b){176 if(!b.getAttribute('aria-label')) btn=b;177 });178 if(!btn) btn=row;179 ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});180 return 'selected';181})();")182echo "sélection piste: $R"183sleep 4184R=$(js "185(function(){186 var ok=false;187 document.querySelectorAll('div[role=dialog] [aria-label]').forEach(function(e){188 if((e.getAttribute('aria-label')||'')==='Supprimer l’audio') ok=true;189 });190 return ok?'AUDIO_OK':'AUDIO_ABSENT';191})();")192echo "vérif audio: $R"193[ "$R" = "AUDIO_OK" ] || { echo "musique non appliquée"; exit 1; }194click_label "Terminé" >/dev/null195sleep 3196197# 5) Suivant (étape 2) puis description198R=""199for i in 1 2 3 4 5; do200 R=$(click_label "Suivant")201 echo "suivant2 (essai $i): $R"202 [ "$R" = "clicked" ] && break203 sleep 5204done205[ "$R" = "clicked" ] || exit 1206sleep 5207pbcopy < "$CAP"208R=$(js "209(function(){210 var ed=document.querySelector('div[role=dialog] [contenteditable=true]');211 if(!ed) return 'NO_ED';212 ed.focus();213 return 'focused';214})();")215echo "description: $R"216[ "$R" = "focused" ] || exit 1217osascript -e 'tell application "Safari" to activate' -e 'delay 0.8' -e 'tell application "System Events" to keystroke "v" using command down'218sleep 3219LEN=$(js "220(function(){221 var ed=document.querySelector('div[role=dialog] [contenteditable=true]');222 return ''+(ed?ed.innerText.length:-1);223})();")224echo "caption len=$LEN"225[ "$LEN" -gt 30 ] 2>/dev/null || { echo "caption paste failed"; exit 1; }226227# 6) Publier et attendre la fermeture228R=$(click_label "Publier")229echo "publier: $R"230[ "$R" = "clicked" ] || exit 1231CLOSED=0232for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do233 sleep 8234 N=$(js "document.querySelectorAll('div[role=dialog]').length + '';")235 echo "fermeture (essai $i): dialogs=$N"236 if [ "$N" = "0" ]; then CLOSED=1; break; fi237done238[ "$CLOSED" = "1" ] || exit 1239echo "OK: reel $SITE publié"240