How do you get the total size of a torrent in libtorrent?

Question:

How do you get the total size of the files in a torrent?

And is there any way to get the number of files in a torrent and the size of each one?

Asked By: Sheik797

||

Answers:

If you take a look at the C++ API you will find that the torrent-info and file_iterator methods a little down the page give you the information you are looking for. The python bindings section states:

The python interface is nearly identical to the C++ interface.

So you should be able to get at these methods with a little effort.

Answered By: Steve Barnes
h = ses.add_torrent(params)
s = h.status()
while (not h.is_seed()):
    print s.total_wanted   # prints total size wanted after meta data is obtained, before that 0 is printed.
Answered By: Sheik797

/usr/local/bin/torrent-size.py

#!/usr/bin/env python3

import libtorrent
import sys

torsize = 0
info = libtorrent.torrent_info(str(sys.argv[1]))
for f in info.files():
    torsize += f.size
print(torsize/1048576, "MB")

This will give you the file size for a torrent provided as the first parameter to the script. Below are instructions to install libtorrent and the libtorrent python bindings. If you get an error about cxxstd on Ubuntu, you need to download boost 1.76, bootstrap it, install it, then copy the source directory to /usr/share/boost-build and export BOOST_ROOT=/usr/share/boost-build and export BOOST_BUILD=/usr/share/boost-build.

macOS:

brew install boost boost-build [email protected] python3
git clone --recurse-submodules https://github.com/arvidn/libtorrent.git
cd libtorrent
echo "using darwin ;" >>~/user-config.jam
python3 setup.py build_ext --b2-args="cxxstd=14 openssl-include=/usr/local/opt/[email protected]/include dht=on asserts=production encryption=on crypto=openssl variant=release deprecated-functions=on i2p=on extensions=on streaming=on mmap-disk-io=on" install
echo '#!/usr/bin/env python3nnimport libtorrentnimport sysnntorsize = 0ninfo = libtorrent.torrent_info(str(sys.argv[1]))nfor f in info.files():nttorsize += f.sizenprint(torsize/1048576, "MB")' > /usr/local/bin/torrent-size.py
chmod 0755 /usr/local/bin/torrent-size.py
torrent-size.py example.torrent

*buntu/debian/kali

sudo apt-get install libboost-tools-dev libboost-dev libboost-system-dev python3-all python3-dev build-essential gcc openssl
git clone --recurse-submodules https://github.com/arvidn/libtorrent.git
cd libtorrent
echo "using gcc ;" >>~/user-config.jam
python3 setup.py build_ext --b2-args="cxxstd=14 dht=on asserts=production encryption=on crypto=openssl variant=release deprecated-functions=on i2p=on extensions=on streaming=on mmap-disk-io=on" install
echo '#!/usr/bin/env python3nnimport libtorrentnimport sysnntorsize = 0ninfo = libtorrent.torrent_info(str(sys.argv[1]))nfor f in info.files():nttorsize += f.sizenprint(torsize/1048576, "MB")' > /usr/local/bin/torrent-size.py
chmod 0755 /usr/local/bin/torrent-size.py
torrent-size.py example.torrent
Answered By: user1833042

Using torrent_parser:

import torrent_parser as tp

torrent_metadata = tp.parse_torrent_file("file.torrent")
totalsize = 0

for file in torrent_metadata['info']['files']:
    totalsize += file['length']

print(totalsize)
Answered By: Benjamin Goodacre
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.