How do I validate all files in a torrent using libtorrent offline in python?

209 Views Asked by At

Given a torrent file, I would like to use libtorrent to validate that I have all the files, essentially what a force recheck would do in a bittorrent client, and I want to do it off-line and without involving some other client.

I've only found how to recalculate the hash for all pieces but I'm not quite sure how to use it if I already have everything and just want to make sure it matches what's already available on disk.

Here's a short example of what I would like to accomplish:

import libtorrent as lt
torrent = lt.load_torrent_file("torrents/legal_linux_iso.torrent")
torrent.save_path = 'data/'
if torrent.recheck():      # Mythical missing part
    print("Hurrah!")

I'm worried that if I try implementing code that calculate every piece, I will end up missing some detail with regards to version 1 or 2, hybrid torrent files, merkle trees and such. Hopefully there's a short and robust solution I've missed. Also, I want to use libtorrent for this because it's used for other things in my code and the client I'm working with, so if there's any quirks I want the quirks to at least match!

1

There are 1 best solutions below

2
Arvid On

There is a flag called stop_when_ready (see documentation) which will prevent the torrent from initiating any downloading.