-
Notifications
You must be signed in to change notification settings - Fork 1
218 lines (189 loc) · 9.14 KB
/
ScriptSync.yml
File metadata and controls
218 lines (189 loc) · 9.14 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
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
212
213
214
215
216
217
218
name: 更新或添加脚本和文件夹
on:
schedule:
- cron: '0 8 * * *' # 每天 UTC 时间 8:00 运行
workflow_dispatch: # 允许手动触发
jobs:
update-scripts:
runs-on: ubuntu-latest
steps:
- name: 检出仓库
uses: actions/checkout@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: 设置 Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: 安装依赖
run: |
python -m pip install --upgrade pip
pip install requests
- name: 更新脚本和文件夹
run: |
import os
import requests
import hashlib
import base64
import traceback
from typing import Dict, List, Optional
def validate_github_url(url: str) -> bool:
"""
验证GitHub URL是否有效
"""
print(f"验证URL: {url}")
if not url:
print("URL为空")
return False
if not url.startswith(('https://raw.githubusercontent.com', 'https://github.com')):
print("不是有效的GitHub URL")
return False
return True
def get_file_content(url: str) -> Optional[bytes]:
"""
获取文件内容,带有详细的错误处理
"""
try:
response = requests.get(url, timeout=30)
if response.status_code == 404:
print(f"文件不存在: {url}")
return None
response.raise_for_status()
return response.content
except requests.exceptions.RequestException as e:
print(f"请求失败: {str(e)}")
if hasattr(e, 'response') and e.response is not None:
print(f"状态码: {e.response.status_code}")
print(f"响应内容: {e.response.text[:200]}...")
return None
def update_file(local_path: str, remote_url: str) -> bool:
"""
更新单个文件,带有完整的错误处理和日志
"""
try:
print(f"\n开始更新文件: {local_path}")
print(f"远程URL: {remote_url}")
# 获取远程文件内容
remote_content = get_file_content(remote_url)
if remote_content is None:
return False
# 确保目录存在
directory = os.path.dirname(local_path)
if directory:
os.makedirs(directory, exist_ok=True)
print(f"确保目录存在: {directory}")
# 检查文件是否需要更新
if os.path.exists(local_path):
with open(local_path, 'rb') as f:
local_content = f.read()
if hashlib.md5(local_content).hexdigest() == hashlib.md5(remote_content).hexdigest():
print(f"文件无变化,跳过更新: {local_path}")
return False
# 写入新内容
with open(local_path, 'wb') as f:
f.write(remote_content)
print(f"文件更新成功: {local_path}")
return True
except IOError as e:
print(f"IO错误 - {local_path}: {str(e)}")
print(f"错误堆栈:\n{traceback.format_exc()}")
return False
except Exception as e:
print(f"意外错误 - {local_path}: {str(e)}")
print(f"错误堆栈:\n{traceback.format_exc()}")
return False
def update_folder(local_path: str, remote_url: str) -> bool:
"""
更新整个文件夹,带有详细的错误处理
"""
try:
print(f"\n开始更新文件夹: {local_path}")
print(f"远程URL: {remote_url}")
# 解析 GitHub URL
parts = remote_url.split('/')
if len(parts) < 8:
print(f"无效的GitHub文件夹URL: {remote_url}")
return False
owner = parts[3]
repo = parts[4]
branch = parts[6]
path = '/'.join(parts[7:])
# 构建 GitHub API URL
api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}?ref={branch}"
print(f"API URL: {api_url}")
# 获取文件列表
response = requests.get(api_url, timeout=30)
if response.status_code == 404:
print(f"文件夹不存在: {remote_url}")
return False
response.raise_for_status()
files = response.json()
# 创建本地文件夹
os.makedirs(local_path, exist_ok=True)
print(f"确保本地文件夹存在: {local_path}")
updated = False
for file in files:
if file['type'] == 'file':
file_path = os.path.join(local_path, file['name'])
if update_file(file_path, file['download_url']):
updated = True
elif file['type'] == 'dir':
subdir_path = os.path.join(local_path, file['name'])
if update_folder(subdir_path, file['url']):
updated = True
return updated
except requests.exceptions.RequestException as e:
print(f"请求错误 - {local_path}: {str(e)}")
if hasattr(e, 'response') and e.response is not None:
print(f"状态码: {e.response.status_code}")
print(f"响应内容: {e.response.text[:200]}...")
return False
except Exception as e:
print(f"意外错误 - {local_path}: {str(e)}")
print(f"错误堆栈:\n{traceback.format_exc()}")
return False
# 要更新的文件列表
files_to_update = [
{"local_path": "ablesci.py", "remote_url": "https://raw.githubusercontent.com/chenlunTian/ablesciSign/main/ablesci.py"},
{"local_path": "Quark/Quark.py", "remote_url": "https://raw.githubusercontent.com/BNDou/Auto_Check_In/main/checkIn_Quark.py"},
{"local_path": "Quark/utils", "remote_url": "https://github.com/BNDou/Auto_Check_In/tree/main/utils"},
{"local_path": "wps.js", "remote_url": "https://raw.githubusercontent.com/wf021325/qx/main/task/wps.js"},
{"local_path": "nodeseek.py", "remote_url": "https://raw.githubusercontent.com/wh1te3zzz/checkin/refs/heads/main/nodeseek.py"},
{"local_path": "nodeloc.py", "remote_url": "https://raw.githubusercontent.com/wh1te3zzz/checkin/refs/heads/main/nodeloc.py"},
]
# 主执行逻辑
print("开始更新文件...")
updated = False
for item in files_to_update:
if not validate_github_url(item['remote_url']):
print(f"跳过无效URL: {item['remote_url']}\n")
continue
try:
if item['remote_url'].startswith('https://github.com') and '/tree/' in item['remote_url']:
if update_folder(item['local_path'], item['remote_url']):
updated = True
else:
if update_file(item['local_path'], item['remote_url']):
updated = True
except Exception as e:
print(f"处理 {item['local_path']} 时发生错误: {str(e)}")
print(f"错误堆栈:\n{traceback.format_exc()}")
# 设置输出
if updated:
print("\n::set-output name=files_updated::true")
print("有文件被更新")
else:
print("\n::set-output name=files_updated::false")
print("没有文件需要更新")
id: update-scripts
shell: python
- name: 提交并推送更改
if: steps.update-scripts.outputs.files_updated == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "更新脚本文件和文件夹"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}