-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear.sh
211 lines (170 loc) · 6.07 KB
/
clear.sh
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
set -e
DATA_DIR=/data
AUTO_CLEAR_THRESHOLD=${AUTO_CLEAR_THRESHOLD:-10}
retry_command() {
# 重试次数和最大重试次数
retries=0
max_retries=10
local cmd="$1"
local success=false
local output=""
while ! $success && [ $retries -lt $max_retries ]; do
output=$(eval "$cmd" 2>&1)
if [ $? -eq 0 ]; then
success=true
else
retries=$(($retries + 1))
echo "#Failed to execute command \"$(echo "$cmd" | awk '{print $1}')\", retrying in 1 seconds (retry $retries of $max_retries)..." >&2
sleep 1
fi
done
if $success; then
echo "$output"
return 0
else
echo "#Failed to execute command after $max_retries retries: $cmd" >&2
echo "#Command output: $output" >&2
return 1
fi
}
get_Header() {
response=$(curl --connect-timeout 5 -m 5 -s -H "Content-Type: application/json" \
-d '{"grant_type":"refresh_token", "refresh_token":"'$refresh_token'"}' \
https://api.aliyundrive.com/v2/account/token)
access_token=$(echo "$response" | sed -n 's/.*"access_token":"\([^"]*\).*/\1/p')
HEADER="Authorization: Bearer $access_token"
if [ -z "$HEADER" ]; then
echo "获取access token失败" >&2
return 1
fi
response="$(curl --connect-timeout 5 -m 5 -s -H "$HEADER" -H "Content-Type: application/json" -X POST -d '{}' "https://user.aliyundrive.com/v2/user/get")"
lagacy_drive_id=$(echo "$response" | sed -n 's/.*"default_drive_id":"\([^"]*\).*/\1/p')
drive_id=$(echo "$response" | sed -n 's/.*"resource_drive_id":"\([^"]*\).*/\1/p')
if [ -z "$drive_id" ]; then
drive_id=$lagacy_drive_id
fi
if [ "$folder_type"x = "b"x ]; then
drive_id=$lagacy_drive_id
fi
if [ -z "$drive_id" ]; then
echo "获取drive_id失败" >&2
return 1
fi
echo "HEADER=\"$HEADER\""
echo "drive_id=\"$drive_id\""
return 0
}
get_rawList() {
waittime=10
if [ -n "$1" ]; then
waittime="$1"
fi
_res=$(curl --connect-timeout 5 -m 5 -s -H "$HEADER" -H "Content-Type: application/json" -X POST -d '{"drive_id": "'$drive_id'","parent_file_id": "'$file_id'"}' "https://api.aliyundrive.com/adrive/v2/file/list")
# 过滤掉最近的文件文件
# 获取当前时间的 Unix 时间戳
current_time=$(date +%s)
# 定义时间间隔(30分钟)的秒数
max_file_age_seconds=$((AUTO_CLEAR_THRESHOLD * 60))
# 使用 jq 解析和筛选 JSON 数据
_res=$(echo "$_res" | jq -c --argjson now "$current_time" --argjson interval "$max_file_age_seconds" '.items |= map(select(($now - ( .created_at | sub("\\.[0-9]+Z$"; "Z") | fromdateiso8601)) > $interval))')
if [ ! $? -eq 0 ] || [ -z "$(echo "$_res" | grep "items")" ]; then
echo "获取文件列表失败:folder_id=$file_id,drive_id=$drive_id" >&2
return 1
fi
echo "$_res"
#简单规避小雅转存后还没来得及获取直链就被删除的问题,降低发生概率
sleep "$waittime"
return 0
}
get_Path() {
_path="$(curl --connect-timeout 5 -m 5 -s -H "$HEADER" -H "Content-Type: application/json" -X POST -d "{\"drive_id\": \"$drive_id\", \"file_id\": \"$file_id\"}" "https://api.aliyundrive.com/adrive/v1/file/get_path" | grep -o "\"name\":\"[^\"]*\"" | cut -d':' -f2- | tr -d '"' | tr '\n' '/' | awk -F'/' '{for(i=NF-1;i>0;i--){printf("/%s",$i)}; printf("%s\n",$NF)}')"
if [ -z "$_path" ]; then
return 1
fi
echo "$_path"
return 0
}
get_List() {
_res=$raw_list
#echo "$_res" | tr '{' '\n' | grep -v "folder" | grep -o "\"file_id\":\"[^\"]*\"" | cut -d':' -f2- | tr -d '"'
echo "$_res" | tr '{' '\n' | grep -o "\"file_id\":\"[^\"]*\"" | cut -d':' -f2- | tr -d '"'
return 0
}
delete_File() {
_file_id=$1
_name="$(echo "$raw_list" | grep -o "\"name\":\"[^\"]*\"" | cut -d':' -f2- | tr -d '"' | grep -n . | grep "^$(echo "$raw_list" | grep -o "\"file_id\":\"[^\"]*\"" | cut -d':' -f2- | tr -d '"' | grep -n . | grep "$_file_id" | awk -F: '{print $1}'):" | awk -F: '{print $2}')"
_res=$(curl --connect-timeout 5 -m 5 -s -H "$HEADER" -H "Content-Type: application/json" -X POST -d '{
"requests": [
{
"body": {
"drive_id": "'$drive_id'",
"file_id": "'$_file_id'"
},
"headers": {
"Content-Type": "application/json"
},
"id": "'$_file_id'",
"method": "POST",
"url": "/file/delete"
}
],
"resource": "file"
}' "https://api.aliyundrive.com/v3/batch" | grep "\"status\":204")
if [ -z "$_res" ]; then
return 1
fi
drive_root="资源盘"
if [ "$folder_type"x = "b"x ]; then
drive_root="备份盘"
fi
echo "彻底删除文件:/$drive_root$path/$_name"
return 0
}
_clear_aliyun() {
#eval "$(retry_command "get_Header")"
raw_list=$(retry_command "get_rawList")
path=$(retry_command "get_Path")
_list="$(get_List)"
echo "$_list" | sed '/^$/d' | while read line; do
retry_command "delete_File \"$line\""
done
return "$(echo "$_list" | sed '/^$/d' | wc -l)"
}
clear_aliyun() {
eval "$(retry_command "get_Header")"
raw_list=$(retry_command "get_rawList 0")
_list="$(get_List)"
if [ -z "$(echo "$_list" | sed '/^$/d')" ]; then
return 0
fi
echo -e "\n[$(date '+%Y/%m/%d %H:%M:%S')]开始清理小雅$XIAOYA_NAME转存"
_res=1
_filenum=0
while [ ! $_res -eq 0 ]; do
_clear_aliyun
_res=$?
_filenum=$(($_filenum + $_res))
done
echo "本次共清理小雅$XIAOYA_NAME转存文件$_filenum个"
}
init_para() {
XIAOYA_NAME="alist"
refresh_token="$(cat "${DATA_DIR}/mytoken.txt" | head -n1)"
file_id=$(cat "${DATA_DIR}/temp_transfer_folder_id.txt")
folder_type=$(cat "${DATA_DIR}/folder_type.txt")
if [ -z "$refresh_token" ]; then
echo "未找到refresh_token"
return 1
fi
if [ -z "$file_id" ]; then
echo "未找到file_id"
return 1
fi
if [ -z "$folder_type" ]; then
echo "未找到folder_type"
return 1
fi
}
init_para
clear_aliyun