Repository navigation
Expand file tree
/
Copy pathsync_to_github.py
More file actions
240 lines (200 loc) · 8.77 KB
/
Copy pathsync_to_github.py
File metadata and controls
240 lines (200 loc) · 8.77 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
"""
GitHub 自动同步脚本 (容器内版)
==============================
在呆呆面板容器内运行,将 code/ 目录同步到 GitHub。
环境变量:
GITHUB_TOKEN - GitHub PAT
"""
import os
import sys
import json
import subprocess
import sqlite3
import re
from datetime import datetime, timezone, timedelta
# ==== 配置 ====
GITHUB_REPO = "YR4399/wx-mini-scripts"
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "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")
# 容器内路径
PANEL_DIR = "/app/Dumb-Panel"
SCRIPTS_DIR = os.path.join(PANEL_DIR, "scripts/code")
REPO_DIR = os.path.join(PANEL_DIR, "wx-mini-scripts")
CODE_DIR = os.path.join(REPO_DIR, "code")
README_PATH = os.path.join(REPO_DIR, "README.md")
DB_PATH = os.path.join(PANEL_DIR, "daidai.db")
CST = timezone(timedelta(hours=8))
def run(cmd, cwd=None):
"""执行命令"""
env = os.environ.copy()
env["GIT_TERMINAL_PROMPT"] = "0"
# 清除代理
for k in ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]:
env.pop(k, None)
r = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=cwd or REPO_DIR, env=env, timeout=120)
return r.stdout.strip() if r.returncode == 0 else None
def sync_scripts():
"""同步脚本文件"""
print("[1/4] 同步脚本...")
os.makedirs(CODE_DIR, exist_ok=True)
# 获取源目录脚本
remote = set(f for f in os.listdir(SCRIPTS_DIR) if f.endswith(".py"))
local = set(f for f in os.listdir(CODE_DIR) if f.endswith(".py"))
synced = []
for fname in sorted(remote):
src = os.path.join(SCRIPTS_DIR, fname)
dst = os.path.join(CODE_DIR, fname)
with open(src, "r", encoding="utf-8", errors="ignore") as f:
new = f.read()
old = ""
if os.path.exists(dst):
with open(dst, "r", encoding="utf-8", errors="ignore") as f:
old = f.read()
if new != old:
with open(dst, "w", encoding="utf-8") as f:
f.write(new)
synced.append(fname)
print(f" ✅ {fname}")
# 清理多余文件
for fname in local - remote:
os.remove(os.path.join(CODE_DIR, fname))
print(f" 🗑️ {fname}")
print(f" 共 {len(remote)} 个, 更新 {len(synced)} 个")
return synced
def get_task_status():
"""获取任务状态"""
print("[2/4] 获取状态...")
status = {}
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("SELECT name, command, last_run_status, last_run_at FROM tasks WHERE command LIKE '%code/%' AND status=1")
for name, cmd, run_st, run_at in c.fetchall():
m = re.search(r"code/(.+\.py)", cmd)
if not m:
continue
fname = m.group(1)
t = ""
if run_at:
try:
dt = datetime.fromisoformat(run_at[:19])
t = dt.strftime("%m-%d %H:%M")
except:
t = str(run_at)[:16]
status[fname] = {"name": name, "ok": run_st == 0, "time": t}
conn.close()
except Exception as e:
print(f" [WARN] {e}")
print(f" 找到 {len(status)} 个任务")
return status
def get_desc():
"""获取脚本描述"""
desc = {}
for f in os.listdir(CODE_DIR):
if not f.endswith(".py"):
continue
try:
with open(os.path.join(CODE_DIR, f), "r", encoding="utf-8") as fh:
c = fh.read(2000)
m = re.search(r'"""(.*?)"""', c, re.DOTALL)
desc[f] = m.group(1).strip().split("\n")[0][:60] if m else "(无描述)"
except:
desc[f] = "(读取失败)"
return desc
def update_readme(synced, status, desc):
"""更新 README"""
print("[3/4] 更新 README...")
now = datetime.now(CST).strftime("%Y-%m-%d %H:%M")
# 分类
cats = {"sign": [], "task": [], "auto": [], "yili": [], "other": [], "tool": []}
yili_kw = ["伊利", "qq星", "zhenxi", "甄稀", "星韵"]
sign_kw = ["sign", "签到", "lj_sign", "袋鼠", "haishibao", "angel", "小紫"]
tool_kw = ["wx_code_client"]
for f in sorted(desc):
entry = {
"file": f, "desc": desc[f],
"status": "✨" if status.get(f, {}).get("ok") else ("💥" if f in status else "—"),
"time": status.get(f, {}).get("time", "—"),
"updated": datetime.fromtimestamp(os.path.getmtime(os.path.join(CODE_DIR, f))).strftime("%m-%d"),
}
fl = f.lower()
if any(k in fl for k in tool_kw):
cats["tool"].append(entry)
elif any(k in fl for k in yili_kw):
cats["yili"].append(entry)
elif any(k in fl for k in sign_kw):
cats["sign"].append(entry)
elif any(k in fl for k in ["抽奖", "claim", "gift"]):
cats["task"].append(entry)
elif any(k in fl for k in ["jph", "mcyx", "monalisa", "wps", "牛牛", "纷享", "飞鹤", "xmyx"]):
cats["auto"].append(entry)
else:
cats["other"].append(entry)
titles = {"sign": "🎀 签到类", "task": "🎁 任务 & 抽奖类", "auto": "🌸 综合自动化类",
"yili": "🍼 伊利系列", "other": "🏠 其他", "tool": "🔧 工具模块"}
L = []
L += ['<div align="center">', '', '<img src="banner.svg" width="100%" />', '',
'# ✨ 微信小程序自动化脚本合集 ✨', '',
'**「 每天自动签到,把时间留给二次元 」**', '',
'[](https://github.com/YR4399/wx-mini-scripts)',
'[](https://github.com/YR4399/wx-mini-scripts)',
'[](https://github.com/linzixuanzz/Dumb-Panel)',
'', f'> 🕐 最后同步:{now}', f'> 📦 脚本总数:**{len(desc)}** 个', '',
'</div>', '', '---', '']
for k, items in cats.items():
if not items:
continue
L += [f'<div align="center">', '', f'### {titles[k]}', '', '</div>', '',
'| 脚本 | 说明 | 更新 | 运行 | 状态 |',
'|:-----|:-----|:----:|:----:|:----:|']
for s in items:
L.append(f'| `{s["file"]}` | {s["desc"]} | {s["updated"]} | {s["time"]} | {s["status"]} |')
L.append('')
L += ['<div align="center">', '', '| 标记 | 含义 |', '|:----:|:-----|',
'| ✨ | 最近运行成功 |', '| 💥 | 最近运行失败 |', '| — | 无定时任务 |',
'', '</div>', '', '---', '', '## 🛠️ 快速开始', '', '```bash',
'# 克隆仓库', 'git clone https://github.com/YR4399/wx-mini-scripts.git', '',
'# 将脚本放入呆呆面板', 'cp code/*.py /app/Dumb-Panel/scripts/code/', '',
'# 配置环境变量', "export wx_server_url='http://host.docker.internal:18788'",
"export wx_auth='your_api_key'", "export wx_opinion='openid1&openid2'", '```',
'', '---', '', '<div align="center">', '',
'**「 今天的签到,就交给小助手吧~ 」**', '',
'',
'', '</div>']
with open(README_PATH, "w", encoding="utf-8") as f:
f.write("\n".join(L) + "\n")
return sum(len(v) for v in cats.values())
def git_push(synced):
"""提交并推送"""
print("[4/4] 推送...")
run(f'git config user.name "YR4399"')
run(f'git config user.email "yr@users.noreply.github.com"')
run(f'git remote set-url origin https://YR4399:{GITHUB_TOKEN}@github.com/{GITHUB_REPO}.git')
run("git add -A")
if not run("git status --porcelain"):
print(" 无变更")
return True
now = datetime.now(CST).strftime("%m-%d %H:%M")
msg = f"sync: {len(synced)} scripts [{now}]" if synced else f"sync: status update [{now}]"
run(f'git commit -m "{msg}"')
if run("git push origin main"):
print(" ✅ 推送成功!")
run(f'git remote set-url origin https://github.com/{GITHUB_REPO}.git')
return True
print(" ❌ 推送失败")
return False
def main():
print("=" * 40)
print(" GitHub Auto Sync")
print(f" {datetime.now(CST).strftime('%H:%M:%S')}")
print("=" * 40)
synced = sync_scripts()
status = get_task_status()
desc = get_desc()
total = update_readme(synced, status, desc)
ok = git_push(synced)
print(f"\n{'✅' if ok else '⚠️'} 完成: {total} 脚本, 更新 {len(synced)} 个")
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())