-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_torrent.py
More file actions
36 lines (28 loc) · 1.18 KB
/
read_torrent.py
File metadata and controls
36 lines (28 loc) · 1.18 KB
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
import json
import os
# Function to read a .torrent file and extract metadata
def read_torrent_file(torrent_file_path):
if not os.path.exists(torrent_file_path):
raise FileNotFoundError(f"The file at {torrent_file_path} does not exist.")
with open(torrent_file_path, "r") as file:
try:
metadata = json.load(file)
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON from {torrent_file_path}: {e}")
print(f"Loaded metadata from {torrent_file_path}:")
print(json.dumps(metadata, indent=4))
return metadata
# Function to read a chunk of data from a file
def read_chunk_data(file_name, chunk_index, chunk_size):
if not os.path.exists(file_name):
raise FileNotFoundError(f"The file {file_name} does not exist.")
with open(file_name, 'rb') as f:
f.seek(chunk_index * chunk_size)
return f.read(chunk_size)
# Driver code to test the read_torrent_file function
if __name__ == "__main__":
torrent_file = input("Enter the path to the .torrent file: ")
try:
metadata = read_torrent_file(torrent_file)
except (FileNotFoundError, ValueError) as e:
print(e)