Repository Public
1#!/bin/bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : FB Ka Automation8# File : li_post.sh9# Repository : https://git.spboucher.ai/fb-ka-automation10# Created : 2026-12-0111# Last Modified : 2026-12-0112#13# Author : Simon-Pierre Boucher14# Position : Professor of Finance15# Institution : Université du Québec en Outaouais (UQO)16# Organization : SP Boucher17# Website : https://spboucher.ai18# Git : https://git.spboucher.ai19#20# Preferred Contact: contact@spboucher.ai21# Technical Contact: dev@spboucher.ai22# Security/Admin : admin@spboucher.ai23#24# Copyright (c) 202625# Simon-Pierre Boucher26# All rights reserved where applicable. This file is governed by the27# applicable project license, copyright notices and relevant law.28# Existing third-party rights and notices remain unaffected.29#30# License : None specified (no license file in this repository)31#32# Do not embed credentials, private keys, access tokens,33# certificates or confidential personal information.34#35# =============================================================================36# END OF SP BOUCHER SOURCE HEADER37# =============================================================================3839# Publie un post (image + texte) sur la page LinkedIn Groupe KA via Safari40set -u41TXT="$1"42IMG="$2"43WIN="${3:-13939}"4445js() {46 osascript <<EOF47tell application "Safari"48 do JavaScript "$1" in tab 1 of window id $WIN49end tell50EOF51}5253# 0) dashboard admin54osascript -e "tell application \"Safari\" to set URL of tab 1 of window id $WIN to \"https://www.linkedin.com/company/143347918/admin/dashboard/\""55sleep 105657# 1) Commencer un post (poll)58R=""59for i in 1 2 3 4 5 6; do60 R=$(js "61(function(){62 var btn=null;63 document.querySelectorAll('button, a, [role=button]').forEach(function(b){64 var t=(b.innerText||'').trim();65 if((/Commencer un post/.test(t)||/Créer un post/.test(t)) && !btn) btn=b;66 });67 if(!btn) return 'NO_BTN';68 btn.click();69 return 'clicked';70})();")71 echo "composer (essai $i): $R"72 [ "$R" = "clicked" ] && break73 sleep 474done75[ "$R" = "clicked" ] || exit 176sleep 67778# 2) Ajouter un média79R=""80for i in 1 2 3 4 5; do81 R=$(js "82(function(){83 var m=document.querySelector('.artdeco-modal, div[role=dialog]');84 if(!m) return 'NO_MODAL';85 var btn=null;86 m.querySelectorAll('button').forEach(function(b){87 if((b.getAttribute('aria-label')||'')==='Ajouter un média') btn=b;88 });89 if(!btn) return 'NO_MEDIA_BTN';90 btn.click();91 return 'clicked';92})();")93 echo "media (essai $i): $R"94 [ "$R" = "clicked" ] && break95 sleep 396done97[ "$R" = "clicked" ] || exit 198sleep 499# ouvrir le panneau fichier : bouton « Importer… » sinon input.click(), poll du sheet100SHEETS=0101for i in 1 2 3 4 5; do102 R=$(js "103(function(){104 var m=document.querySelector('.artdeco-modal, div[role=dialog]');105 if(!m) return 'NO_MODAL';106 var btn=null;107 m.querySelectorAll('button').forEach(function(b){108 if((b.innerText||'').trim().indexOf('Importer depuis votre ordinateur')===0) btn=b;109 });110 if(btn){btn.click(); return 'import_btn';}111 var f=null;112 m.querySelectorAll('input[type=file]').forEach(function(x){f=x;});113 if(f){f.click(); return 'input_click';}114 return 'NO_TRIGGER';115})();")116 echo "ouverture panneau (essai $i): $R"117 sleep 3118 SHEETS=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)119 echo "sheets=$SHEETS"120 [ "$SHEETS" = "1" ] && break121 sleep 2122done123[ "$SHEETS" = "1" ] || { echo "pas de panneau fichier"; exit 1; }124osascript <<EOF125tell application "Safari" to activate126delay 0.8127tell application "System Events"128 tell process "Safari"129 keystroke "g" using {command down, shift down}130 delay 1.5131 keystroke "$IMG"132 delay 1.0133 key code 36134 delay 2.0135 key code 36136 end tell137end tell138EOF139sleep 4140S2=$(osascript -e 'tell application "System Events" to tell process "Safari" to count of sheets of window 1' 2>/dev/null || echo 0)141echo "sheet après sélection: $S2"142[ "$S2" = "0" ] || { echo "panneau toujours ouvert"; exit 1; }143echo "image choisie"144sleep 6145146# 2b) Suivant (modal média) — attendre que l'upload l'active (jusqu'à ~60 s)147R=""148for i in 1 2 3 4 5 6 7 8 9 10 11 12; do149 R=$(js "150(function(){151 var mods=document.querySelectorAll('.artdeco-modal, div[role=dialog]');152 var btn=null;153 mods.forEach(function(m){154 m.querySelectorAll('button').forEach(function(b){155 if((b.innerText||'').trim()==='Suivant' && !b.disabled) btn=b;156 });157 });158 if(!btn) return 'OFF';159 btn.click();160 return 'suivant';161})();")162 echo "suivant média (essai $i): $R"163 [ "$R" = "suivant" ] && break164 sleep 5165done166[ "$R" = "suivant" ] || { echo "upload jamais prêt"; exit 1; }167sleep 4168169# 3) texte170pbcopy < "$TXT"171R=$(js "172(function(){173 var m=document.querySelector('.artdeco-modal, div[role=dialog]');174 if(!m) return 'NO_MODAL';175 var ed=m.querySelector('[contenteditable=true]');176 if(!ed) return 'NO_ED';177 ed.focus();178 return 'focused';179})();")180echo "editor: $R"181[ "$R" = "focused" ] || exit 1182LEN=0183for i in 1 2 3; do184 osascript -e 'tell application "Safari" to activate' -e 'delay 0.8' -e 'tell application "System Events" to keystroke "v" using command down'185 sleep 3186 LEN=$(js "187(function(){188 var m=document.querySelector('.artdeco-modal, div[role=dialog]');189 var ed=m?m.querySelector('[contenteditable=true]'):null;190 return ''+(ed?ed.innerText.length:-1);191})();")192 echo "texte len=$LEN (essai $i)"193 [ "$LEN" -gt 100 ] 2>/dev/null && break194 sleep 2195done196[ "$LEN" -gt 100 ] 2>/dev/null || { echo "paste failed"; exit 1; }197sleep 2198199# 4) Publier200R=""201for i in 1 2 3 4 5; do202 R=$(js "203(function(){204 var m=document.querySelector('.artdeco-modal, div[role=dialog]');205 if(!m) return 'NO_MODAL';206 var btn=null;207 m.querySelectorAll('button').forEach(function(b){208 if((b.innerText||'').trim()==='Publier' && !b.disabled) btn=b;209 });210 if(!btn) return 'NO_BTN';211 btn.click();212 return 'publie';213})();")214 echo "publier (essai $i): $R"215 [ "$R" = "publie" ] && break216 sleep 4217done218[ "$R" = "publie" ] || exit 1219CLOSED=0220for i in 1 2 3 4 5 6 7 8 9 10; do221 sleep 6222 N=$(js "document.querySelector('.artdeco-modal, div[role=dialog]') ? 'OPEN' : 'CLOSED';")223 echo "fermeture ($i): $N"224 if [ "$N" = "CLOSED" ]; then CLOSED=1; break; fi225done226[ "$CLOSED" = "1" ] || exit 1227echo "OK: $(basename "$TXT") + $(basename "$IMG") publié sur LinkedIn"228