下载工具 aria2 控制文件(.aria2)解析
.aria2c 定义
参考文档:https://aria2.document.top/zh/technical-notes.html#aria2
解析脚本
parse_aria2c.py
import os
import sys
import struct
from datetime import datetime
def Usage():
print("Usage: python3 parse_aria2c.py <aria2c_file_path> <show_detail>")
print("show_detail: True or False")
print("Example: python3 parse_aria2c.py aria2c_file True")
def read_bytes(file, offset, length):
"""读取指定偏移和长度的字节"""
file.seek(offset)
return file.read(length)
def format_size(bytes):
"""
将字节大小转换为适当的单位(KB, MB, GB等),支持负数。
:param bytes: 原始字节大小,可以为负数
:return: 字符串,格式化后的大小和单位
"""
# 定义单位和阈值
units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
# 记录负号
sign = "-" if bytes < 0 else ""
# 转为正数进行计算
size = abs(bytes)
index = 0
# 每次除以1024,直到大小合适或达到最大单位
while size >= 1024 and index < len(units) - 1:
size /= 1024.0
index += 1
# 格式化输出,保留两位小数,并加上负号(如有)
return f"{sign}{size:.2f} {units[index]}"
def parse_file(file_path, show_detail=False):
print(f"Start parsing file:{file_path}")
file_stat = os.stat(file_path)
print("------ .aria2 FILE STAT ------")
print(f"Size: \t\t{format_size(file_stat.st_size)}")
print(f"Modified Time: \t{datetime.fromtimestamp(file_stat.st_mtime)}")
print(f"Access Time: \t{datetime.fromtimestamp(file_stat.st_atime)}")
print(f"Creation Time: \t{datetime.fromtimestamp(file_stat.st_ctime)}")
print()
print("------ .aria2 FILE INFO ------")
with open(file_path, "rb") as file:
# VER (版本): 2 bytes
# 从偏移量为 0 开始解析 VER (版本)
ver = struct.unpack(">H", read_bytes(file, 0, 2))[0]
print(f"Version: {ver}")
# EXT (扩展): 4 bytes
# 解析 EXT (扩展)
ext = struct.unpack(">I", read_bytes(file, 2, 4))[0]
print(f"Extension: {ext}")
# INFO HASH LENGTH: 4 bytes
# 解析 InfoHash Length
info_hash_length = struct.unpack(">I", read_bytes(file, 6, 4))[0]
print(f"InfoHash Length: {info_hash_length}")
# INFO HASH: (INFO HASH LENGTH) bytes
# 解析 InfoHash
info_hash = read_bytes(file, 10, info_hash_length).hex()
print(f"InfoHash: {info_hash}")
current_offset = 10 + info_hash_length
# PIECE LENGTH: 4 bytes
# 解析 Piece Length
piece_length = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]
print(f"Piece Length: {piece_length} ({format_size(piece_length)})")
current_offset += 4
# TOTAL LENGTH: 8 bytes
# 解析 Total Length
total_length = struct.unpack(">Q", read_bytes(file, current_offset, 8))[0]
print(f"Total Length: {total_length} ({format_size(total_length)})")
current_offset += 8
# UPLOAD LENGTH: 8 bytes
# 解析 Upload Length
upload_length = struct.unpack(">Q", read_bytes(file, current_offset, 8))[0]
print(f"Upload Length: {upload_length} ({format_size(upload_length)})")
current_offset += 8
# BITFIELD LENGTH: 4 bytes
# 解析 Bitfield Length
bitfield_length = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]
print(f"Bitfield Length: {bitfield_length}")
current_offset += 4
# BITFIELD: (BITFIELD LENGTH) bytes
# 解析 Bitfield
bitfield = read_bytes(file, current_offset, bitfield_length).hex()
print(f"Bitfield: {bitfield}")
current_offset += bitfield_length
# NUM IN-FLIGHT PIECE: 4 bytes
# 解析 Num In-Flight Piece
num_in_flight_piece = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]
print(f"Num In-Flight Piece: {num_in_flight_piece}")
current_offset += 4
# 以下4个字段重复 (NUM IN-FLIGHT PIECE) 次数。
# INDEX: 4 bytes
# LENGTH: 4 bytes
# PIECE BITFIELD LENGTH: 4 bytes
# PIECE BITFIELD: (PIECE BITFIELD LENGTH) bytes
# 循环解析每个 Num In-Flight Piece 的字段
for i in range(num_in_flight_piece):
index = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]
length = struct.unpack(">I", read_bytes(file, current_offset + 4, 4))[0]
piece_bitfield_length = struct.unpack(">I", read_bytes(file, current_offset + 8, 4))[0]
current_offset += 12 # 跳过 Index (4 bytes), Length (4 bytes), Piece Bitfield Length (4 bytes)
# 解析 Piece Bitfield
piece_bitfield = read_bytes(file, current_offset, piece_bitfield_length).hex()
current_offset += piece_bitfield_length
if show_detail:
print(f"In-Flight Piece {i}: Index: {index}, Length: {length} ({format_size(length)}), Piece Bitfield Length: {piece_bitfield_length}, Piece Bitfield: {piece_bitfield}")
else:
print(f"In-Flight Piece {i}: Index: {index}, Length: {length} ({format_size(length)})")
if __name__ == '__main__':
argv = sys.argv
if len(argv) not in (2,3):
Usage()
sys.exit(1)
show_detail=False
file_path = argv[1]
if len(argv) == 3:
show_detail_str = argv[2]
if show_detail_str.lower() in ("true","false"):
show_detail = True if show_detail_str.lower() == "true" else False
else:
Usage()
sys.exit(1)
if not os.path.exists(file_path):
print(f"File:{file_path} not exists")
sys.exit(1)
parse_file(file_path, show_detail)
运行效果:
# python3 parse_aria2c.py tst.aria2
# 简化模式
------ .aria2 FILE STAT ------
Size: 3.05 KB
Modified Time: 2025-03-14 10:44:14.239716
Access Time: 2025-03-14 10:47:17.781647
Creation Time: 2025-03-14 10:44:14.239716
------ .aria2 FILE INFO ------
Version: 1
Extension: 0
InfoHash Length: 0
InfoHash:
Piece Length: 10485760 (10.00 MB)
Total Length: 12185573232 (11.35 GB)
Upload Length: 0 (0.00 B)
Bitfield Length: 146
Bitfield: ffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Num In-Flight Piece: 32
In-Flight Piece 0: Index: 51, Length: 10485760 (10.00 MB)
In-Flight Piece 1: Index: 52, Length: 10485760 (10.00 MB)
In-Flight Piece 2: Index: 53, Length: 10485760 (10.00 MB)
In-Flight Piece 3: Index: 54, Length: 10485760 (10.00 MB)
In-Flight Piece 4: Index: 55, Length: 10485760 (10.00 MB)
In-Flight Piece 5: Index: 56, Length: 10485760 (10.00 MB)
In-Flight Piece 6: Index: 57, Length: 10485760 (10.00 MB)
In-Flight Piece 7: Index: 58, Length: 10485760 (10.00 MB)
In-Flight Piece 8: Index: 59, Length: 10485760 (10.00 MB)
In-Flight Piece 9: Index: 60, Length: 10485760 (10.00 MB)
In-Flight Piece 10: Index: 61, Length: 10485760 (10.00 MB)
In-Flight Piece 11: Index: 62, Length: 10485760 (10.00 MB)
In-Flight Piece 12: Index: 63, Length: 10485760 (10.00 MB)
In-Flight Piece 13: Index: 64, Length: 10485760 (10.00 MB)
In-Flight Piece 14: Index: 65, Length: 10485760 (10.00 MB)
In-Flight Piece 15: Index: 66, Length: 10485760 (10.00 MB)
In-Flight Piece 16: Index: 67, Length: 10485760 (10.00 MB)
In-Flight Piece 17: Index: 68, Length: 10485760 (10.00 MB)
In-Flight Piece 18: Index: 69, Length: 10485760 (10.00 MB)
In-Flight Piece 19: Index: 70, Length: 10485760 (10.00 MB)
In-Flight Piece 20: Index: 71, Length: 10485760 (10.00 MB)
In-Flight Piece 21: Index: 72, Length: 10485760 (10.00 MB)
In-Flight Piece 22: Index: 73, Length: 10485760 (10.00 MB)
In-Flight Piece 23: Index: 74, Length: 10485760 (10.00 MB)
In-Flight Piece 24: Index: 75, Length: 10485760 (10.00 MB)
In-Flight Piece 25: Index: 76, Length: 10485760 (10.00 MB)
In-Flight Piece 26: Index: 77, Length: 10485760 (10.00 MB)
In-Flight Piece 27: Index: 78, Length: 10485760 (10.00 MB)
In-Flight Piece 28: Index: 79, Length: 10485760 (10.00 MB)
In-Flight Piece 29: Index: 80, Length: 10485760 (10.00 MB)
In-Flight Piece 30: Index: 81, Length: 10485760 (10.00 MB)
In-Flight Piece 31: Index: 82, Length: 10485760 (10.00 MB)
# python3 parse_aria2c.py tst.aria2 True
# 详细模式
------ .aria2 FILE STAT ------
Size: 3.05 KB
Modified Time: 2025-03-14 10:44:14.239716
Access Time: 2025-03-14 10:47:17.781647
Creation Time: 2025-03-14 10:44:14.239716
------ .aria2 FILE INFO ------
Version: 1
Extension: 0
InfoHash Length: 0
InfoHash:
Piece Length: 10485760 (10.00 MB)
Total Length: 12185573232 (11.35 GB)
Upload Length: 0 (0.00 B)
Bitfield Length: 146
Bitfield: ffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Num In-Flight Piece: 32
In-Flight Piece 0: Index: 51, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe00000000000000000
In-Flight Piece 1: Index: 52, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000
In-Flight Piece 2: Index: 53, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000
In-Flight Piece 3: Index: 54, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000
In-Flight Piece 4: Index: 55, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000000000
In-Flight Piece 5: Index: 56, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000
In-Flight Piece 6: Index: 57, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 7: Index: 58, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 8: Index: 59, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffe00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 9: Index: 60, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 10: Index: 61, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 11: Index: 62, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 12: Index: 63, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 13: Index: 64, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 14: Index: 65, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 15: Index: 66, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 16: Index: 67, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 17: Index: 68, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 18: Index: 69, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 19: Index: 70, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 20: Index: 71, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 21: Index: 72, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 22: Index: 73, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 23: Index: 74, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 24: Index: 75, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 25: Index: 76, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 26: Index: 77, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 27: Index: 78, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 28: Index: 79, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 29: Index: 80, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 30: Index: 81, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 31: Index: 82, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000