-
Notifications
You must be signed in to change notification settings - Fork 2
172 lines (149 loc) · 6.99 KB
/
fetch_code.yml
File metadata and controls
172 lines (149 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
name: Fetch JTWC Products
# 触发条件:每10分钟自动运行,且允许手动触发
on:
schedule:
- cron: '0 */1 * * *' # 每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."
else
# 临时目录存放下载的原始文件
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/jtwc_downloads /tmp/download_map.txt /tmp/download_map_sorted.txt
fi
# 清理natyphoon临时文件
rm -rf /tmp/active_typhoons.txt /tmp/fn_list.txt /tmp/lm_list.txt /tmp/paired_list.txt
# 直接下载 ABPW10 公报(不依赖natyphoon)
- name: Fetch ABPW10 product
run: |
TMPFILE="/tmp/abpwweb.txt"
HTTP_CODE=$(curl -o "$TMPFILE" -w '%{http_code}' -s "https://www.metoc.navy.mil/jtwc/products/abpwweb.txt")
if [ "$HTTP_CODE" = "200" ]; then
FIRST_CHAR=$(head -c 1 "$TMPFILE")
if [ "$FIRST_CHAR" = "<" ]; then
echo "ERROR: abpwweb.txt returned HTML error page"
rm -f "$TMPFILE"
else
PREFIX6=$(head -c 6 "$TMPFILE" | tr '[:upper:]' '[:lower:]')
FINAL_NAME="${PREFIX6}.txt"
if [ -f "$FINAL_NAME" ]; then
echo "CONFLICT: '${PREFIX6}' already exists from natyphoon fetch, overwriting with abpwweb.txt"
fi
cp "$TMPFILE" "$FINAL_NAME"
rm -f "$TMPFILE"
fi
else
if [ "$HTTP_CODE" != "404" ]; then
echo "ERROR: abpwweb.txt returned HTTP $HTTP_CODE"
fi
rm -f "$TMPFILE"
fi
# 配置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