AriaNg 保存下载历史

前言

Aria2 的任务记录默认存储在内存中,AriaNg 重启后已完成任务会全部消失。为了让下载历史能够长期保存,可以利用 Aria2 的 event‑hook 机制,在任务完成时自动写入日志文件。

本文提供两种方法来保存已完成任务:

  • 简单方法:通过传递参数直接记录完成时间、GID、文件路径
  • 进阶方法:通过 JSON‑RPC 查询更多信息(如文件大小、下载地址)

当前环境:
Aria2 1.37.0
AriaNg 1.3.11

官方文档:event-hook


简单方法:直接写入日志

[!NOTE]
Aria2 在触发 on-download-complete 时会向脚本传递三个参数:

  1. %1:GID
  2. %2:文件数量
  3. %3:文件路径

思路:在任务完成时调用批处理脚本,将必要信息写入 history.txt

1. 修改 aria2.conf

on-download-complete=on-complete-hook.bat

2. 创建 on-complete-hook.bat(放在 aria2.exe 同目录)

@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

:: 获取当前时间
set datetime=%date% %time%

:: 写入日志
echo [%datetime%] GID=%1 FILE=%3 >> history.txt

endlocal

3. 示例输出

$ cat history.txt
[周一 2026/03/02 19:29:44.41] GID=68c9c804983c04fd FILE="F:/Downloads/test.txt"
[周一 2026/03/02 19:33:44.67] GID=8ec7f5f31254dbfb FILE="F:/Downloads/test2.txt"

此方法简单无脑,但只能获取 GID 、文件数量和文件路径3个信息。


进阶方法:通过 JSON‑RPC 获取完整任务信息

[!NOTE]
如果要记录 文件大小、下载地址、更多元数据,则需要通过 RPC 查询任务详情,可以在 hook.bat 中调用 PowerShell 来处理数据。
Bat 对 JSON 等数据处理太麻烦,所以在 Bat 中调用 PowerShell 来处理。

流程:

aria2 → on-complete-hook.bat → on-complete.ps1 → history.txt

1. 修改 aria2.conf

on-download-complete=on-complete-hook.bat

2. 创建 on-complete-hook.bat(放在 aria2.exe 同目录)

@echo off
powershell -ExecutionPolicy Bypass -File "Aria2 绝对路径\on-complete.ps1" %1 %2 %3

[!IMPORTANT]
需要修改上述代码中的实际 Aria2 绝对路径,例如:

powershell -ExecutionPolicy Bypass -File "D:\Program Files\Aria2\on-complete.ps1" %1 %2 %3

3. 创建 on-complete.ps1(放在 aria2.exe 同目录)

param(
    [string]$GID,
    [string]$FileCount,
    [string]$FilePath
)

# ========= 配置 =========
$RpcUrl = "http://127.0.0.1:6800/jsonrpc"
$RpcToken = ""   # 如使用 rpc-secret,请填写
$LogFile = "history.txt"
# ========================

$Now = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

# 构造 JSON 请求
$Body = @{
    jsonrpc = "2.0"
    method  = "aria2.tellStatus"
    id      = "1"
    params  = @("token:$RpcToken", $GID, @("totalLength","files"))
} | ConvertTo-Json -Depth 5

# 调用 RPC
$response = Invoke-RestMethod -Uri $RpcUrl -Method Post -Body $Body -ContentType "application/json"

$totalLength = [int64]$response.result.totalLength
$uri = $response.result.files[0].uris[0].uri

# 文件大小格式化
function Format-Size($bytes) {
    if ($bytes -ge 1GB) { return "{0:N2} GB" -f ($bytes / 1GB) }
    elseif ($bytes -ge 1MB) { return "{0:N2} MB" -f ($bytes / 1MB) }
    elseif ($bytes -ge 1KB) { return "{0:N2} KB" -f ($bytes / 1KB) }
    else { return "$bytes B" }
}

$sizeText = Format-Size $totalLength

# 写入日志
$logText = @"
======================================
GID: $GID
Time: $Now
Path: $FilePath
Size: $sizeText
URI: $uri

"@

$logText | Out-File -FilePath $LogFile -Encoding utf8 -Append

4. 示例输出

$ cat history.txt
======================================
GID: 274e3438cfcfc01c
Time: 2026-03-02 19:48:26
Path: F:/Downloads/test.txt
Size: 16 B
URI: https://d.pcs.baidu.com/file/...

这种方法可记录完整下载信息,适合长期归档或分析下载历史。

5. 可选择输出为 JSON Lines 格式,而非 txt

将 on-complete.ps1 修改如下:

点击查看代码
param(
    [string]$GID,
    [string]$FileCount,
    [string]$FilePath
)

# ========= 配置 =========
$RpcUrl = "http://127.0.0.1:6800/jsonrpc"
$RpcToken = ""   # 如使用 rpc-secret,请填写
$LogFile = "history.jsonl"
# ========================

$Now = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

# 构造 JSON-RPC 请求
$Params = if ($RpcToken -ne "") {
    @("token:$RpcToken", $GID, @("totalLength","files"))
} else {
    @($GID, @("totalLength","files"))
}

$Body = @{
    jsonrpc = "2.0"
    method  = "aria2.tellStatus"
    id      = "1"
    params  = $Params
} | ConvertTo-Json -Depth 5

# 调用 RPC
try {
    $response = Invoke-RestMethod -Uri $RpcUrl -Method Post -Body $Body -ContentType "application/json"
} catch {
    $errorJson = @{
        time = $Now
        gid  = $GID
        error = "RPC request failed"
    } | ConvertTo-Json -Compress
    Add-Content -Path $LogFile -Value $errorJson
    exit
}

# 解析字段
$totalLength = [int64]$response.result.totalLength
$files = $response.result.files

# 取第一个文件的下载地址(BT 多文件任务也能工作)
$uri = $files[0].uris[0].uri

# 构造 JSON 行
$logObject = @{
    time  = $Now
    gid   = $GID
    path  = $FilePath
    size  = $totalLength
    uri   = $uri
}

# 写入 JSON Lines
$logJson = $logObject | ConvertTo-Json -Compress
Add-Content -Path $LogFile -Value $logJson

jsonl 输出示例:

$ cat history.jsonl
{"time":"2026-03-02 19:48:26","gid":"274e3438cfcfc01c","path":"F:/Downloads/test.txt","size":16,"uri":"https://d.pcs.baidu.com/file/..."}

总结

Aria2 的 event‑hook 提供了灵活的扩展能力,上述两种方法虽然只适用于 Windows,但在 Linux/macOS 中只需将 bat 换为 sh,也能实现历史记录的持久化。

posted @ 2026-03-02 20:12  凌雪寒  阅读(19)  评论(0)    收藏  举报