SPB Git

Repository Public

qub-token-safari.sh 5.3 KB 111 lines shellscript

1#!/bin/zsh2# =============================================================================3#                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : Vrai-Prix8# File             : scripts/qub-token-safari.sh9# Module           : scripts10# Repository       : https://git.spboucher.ai/vrai-prix11# Created          : 2026-10-2012# Last Modified    : 2026-10-2013#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# Jeton Bearer QUB (Cognito id-token, valide 1 h) obtenu en pilotant Safari par41# AppleScript — solution de repli (ou de secours) à scripts/qub-token.mjs, qui42# passe par Scrapfly et échoue parfois sur l'anti-bot de connect.qub.ca.43#44# S'exécute SUR LE LAPTOP (Safari + cliclick). Imprime le jeton sur stdout, les45# étapes sur stderr. Identifiants : QUB_EMAIL / QUB_PASSWORD dans l'environnement46# ou dans ~/.config/vrai-prix/qub.env (chmod 600).47#48#   scripts/qub-token-safari.sh                 # → jeton49#   scripts/ingest-via-safari.sh [--since=…]    # jeton + ingestion sur le nœud50set -u51PAGE='https://www.journaldequebec.com/argent/immobilier/transactions-immobilieres'52[[ -f ~/.config/vrai-prix/qub.env ]] && source ~/.config/vrai-prix/qub.env53: "${QUB_EMAIL:?QUB_EMAIL manquant}" "${QUB_PASSWORD:?QUB_PASSWORD manquant}"54command -v cliclick >/dev/null || { echo "cliclick requis (brew install cliclick)" >&2; exit 2; }5556log() { print -u2 -- "[qub-token-safari] $*"; }57js() { local code=${1//\"/\\\"}; osascript -e "tell application \"Safari\" to do JavaScript \"$code\" in front document" 2>/dev/null; }58url() { osascript -e 'tell application "Safari" to return URL of front document' 2>/dev/null; }5960# jeton via /api/checklogin (XHR synchrone, même origine) — vide si non connecté61grab() {62  js '(function(){try{var x=new XMLHttpRequest();x.open("GET","/api/checklogin",false);x.send();var j=JSON.parse(x.responseText);return j.userToken||""}catch(e){return ""}})()'63}6465osascript -e "tell application \"Safari\"66  activate67  make new document with properties {URL:\"$PAGE\"}68end tell" >/dev/null69sleep 67071tok=$(grab)72if [[ -n "$tok" ]]; then log "session déjà connectée"; print -- "$tok"; exit 0; fi7374log "non connecté : ouverture de connect.qub.ca"75js 'Array.from(document.querySelectorAll("a")).filter(e=>/Me connecter|Se connecter/i.test(e.innerText))[0].click()' >/dev/null76sleep 77778# écran « Sélectionner un compte » → formulaire classique79if js 'document.body.innerText.includes("Changer de compte")' | grep -q true; then80  js 'Array.from(document.querySelectorAll("a")).filter(e=>/Changer de compte/i.test(e.innerText))[0].click()' >/dev/null81  sleep 582fi8384if ! js 'String(!!document.querySelector("input[type=email]"))' | grep -q true; then85  log "formulaire de connexion introuvable ($(url))"; exit 386fi8788# remplissage (setter natif + événements React), puis fermeture du remplissage89# automatique Touch ID qui s'ouvre au focus, puis VRAI clic souris sur le bouton90# (un click() JavaScript n'est pas honoré par le formulaire connect.qub.ca)91js "(function(){function set(el,v){Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set.call(el,v);el.dispatchEvent(new Event('input',{bubbles:true}));el.dispatchEvent(new Event('change',{bubbles:true}))}set(document.querySelector('input[type=email]'),'$QUB_EMAIL');set(document.querySelector('input[type=password]'),'$QUB_PASSWORD');return 'ok'})()" >/dev/null92sleep 193osascript -e 'tell application "System Events" to key code 53' # Échap : popup Touch ID94sleep 195pos=$(js '(function(){var b=document.querySelector("button[type=submit]");var r=b.getBoundingClientRect();return [Math.round(r.left+r.width/2),Math.round(r.top+r.height/2),window.outerHeight-window.innerHeight].join(",")})()')96bounds=$(osascript -e 'tell application "Safari" to get bounds of front window')97x0=${${(s:, :)bounds}[1]}; y0=${${(s:, :)bounds}[2]}98bx=${${(s:,:)pos}[1]}; by=${${(s:,:)pos}[2]}; chrome=${${(s:,:)pos}[3]}99osascript -e 'tell application "Safari" to activate'; sleep 0.5100cliclick c:$((x0 + bx)),$((y0 + chrome + by))101log "formulaire soumis, attente de l'OAuth…"102103for i in {1..10}; do104  sleep 3105  if [[ "$(url)" == *journaldequebec.com* ]]; then106    tok=$(grab)107    if [[ -n "$tok" ]]; then log "jeton obtenu (essai $i)"; print -- "$tok"; exit 0; fi108  fi109done110log "échec : $(url)"; exit 4111
Files main
Menu

Theme