1+ # Requires -RunAsAdministrator
2+ param (
3+ [switch ]$UseProxy ,
4+ [string ]$ProxyHost = " 127.0.0.1:7890" ,
5+ [switch ]$SkipConfirm
6+ )
7+ $ErrorActionPreference = " Stop"
8+
9+ # ---------- 代理开关 ----------
10+ function Use-ProxyIfNeeded {
11+ param ([scriptblock ]$Script )
12+ if ($UseProxy ) {
13+ $oldHttp = [Environment ]::GetEnvironmentVariable(" HTTP_PROXY" )
14+ $oldHttps = [Environment ]::GetEnvironmentVariable(" HTTPS_PROXY" )
15+ [Environment ]::SetEnvironmentVariable(" HTTP_PROXY" , " http://$ProxyHost " , " Process" )
16+ [Environment ]::SetEnvironmentVariable(" HTTPS_PROXY" , " http://$ProxyHost " , " Process" )
17+ try { & $Script }
18+ finally {
19+ [Environment ]::SetEnvironmentVariable(" HTTP_PROXY" , $oldHttp , " Process" )
20+ [Environment ]::SetEnvironmentVariable(" HTTPS_PROXY" , $oldHttps , " Process" )
21+ }
22+ } else { & $Script }
23+ }
24+
25+ # ---------- 工具检测 ----------
26+ function Test-Cmd { param ($cmd ); $null -ne (Get-Command $cmd - ErrorAction SilentlyContinue) }
27+
28+ # ---------- 用户确认 ----------
29+ if (-not $SkipConfirm ) {
30+ Write-Host " 警告:此脚本仅适用于 Windows 10/11 64 位"
31+ Write-Host " 建议使用Windows Terminal终端"
32+ Write-Host " 默认执行命令.\drpys.ps1"
33+ Write-Host " 下载失败可以指定旁路由代理执行命令.\drpys.ps1 -UseProxy -ProxyHost " 旁路由/ clash:7890 " "
34+ Write-Host " 警告:此脚本仅适用于 Windows 10/11 64 位"
35+ $confirm = Read-Host " 您是否理解并同意继续?(y/n) 默认(y)"
36+ if ($confirm -eq " n" ) { exit 1 }
37+ }
38+
39+ # ---------- 安装 Node ----------
40+ Use-ProxyIfNeeded - Script {
41+ if (Test-Cmd " node" ) {
42+ $nodeVer = (node - v) -replace ' ^v' , ' ' -split ' \.' | ForEach-Object { [int ]$_ }
43+ if ($nodeVer [0 ] -ge 20 ) {
44+ Write-Host " 已检测到 Node v$ ( $nodeVer -join ' .' ) ≥20,跳过安装"
45+ } else {
46+ if (-not (Test-Cmd " nvm" )) {
47+ Write-Host " 正在安装 nvm-windows..."
48+ $nvmSetup = " $env: TEMP \nvm-setup.exe"
49+ Invoke-WebRequest " https://download.fastgit.org/coreybutler/nvm-windows/releases/latest/download/nvm-setup.exe" - OutFile $nvmSetup
50+ Start-Process - Wait - FilePath $nvmSetup - ArgumentList " /silent"
51+ Remove-Item $nvmSetup
52+ $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , " Machine" ) + " ;" + [System.Environment ]::GetEnvironmentVariable(" Path" , " User" )
53+ }
54+ nvm install 20
55+ nvm use 20
56+ }
57+ } else {
58+ if (-not (Test-Cmd " nvm" )) {
59+ Write-Host " 正在安装 nvm-windows..."
60+ $nvmSetup = " $env: TEMP \nvm-setup.exe"
61+ Invoke-WebRequest " https://download.fastgit.org/coreybutler/nvm-windows/releases/latest/download/nvm-setup.exe" - OutFile $nvmSetup
62+ Start-Process - Wait - FilePath $nvmSetup - ArgumentList " /silent"
63+ Remove-Item $nvmSetup
64+ $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , " Machine" ) + " ;" + [System.Environment ]::GetEnvironmentVariable(" Path" , " User" )
65+ }
66+ nvm install 20
67+ nvm use 20
68+ }
69+
70+ foreach ($tool in @ (" yarn" , " pm2" , " git" , " python" )) {
71+ if (-not (Test-Cmd $tool )) {
72+ switch ($tool ) {
73+ " yarn" { npm install - g yarn }
74+ " pm2" { npm install - g pm2 }
75+ " git" { winget install -- id Git.Git - e -- source winget }
76+ " python" { winget install -- id Python.Python.3 - e -- source winget }
77+ }
78+ $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , " Machine" ) + " ;" + [System.Environment ]::GetEnvironmentVariable(" Path" , " User" )
79+ }
80+ }
81+ }
82+
83+ # ---------- 工作目录 ----------
84+ $repoDir = Read-Host " 请输入项目存放目录(留空则使用当前目录)"
85+ if ([string ]::IsNullOrWhiteSpace($repoDir )) { $repoDir = (Get-Location ).Path }
86+ $projectPath = Join-Path $repoDir " drpy-node"
87+ $remoteRepo = " https://github.com/hjdhnx/drpy-node.git"
88+
89+ # ---------- 首次克隆 / 配置 ----------
90+ Use-ProxyIfNeeded - Script {
91+ if (-not (Test-Path $projectPath )) {
92+ Write-Host " 正在克隆仓库..."
93+ git clone $remoteRepo $projectPath
94+ }
95+ Set-Location $projectPath
96+
97+ # 初始化配置
98+ $configJson = " config\env.json"
99+ if (-not (Test-Path $configJson )) {
100+ @ {
101+ ali_token = " " ; ali_refresh_token = " " ; quark_cookie = " " ;
102+ uc_cookie = " " ; bili_cookie = " " ; thread = " 10" ;
103+ enable_dr2 = " 1" ; enable_py = " 2"
104+ } | ConvertTo-Json | Set-Content $configJson - Encoding UTF8
105+ }
106+
107+ $envFile = " .env"
108+ if (-not (Test-Path $envFile )) {
109+ Copy-Item " .env.development" $envFile
110+ $cookieAuth = Read-Host " 网盘入库密码(默认 drpys)"
111+ $apiUser = Read-Host " 登录用户名(默认 admin)"
112+ $apiPass = Read-Host " 登录密码(默认 drpys)"
113+ $apiPwd = Read-Host " 订阅PWD值(默认 dzyyds)"
114+ (Get-Content $envFile ) `
115+ -replace ' COOKIE_AUTH_CODE = .*' , " COOKIE_AUTH_CODE = $ ( if ([string ]::IsNullOrWhiteSpace($cookieAuth )){' drpys' }else {$cookieAuth }) " `
116+ -replace ' API_AUTH_NAME = .*' , " API_AUTH_NAME = $ ( if ([string ]::IsNullOrWhiteSpace($apiUser )){' admin' }else {$apiUser }) " `
117+ -replace ' API_AUTH_CODE = .*' , " API_AUTH_CODE = $ ( if ([string ]::IsNullOrWhiteSpace($apiPass )){' drpys' }else {$apiPass }) " `
118+ -replace ' API_PWD = .*' , " API_PWD = $ ( if ([string ]::IsNullOrWhiteSpace($apiPwd )){' dzyyds' }else {$apiPwd }) " |
119+ Set-Content $envFile - Encoding UTF8
120+ }
121+
122+ # 首次安装依赖
123+ if (-not (Test-Path " node_modules" )) {
124+ Write-Host " 首次安装 Node 依赖..."
125+ yarn config set registry https:// registry.npmmirror.com /
126+ yarn
127+ }
128+ if (-not (Test-Path " .venv\pyvenv.cfg" )) {
129+ Write-Host " 首次创建 Python 虚拟环境..."
130+ python - m venv .venv
131+ }
132+ & .\.venv\Scripts\Activate.ps1
133+ if ((git diff HEAD^ HEAD -- name- only 2> $null ) -match " requirements.txt" ) {
134+ Write-Host " 检测到 requirements.txt 变动,更新 Python 依赖..."
135+ pip install - r spider\py\base\requirements.txt - i https:// mirrors.cloud.tencent.com / pypi/ simple
136+ }
137+
138+ # 首次或 PM2 未启动时启动
139+ if (-not (pm2 list | Select-String " drpyS.*online" )) {
140+ Write-Host " 首次启动 PM2 进程..."
141+ pm2 start index.js -- name drpyS -- update-env
142+ pm2 save
143+ } else {
144+ Write-Host " PM2 进程 drpyS 已在运行,跳过启动"
145+ }
146+ }
147+
148+ # ---------- 任务计划 ----------
149+ $taskStartup = " drpyS_PM2_Startup"
150+ $taskUpdate = " drpyS_Update"
151+
152+ if (-not (Get-ScheduledTask - TaskName $taskStartup - ErrorAction SilentlyContinue)) {
153+ $action = New-ScheduledTaskAction - Execute " powershell.exe" `
154+ - Argument " -NoProfile -WindowStyle Hidden -Command pm2 resurrect"
155+ $trigger = New-ScheduledTaskTrigger - AtStartup - RandomDelay (New-TimeSpan - Seconds 30 )
156+ $setting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
157+ Register-ScheduledTask - TaskName $taskStartup - Action $action - Trigger $trigger `
158+ - Settings $setting - User " SYSTEM" - RunLevel Highest - Force | Out-Null
159+ Write-Host " 已创建开机自启任务:$taskStartup "
160+ }
161+
162+ if (-not (Get-ScheduledTask - TaskName $taskUpdate - ErrorAction SilentlyContinue)) {
163+ $action = New-ScheduledTaskAction - Execute " powershell.exe" `
164+ - Argument " -NoProfile -WindowStyle Hidden -Command `" & { cd '$projectPath '; git fetch origin; if (git status -uno | Select-String 'Your branch is behind') { git reset --hard origin/main; yarn --prod --silent; if (git diff HEAD^ HEAD --name-only | Select-String 'spider/py/base/requirements.txt') { python -m venv .venv; & .\.venv\Scripts\Activate.ps1; pip install -r spider\py\base\requirements.txt -q } pm2 restart drpyS } }`" "
165+ $trigger = New-ScheduledTaskTrigger - Once - At (Get-Date ) - RepetitionInterval (New-TimeSpan - Hours 24 )
166+ $setting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable
167+ Register-ScheduledTask - TaskName $taskUpdate - Action $action - Trigger $trigger `
168+ - Settings $setting - User " SYSTEM" - RunLevel Highest - Force | Out-Null
169+ Write-Host " 已创建每 24 小时更新任务:$taskUpdate "
170+ }
171+
172+ # ---------- 完成 ----------
173+ $ip = (ipconfig | Select-String " IPv4 地址" | Select-Object - First 1 ).ToString().Split(" :" )[-1 ].Trim()
174+ $public = (Invoke-RestMethod " https://ipinfo.io/ip" )
175+ Write-Host " 内网地址:http://${ip} :5757"
176+ Write-Host " 公网地址:http://${public} :5757"
177+ Write-Host " 脚本执行完成!重启后 drpyS 自动启动并每 24 小时检查更新。"
178+ Write-Host " 脚本只需要执行一次不需要重复执行。"
179+ Write-Host " 如果弹出空白窗口可以直接关闭不影响使用。"
0 commit comments