Created
July 26, 2019 20:39
-
-
Save Nachtalb/1aa4fbf41561d34b1583de028f418b2e to your computer and use it in GitHub Desktop.
Check total size of all torrents recursively in a dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
# Install https://pypi.org/project/bencode.py/ | |
# $ pip install bencode.py | |
import bencode | |
total_bytes = 0 | |
files = list(Path().rglob('*.torrent')) | |
total_files = len(files) | |
print('Getting file sizes') | |
for index, torrent_file in enumerate(files): | |
print(f'{index + 1:05}/{total_files}', end='\r') | |
with open(torrent_file, 'rb') as file_: | |
torrent_data = file_.read() | |
torrent = bencode.decode(torrent_data) | |
byte = torrent['info'].get('length', 0) | |
if not byte: | |
for tor_file in torrent['info'].get('files', []): | |
byte += tor_file['length'] | |
if not byte: | |
print(f'Could not read size from {torrent_file}') | |
total_bytes += byte | |
sizes = { | |
1: 'KB', | |
2: 'MB', | |
3: 'GB', | |
4: 'TB', | |
5: 'PT', | |
} | |
index = len(str(total_bytes)) // 4 + 1 | |
human_readable_size = total_bytes | |
for temp in range(index): | |
human_readable_size = human_readable_size / 1024 | |
print(f'Toal size: {human_readable_size:.2f} {sizes[index]} ({total_bytes} B)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment