This is a Python script that parses a torrent file and extracts file data from it. It can build a tree-like structure of the files and directories present in the torrent.
- Python 3.x
- bencodepy (a library to parse bencoded data) - You can install it using
pip install bencode.py
.
-
Place the
test.torrent
file in the same directory as the script. -
Run the script
torrent_parser.py
. -
The script will read the
test.torrent
file, extract the file data, and build a tree-like structure representing the files and directories. -
The resulting tree structure will be printed in JSON format.
Parse the torrent file and extract the file data.
Parameters:
torrent_file_path
(str): The path to the torrent file.
Returns:
list[dict]
: A list of dictionaries, each containing the 'path' and 'length' of a file.
Build the tree-like structure out of the file data.
Parameters:
file_data_list
(list[dict]): A list of dictionaries, each containing the 'path' and 'length' of a file.
Returns:
dict
: A dictionary representing the tree-like structure of the files and directories, with the file lengths as values.
{
"Mushoku Tensei II - Isekai Ittara Honki Dasu [TV-2] [2023] [WEBRip] [1080p] [RUS + JAP]": {
"Mushoku Tensei II - 00 (WEBRip 1920x1080 x264 AAC Rus + Jap).mkv": 1076851686,
"Mushoku Tensei II - 01 (WEBRip 1920x1080 x264 AAC Rus + Jap).mkv": 1076777825,
"Mushoku Tensei II - 02 (WEBRip 1920x1080 x264 AAC Rus + Jap).mkv": 1303506293,
"Mushoku Tensei II - 03 (WEBRip 1920x1080 x264 AAC Rus + Jap).mkv": 1029535452
}
}