-
Notifications
You must be signed in to change notification settings - Fork 12
154 lines (132 loc) · 4.92 KB
/
sync-neodb-data.yml
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
name: Sync NeoDB Data
on:
# schedule:
# every monday
# - cron: "0 9 * * 1"
# watch:
# types: [started]
workflow_dispatch:
repository_dispatch:
types: [sync-neodb]
jobs:
douban:
name: Sync NeoDB Data
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
# 检查是否安装了 JQ
- name: Check JQ
run: |
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Installing..."
sudo apt-get update
sudo apt-get install -y jq
else
echo "jq is already installed."
fi
# 把当前目录保存到环境变量中
echo "WORK_DIR=$(pwd)" >> $GITHUB_ENV
# 获取本地现有文件中最新的 UUID
- name: Get Local Current UUID
run: |
CURRENT_UUID() {
jq -r '.data[0].item.uuid' assets/data/neodb/movie.json
}
echo "CURRENT_UUID=$(CURRENT_UUID)" >> $GITHUB_ENV
# 获取远程最新的 UUID
- name: Get NeoDB Latest UUID and Pages
run: |
RESPONSE=$(curl -s -X 'GET' \
'https://neodb.social/api/me/shelf/complete?page=1' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${{ secrets.NEODB_ACCESS_TOKEN }}')
REMOTE_UUID=$(echo "$RESPONSE" | jq -r '.data[0].item.uuid')
REMOTE_PAGES=$(echo "$RESPONSE" | jq -r '.pages')
echo "REMOTE_UUID=$REMOTE_UUID" >> $GITHUB_ENV
echo "REMOTE_PAGES=$REMOTE_PAGES" >> $GITHUB_ENV
# 对比本地的 UUID 和远程 UUID,相等就跳过,不相等就下载新数据
- name: UUID Compare
run: |
if [ ${{ env.REMOTE_UUID }} != ${{ env.CURRENT_UUID }} ]; then
echo "Variables are not equal. Running the next steps."
else
echo "Variables are equal. Skipping the next steps."
exit 0
fi
# 下载所有数据
- name: Get All NeoDB
if: ${{ env.REMOTE_UUID != env.CURRENT_UUID }}
run: |
pages=${{ env.REMOTE_PAGES }}
# 个人使用,新建 WorkDIR ,排除 vercel.json 和 package.json 等
mkdir neodb
cd neodb
# 下载数据
for ((i=1; i<=$pages; i++)); do
url="https://neodb.social/api/me/shelf/complete?page=$i"
filename="movie$i.json"
# 下载文件并保存为对应的文件名
curl -X 'GET' "$url" \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${{ secrets.NEODB_ACCESS_TOKEN }}' > "$filename"
done
# 把所有数据合并成一个文件
jq -c -s '{data: map(.data[]) | unique | sort_by(.created_time) | reverse, pages: map(.pages)[0], count: map(.count)[0]}' *.json > movie.json
# 更新 NeoDB 数据
cp -f movie.json ${{ env.WORK_DIR }}/assets/data/neodb/
- name: Download NeoDB Cover
if: ${{ env.REMOTE_UUID != env.CURRENT_UUID }}
run: |
# 检查 movie 目录是否存在,如果不存在则创建
if [ ! -d "movie" ]; then
mkdir movie
fi
# 读取本地的 movie.json 文件内容
json=$(cat assets/data/neodb/movie.json)
# 提取图片 URL
image_urls=$(echo "$json" | jq -r '.data[].item.cover_image_url')
# 遍历图片 URL 并下载图片
for url in $image_urls; do
filename=$(basename "$url")
filepath="assets/images/neodb/cover/$filename"
# 检查文件是否已存在
if [ -f "$filepath" ]; then
echo "Skipping $filename - File already exists"
else
# 使用 curl 命令下载图片
curl -o "$filepath" "$url"
echo "Downloaded $filename"
echo "REMOTE_UUID=''" >> $GITHUB_ENV
fi
done
# 把修改后的数据提交到 GitHub 仓库
- name: Git Add and Commit
if: ${{ env.REMOTE_UUID != env.CURRENT_UUID }}
uses: EndBug/add-and-commit@v9
with:
message: 'chore(data): update neodb data'
committer_name: 'github-actions[bot]'
committer_email: 'github-actions[bot]@users.noreply.github.com'
add: |
'./assets/data/neodb'
'./assets/images/neodb/cover'
# # 调用另外的 GitHub Actions 构建 Hugo
- name: Build Hugo and Deploy
if: ${{ env.REMOTE_UUID != env.CURRENT_UUID }}
uses: peter-evans/repository-dispatch@v3
with:
event-type: build-hugo
# 同步新海报到 s3
- name: Sync Cover to static s3
if: ${{ env.REMOTE_UUID != env.CURRENT_UUID }}
run: |
git clone https://github.com/eallion/static tmp
cp -rn assets/images/neodb/cover/* tmp/images/neodb/
cd tmp
git config user.name "eallion"
git config user.email "eallions@gmail.com"
git remote set-url --add --push origin https://eallion:${{ secrets.PICLIST_GITHUB_TOKEN }}@github.com/eallion/static.git
git add .
git commit -m "chore: sync cover"
git push