CaiWuXiTong/scripts/wangshang-mail/import-accounts.ps1
shanshi 65d37c8093 feat: 接入网商银行邮件账单自动导入 (#1199)
独立邮箱机器人读取网商银行加密账单,解密并导入财务台账。
2026-11-16 16:41:07 +08:00

75 lines
2.2 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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
}