-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrent_parser.py
65 lines (51 loc) · 2.15 KB
/
torrent_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import bencodepy as bcp
import json
def parse_torrent(torrent_file_path: str) -> list[dict]:
"""
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.
"""
with open(torrent_file_path, "rb") as f:
torrent_data = bcp.Bencode(encoding="utf-8", encoding_fallback="value").decode(f.read())
file_data_list = []
if "info" in torrent_data and "files" in torrent_data["info"]:
files_len = len(torrent_data["info"]["files"])
for file_info in torrent_data["info"]["files"]:
length = file_info["length"]
path = "/".join(file_info["path"])
if files_len > 1:
path = torrent_data["info"]["name"] + "/" + path
file_data_list.append({"path": path, "length": length})
return file_data_list
def build_path_tree(file_data_list: list[dict]) -> dict:
"""
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.
"""
def _recurse(dic, path_parts, length):
if not path_parts:
return
if len(path_parts) == 1:
dic[path_parts[0]] = length
return
key, *new_path_parts = path_parts
if key not in dic:
dic[key] = {}
_recurse(dic[key], new_path_parts, length)
path_dict = {}
for file_info in file_data_list:
path_parts = file_info["path"].split("/")
file_length = file_info["length"]
_recurse(path_dict, path_parts, file_length)
return path_dict
if __name__ == "__main__":
torrent_file_path = "test.torrent"
file_data_list = parse_torrent(torrent_file_path)
result = build_path_tree(file_data_list)
print(json.dumps(result, indent=2))