SPB Git

Repository Public

fb_post_multi.sh 7.6 KB 238 lines shellscript

1#!/bin/bash2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : FB Ka Automation8# File             : posts/fb_post_multi.sh9# Module           : posts10# 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 texte + TOUTES les images d'un dossier (carrousel) sur la page FB Groupe KA41# Usage: fb_post_multi.sh <fichier.txt> <dossier_images> <window_id>42set -u43FILE="$1"44DIR="$2"45WIN="${3:-18972}"46NIMG=$(ls "$DIR"/*.png 2>/dev/null | wc -l | tr -d ' ')4748js() {49  osascript <<EOF50tell application "Safari"51  do JavaScript "$1" in tab 1 of window id $WIN52end tell53EOF54}5556clickbtn() {57  js "58(function(){59  var dlgs=document.querySelectorAll('div[role=dialog]');60  if(!dlgs.length) return 'NO_DIALOG';61  var btn=null;62  dlgs.forEach(function(dlg){63    dlg.querySelectorAll('[role=button]').forEach(function(b){64      var al=b.getAttribute('aria-label')||'';65      if(al==='$1') btn=b;66    });67  });68  if(!btn) return 'NO_BTN';69  if(btn.getAttribute('aria-disabled')==='true') return 'DISABLED';70  ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});71  return 'clicked';72})();"73}7475# Étape 0 : recharger l'accueil de la page76osascript -e "tell application \"Safari\" to set URL of tab 1 of window id $WIN to \"https://www.facebook.com/profile.php?id=61593422723708\""77sleep 107879# Étape 1 : ouvrir le composeur80R1="NOT_FOUND"81for i in 1 2 3 4 5 6 7 8 9; do82  R1=$(js "83(function(){84  var target=null;85  document.querySelectorAll('span,div').forEach(function(e){86    var t=(e.innerText||'').trim();87    if(!target && e.children.length===0 && (t.indexOf('Présentez votre marque')===0 || t.indexOf('Que voulez-vous dire')===0)) target=e;88  });89  if(!target) return 'NOT_FOUND';90  var btn=target.closest('[role=button]');91  if(!btn) return 'NO_BTN_ANCESTOR';92  ['mousedown','mouseup','click'].forEach(function(ev){93    btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));94  });95  return 'opened';96})();")97  echo "step1 (essai $i): $R1"98  [ "$R1" = "opened" ] && break99  sleep 5100done101[ "$R1" = "opened" ] || exit 1102103# Étape 2 : coller le texte104pbcopy < "$FILE"105R2a="NO_DIALOG"106for i in 1 2 3 4 5 6 7 8; do107  R2a=$(js "108(function(){109  var dlg=document.querySelector('div[role=dialog]');110  if(!dlg) return 'NO_DIALOG';111  var box=dlg.querySelector('div[contenteditable=true][role=textbox]');112  if(!box) return 'NO_BOX';113  box.focus();114  return 'focused';115})();")116  echo "step2a (essai $i): $R2a"117  [ "$R2a" = "focused" ] && break118  sleep 3119done120[ "$R2a" = "focused" ] || exit 1121sleep 1122# Collage 100 % JS (ClipboardEvent synthétique) — insensible au focus/clavier.123# Lexical traite le paste en asynchrone : vérifier la longueur APRÈS un délai.124B64=$(base64 -i "$FILE" | tr -d '\n')125LEN=0126for i in 1 2 3; do127  js "128(function(){129  var dlg=document.querySelector('div[role=dialog]');130  if(!dlg) return 'NO_DIALOG';131  var box=dlg.querySelector('div[contenteditable=true][role=textbox]');132  if(!box) return 'NO_BOX';133  box.focus();134  var txt=decodeURIComponent(escape(atob('$B64')));135  var dt=new DataTransfer();136  dt.setData('text/plain', txt);137  box.dispatchEvent(new ClipboardEvent('paste',{clipboardData:dt,bubbles:true,cancelable:true}));138  return 'pasted';139})();" >/dev/null140  sleep 3141  R2b=$(js "142(function(){143  var dlg=document.querySelector('div[role=dialog]');144  if(!dlg) return 'NO_DIALOG';145  var box=dlg.querySelector('div[contenteditable=true][role=textbox]');146  return 'len:'+(box?box.innerText.length:-1);147})();")148  echo "step2b (essai $i): $R2b"149  LEN=${R2b#len:}150  [ "$LEN" -gt 100 ] 2>/dev/null && break151  sleep 2152done153[ "$LEN" -gt 100 ] 2>/dev/null || { echo "paste failed"; exit 1; }154sleep 2155156# Étape 3 : joindre les images (bouton Photo/Vidéo -> panneau -> dossier -> Cmd+A)157R3=$(clickbtn "Photo/Vidéo")158echo "step3 (Photo/Vidéo): $R3"159[ "$R3" = "clicked" ] || exit 1160sleep 3161SHEETS=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)162if [ "$SHEETS" = "0" ]; then163  R3b=$(js "164(function(){165  var btn=null;166  document.querySelectorAll('div[role=dialog] [role=button]').forEach(function(b){167    var t=(b.innerText||'').trim();168    if(/Ajouter des photos|Ajouter photos|photos\\/vidéos/i.test(t) && !btn) btn=b;169  });170  if(!btn) return 'NO_DROPZONE';171  ['mousedown','mouseup','click'].forEach(function(ev){btn.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));});172  return 'dropzone_clicked';173})();")174  echo "step3b: $R3b"175  sleep 3176  SHEETS=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)177fi178echo "sheets=$SHEETS"179[ "$SHEETS" = "1" ] || { echo "no file panel"; exit 1; }180osascript <<EOF181tell application "Safari" to activate182delay 0.5183tell application "System Events"184  tell process "Safari"185    keystroke "g" using {command down, shift down}186    delay 1.2187    keystroke "$DIR"188    delay 0.8189    key code 36190    delay 2.0191    keystroke "a" using command down192    delay 0.8193    key code 36194  end tell195end tell196EOF197echo "sélection de $NIMG images, upload en cours…"198199# attendre que les aperçus soient chargés (>= NIMG images dans le composeur)200NPREV=0201for i in 1 2 3 4 5 6 7 8 9 10 11 12; do202  sleep 6203  NPREV=$(js "204(function(){205  var dlg=document.querySelector('div[role=dialog]');206  if(!dlg) return 0;207  return dlg.querySelectorAll('img').length;208})();")209  NPREV=${NPREV%%.*}   # AppleScript renvoie « 7.0 »210  echo "aperçus (essai $i): $NPREV"211  [ "$NPREV" -ge "$NIMG" ] 2>/dev/null && break212done213[ "$NPREV" -ge "$NIMG" ] 2>/dev/null || { echo "aperçus incomplets ($NPREV/$NIMG)"; exit 1; }214215# Étape 4 : Suivant puis Publier216R4=$(clickbtn "Suivant")217echo "step4 (Suivant): $R4"218if [ "$R4" = "clicked" ]; then sleep 8; fi219R5="NO_BTN"220for i in 1 2 3 4 5 6; do221  R5=$(clickbtn "Publier")222  echo "step5 (Publier, essai $i): $R5"223  [ "$R5" = "clicked" ] && break224  sleep 4225done226[ "$R5" = "clicked" ] || exit 1227228# Étape 6 : attendre la fermeture (5 uploads = lent, jusqu'à ~2,5 min)229R6="STILL_OPEN"230for i in $(seq 1 18); do231  sleep 9232  R6=$(js "document.querySelector('div[role=dialog]') ? 'STILL_OPEN' : 'CLOSED';")233  echo "step6 (essai $i): $R6"234  [ "$R6" = "CLOSED" ] && break235done236[ "$R6" = "CLOSED" ] || exit 1237echo "OK: $(basename "$FILE") + $NIMG images publié"238
Files main
Menu

Theme