[CmdletBinding()] param( [string] $Workbook = 'D:\Desktop\网商数据抓取.xlsx', [string] $Output = '', [string] $AuthCode = '', [string] $ImapHost = 'imap.163.com', [int] $ImapPort = 993 ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ([string]::IsNullOrWhiteSpace($Output)) { $Output = Join-Path $root 'runtime\private\wangshang-mail-accounts.json' } function Resolve-Python { $configured = $env:WANGSHANG_MAIL_PYTHON $candidates = @( $configured, (Join-Path $env:USERPROFILE '.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe') ) foreach ($candidate in $candidates) { if (-not [string]::IsNullOrWhiteSpace($candidate) -and (Test-Path -LiteralPath $candidate -PathType Leaf)) { return $candidate } } $command = Get-Command python.exe -ErrorAction SilentlyContinue | Select-Object -First 1 if ($null -ne $command -and -not [string]::IsNullOrWhiteSpace($command.Source)) { & $command.Source -c 'import openpyxl' 2>$null if ($LASTEXITCODE -eq 0) { return $command.Source } } throw '未找到可用的 Python 和 openpyxl,请设置 WANGSHANG_MAIL_PYTHON' } if ([string]::IsNullOrWhiteSpace($AuthCode)) { $secure = Read-Host '请输入网易邮箱客户端授权码' -AsSecureString $pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) try { $AuthCode = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($pointer) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($pointer) } } if ([string]::IsNullOrWhiteSpace($AuthCode)) { throw '邮箱客户端授权码不能为空' } $env:WANGSHANG_MAIL_AUTH_CODE = $AuthCode try { $python = Resolve-Python $script = Join-Path $PSScriptRoot 'fetch_daily.py' & $python -X utf8 $script init ` --workbook $Workbook ` --output $Output ` --imap-host $ImapHost ` --imap-port $ImapPort if ($LASTEXITCODE -ne 0) { throw '生成网商邮件配置失败' } } finally { Remove-Item Env:WANGSHANG_MAIL_AUTH_CODE -ErrorAction SilentlyContinue }