#!/bin/bash # Daily watch on sciCORE's open-positions page. # Sends a Telegram message ONLY when a new line mentioning "software" appears. # Other changes are logged silently so the snapshot stays current. # Credentials come from .env: TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID set -uo pipefail REPO="/Users/polux/Projetos/linkme" URL="${SCICORE_URL:-https://scicore.unibas.ch/about-scicore/open-positions/}" # override for testing WANTED="software" # only changes mentioning this word are sent STATE="$REPO/results/.scicore-watch" SNAP="$STATE/snapshot.txt" FAILS="$STATE/consecutive-failures" LOG="$STATE/watch.log" UA="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" mkdir -p "$STATE" # shellcheck disable=SC1091 # accepts either TELEGRAM_BOT_TOKEN= or the shorter TELEGRAM_BOT= TELEGRAM_BOT_TOKEN=$(grep -E '^TELEGRAM_BOT(_TOKEN)?=' "$REPO/.env" 2>/dev/null | head -1 | cut -d= -f2-) TELEGRAM_CHAT_ID=$(grep -E '^TELEGRAM_CHAT_ID=' "$REPO/.env" 2>/dev/null | cut -d= -f2-) log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $*" >> "$LOG"; } notify() { local text="$1" if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then log "NOTIFY SKIPPED (no telegram creds in .env): $text" return 1 fi local code code=$(curl -s -o "$STATE/last-telegram-response.json" -w '%{http_code}' \ "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ --data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" \ --data-urlencode "text=${text}" \ --data-urlencode "disable_web_page_preview=true") log "telegram http $code" [ "$code" = "200" ] } HTML=$(curl -s --max-time 30 -A "$UA" "$URL") if [ -z "$HTML" ]; then n=$(( $(cat "$FAILS" 2>/dev/null || echo 0) + 1 )) echo "$n" > "$FAILS" log "FETCH FAILED (streak $n)" [ "$n" -eq 3 ] && notify "⚠️ sciCORE watcher: 3 days in a row the page failed to load. Check the script. $URL" exit 0 fi echo 0 > "$FAILS" CURRENT=$(printf '%s' "$HTML" | SCICORE_BASE="$URL" python3 -c ' import sys, re, html, os from urllib.parse import urljoin BASE = os.environ.get("SCICORE_BASE", "") h = sys.stdin.read() m = re.search(r"