Skip to content

Fetch JTWC Products #90

Fetch JTWC Products

Fetch JTWC Products #90

Workflow file for this run

name: Fetch JTWC Products
# 触发条件:每10分钟自动运行,且允许手动触发
on:
schedule:
- cron: '*/10 * * * *' # 每10分钟运行一次
workflow_dispatch: # 允许手动触发
jobs:
fetch-and-commit:
runs-on: ubuntu-latest
steps:
# 检出代码仓库
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # 确保获取所有分支历史
# 切换到jtwc分支,如果不存在则创建
- name: Switch to jtwc branch or create it
run: |
if git ls-remote --heads origin jtwc | grep -q jtwc; then
if git rev-parse --verify jtwc 2>/dev/null; then
git checkout jtwc
else
git checkout -b jtwc origin/jtwc
fi
git pull origin jtwc
else
git checkout --orphan jtwc
git rm -rf . 2>/dev/null || true
fi
# 从natyphoon获取活跃台风系统,构造JTWC链接并下载
- name: Fetch active systems and download JTWC products
run: |
# 下载natyphoon目录页
curl -o natyphoon_index.html "https://www.natyphoon.top/atcf/temp/?C=M;O=A"
# 提取 FN (文件名) 和 LM (最后修改时间) 两个JS数组
FN_RAW=$(sed -n 's/^const FN = \[\(.*\)\];$/\1/p' natyphoon_index.html)
LM_RAW=$(sed -n 's/^const LM = \[\(.*\)\];$/\1/p' natyphoon_index.html)
rm -f natyphoon_index.html
# 转为换行分隔的列表(去掉引号)
echo "$FN_RAW" | tr ',' '\n' | sed 's/"//g' > /tmp/fn_list.txt
echo "$LM_RAW" | tr ',' '\n' | sed 's/"//g' > /tmp/lm_list.txt
# 计算2天前的日期 (格式: YYYY/MM/DD)
CUTOFF_DATE=$(date -u -d '2 days ago' +'%Y/%m/%d')
# 配对文件名和日期,筛选最近2天的
rm -f /tmp/active_typhoons.txt
touch /tmp/active_typhoons.txt
paste /tmp/fn_list.txt /tmp/lm_list.txt > /tmp/paired_list.txt
while IFS=$'\t' read -r fname lm_date; do
CLEAN_DATE=$(echo "$lm_date" | sed 's/^ *//')
DATE_PART=$(echo "$CLEAN_DATE" | cut -d' ' -f1)
if [[ -n "$DATE_PART" ]] && [[ "$DATE_PART" > "$CUTOFF_DATE" || "$DATE_PART" == "$CUTOFF_DATE" ]]; then
echo -e "${fname}\t${CLEAN_DATE}" >> /tmp/active_typhoons.txt
fi
done < /tmp/paired_list.txt
ACTIVE_COUNT=$(wc -l < /tmp/active_typhoons.txt | tr -d ' ')
if [ "$ACTIVE_COUNT" -eq 0 ]; then
echo "No active systems found in last 2 days."
exit 0
fi
# 临时目录存放下载的原始文件
mkdir -p /tmp/jtwc_downloads
rm -f /tmp/download_map.txt
touch /tmp/download_map.txt
# 对每个活跃的台风系统,构造JTWC URL并尝试下载
while IFS=$'\t' read -r datfile sys_timestamp; do
REGION=$(echo "$datfile" | sed -E 's/^b([a-z]{2})[0-9]+\.dat$/\1/')
NUMYEAR=$(echo "$datfile" | sed -E 's/^b[a-z]{2}(.*)\.dat$/\1/')
YEAR="${NUMYEAR: -4}"
NUM="${NUMYEAR%${YEAR}}"
SHORT_YEAR="${YEAR: -2}"
for TYPE in web prog fix; do
URL="https://www.metoc.navy.mil/jtwc/products/${REGION}${NUM}${SHORT_YEAR}${TYPE}.txt"
TMPFILE="/tmp/jtwc_downloads/${REGION}${NUM}${SHORT_YEAR}${TYPE}.txt"
HTTP_CODE=$(curl -o "$TMPFILE" -w '%{http_code}' -s "$URL")
if [ "$HTTP_CODE" = "200" ]; then
FIRST_CHAR=$(head -c 1 "$TMPFILE")
if [ "$FIRST_CHAR" = "<" ]; then
echo "ERROR: $URL returned HTML error page"
rm -f "$TMPFILE"
else
PREFIX6=$(head -c 6 "$TMPFILE" | tr '[:upper:]' '[:lower:]')
echo -e "${PREFIX6}\t${TMPFILE}\t${sys_timestamp}\t${URL}" >> /tmp/download_map.txt
fi
else
if [ "$HTTP_CODE" != "404" ]; then
echo "ERROR: $URL returned HTTP $HTTP_CODE"
fi
rm -f "$TMPFILE"
fi
done
done < /tmp/active_typhoons.txt
# 按 prefix6 分组,同一 prefix6 保留 natyphoon 更新时间最新的
sort -t$'\t' -k1,1 -k3,3r /tmp/download_map.txt > /tmp/download_map_sorted.txt
PREV_PREFIX=""
while IFS=$'\t' read -r prefix6 tmpfile sys_ts url; do
if [ "$prefix6" = "$PREV_PREFIX" ]; then
echo "CONFLICT: '${prefix6}' keeping newer (ts=$sys_ts), skipped $url"
rm -f "$tmpfile"
else
cp "$tmpfile" "${prefix6}.txt"
rm -f "$tmpfile"
fi
PREV_PREFIX="$prefix6"
done < /tmp/download_map_sorted.txt
# 清理临时文件
rm -rf /tmp/active_typhoons.txt /tmp/fn_list.txt /tmp/lm_list.txt /tmp/paired_list.txt /tmp/download_map.txt /tmp/download_map_sorted.txt /tmp/jtwc_downloads
# 配置Git用户信息
- name: Configure Git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
# 提交更改
- name: Commit and push changes
run: |
git add *.txt 2>/dev/null || true
if git diff --cached --quiet; then
echo "No changes to commit"
else
FILE_COUNT=$(git diff --cached --name-only | wc -l | tr -d ' ')
git commit -m "Update JTWC products ($FILE_COUNT files): $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
git push origin jtwc
fi