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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

/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 openssl@1.1 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/openssl@1.1/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 python3\n\nimport libtorrent\nimport sys\n\ntorsize = 0\ninfo = libtorrent.torrent_info(str(sys.argv[1]))\nfor f in info.files():\n\ttorsize += f.size\nprint(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 python3\n\nimport libtorrent\nimport sys\n\ntorsize = 0\ninfo = libtorrent.torrent_info(str(sys.argv[1]))\nfor f in info.files():\n\ttorsize += f.size\nprint(torsize/1048576, "MB")' > /usr/local/bin/torrent-size.py
chmod 0755 /usr/local/bin/torrent-size.py
torrent-size.py example.torrent
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top