Use Tableau PAT for automated refresh

This commit is contained in:
Codex
2026-09-10 14:42:38 +08:00
parent 1d3c6a919c
commit 96bddd1096
4 changed files with 125 additions and 11 deletions
+21 -10
View File
@@ -1,19 +1,30 @@
#!/usr/bin/env bash
set -euo pipefail
# AgentStage runs this hourly. The guard below refreshes once after 12:00 Asia/Shanghai.
# AgentStage runs this hourly. Refresh exactly once during the 12:00 hour.
export TZ=Asia/Shanghai
hour="$(date +%H)"
target_date="$(date -v-1d +%F 2>/dev/null || date -d yesterday +%F)"
if [ "$hour" -lt 12 ]; then exit 0; fi
if [ "$hour" -ne 12 ]; then exit 0; fi
: "${TABLEAU_COOKIE:?Set TABLEAU_COOKIE in the AgentStage runner secret environment}"
base_url="${TABLEAU_CSV_URL:-https://mars.ushareit.org/views/DSPSpend/DSPDailyData.csv}"
tmp_file="$(mktemp)"
trap 'rm -f "$tmp_file"' EXIT
: "${TABLEAU_PAT_NAME:?Set TABLEAU_PAT_NAME in the AgentStage runner secret environment}"
: "${TABLEAU_PAT_SECRET:?Set TABLEAU_PAT_SECRET in the AgentStage runner secret environment}"
pkg_csv="$(python3 -c 'import json; d=json.load(open("campaign_config.json")); print(",".join(sorted({r["pkg"] for r in d if r["status"].lower()=="active"})))')"
query="?:showVizHome=no&dt=${target_date}&pkg=${pkg_csv}&维度1=pkg&维度2=campaign_name&Source=展开&source=bigo,kwai,mintegral"
curl -fsSL --cookie "$TABLEAU_COOKIE" "${base_url}${query}" -o "$tmp_file"
repo_dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
cd "$repo_dir"
runtime_dir="${TABLEAU_HYPER_RUNTIME:-$repo_dir/.runtime/tableau-hyperapi}"
success_marker="$repo_dir/.runtime/last-successful-date"
if [ -f "$success_marker" ] && [ "$(cat "$success_marker")" = "$target_date" ]; then
exit 0
fi
if ! PYTHONPATH="$runtime_dir" python3 -c 'import tableauhyperapi' 2>/dev/null; then
mkdir -p "$runtime_dir"
python3 -m pip install --disable-pip-version-check --target "$runtime_dir" tableauhyperapi
fi
python3 transform_tableau.py "$tmp_file" "$target_date"
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT
raw_file="$tmp_dir/tableau.csv"
PYTHONPATH="$runtime_dir" python3 refresh_tableau.py "$target_date" "$raw_file"
python3 transform_tableau.py "$raw_file" "$target_date"
printf '%s\n' "$target_date" > "$success_marker"