Webtorrent: How can I get total number of seeders and leechers in a swarm.

Created on 22 Oct 2018  路  8Comments  路  Source: webtorrent/webtorrent

How can I get total number of seeders and leechers of a torrent file with webtorrent. I am trying to make a torrent DHT crawler.

All 8 comments

Hey @bubundas17,

Unlike trackers which support the scrape extension, you will need to connect to every peer in the swarm in order to gather the total number of active peers and weather a peer is a seeder or leecher.

An easy way to gather weather a peer is a seeder or leecher is checking the bitfield against the total number of pieces in the torrent. You can achieve that by doing the following:

client.on('torrent', (torrent)=>{
  torrent.on('wire', (wire)=>{
    wire.on('bitfield', (bitfield)=>{
      // Bits set in the bitfield.
      var setBits = 0
      // Maximum number of bits available to be set with the current field size.
      var maxBits = bitfield.buffer.length << 3
      // The maximum number of bits which constitutes the whole torrent.
      var fullBits = torrent.pieces.length

      for(i=0; i<=maxBits; i++){
        if(bitfield.get(i)) setBits++
      }
      var state = fullBits === setBits ? "SEEDER" : "LEECHER";
      console.log(`[${torrent.infoHash}][${wire.peerId}] Pieces ${setBits}/${fullBits} - ${state}`)
    })
  })
})

Note: You need the metadata for the torrent before able to check if the peer is a seeder or leecher, but the bitfield is only sent once, as soon as the peer connects, so you will likely need to store the bitfield and wait for ut_metadata to get the metadata from other peers before determining if the peer is a seed or leech, which isn't accounted for here.

I personally wouldn't recommend using WebTorrent, instead using the components which WebTorrent uses under the hood (torrent-discovery, bittorrent-protocol, ut_metadata, ect.) to give you the most flexibility in your solution, but depending on what you need, the code above should give you a good foundation to work from if you plan to use the main library.

If you need any more help, feel free to continue to ask here, but as this is a question and not an issue, I'm going to close this.

Well, Can you please share the code example with using underlying modules? I wanna extract the total number of seeders and leechers of a magnet link. basically, I am trying to make a torrent crawler using nodejs. which basically index torrent information.

Hey @bubundas17,

Here's an example which I've thrown together quickly, it can be found here. It works with multiple info hashes but only relies on bitfield information which not all clients provide, you can manually iterate through all the pieces using wire.peerPieces.get(i) on peers that don't provide bitfields, but I'll leave that to you if you decide to do it this way without using the full WebTorrent library.

Hope it helps!

Hey @bubundas17,

Here's an example which I've thrown together quickly, it can be found here. It works with multiple info hashes but only relies on bitfield information which not all clients provide, you can manually iterate through all the pieces using wire.peerPieces.get(i) on peers that don't provide bitfields, but I'll leave that to you if you decide to do it this way without using the full WebTorrent library.

Hope it helps!

Thank You So much! It helped a lot!
Thanks, again!

well a personal thought.
what did you mean by "Unlike trackers which support the scrape extension"?
and i tried the code it kinda works but not giving total amount of seeders and leechers, or takes long time to calculate accurately.

Hey @bubundas17,

If a tracker supports the scrape extension, it allows a client to request the number of downloading and complete peers it's tracking.

As you asked for a solution to scrape the DHT, not trackers, the DHT does not have a built in way to determine the number of leeches or seeds without individually connecting to each peer returned by the DHT and calculating if they are a seed or leech, which will take a long time to iterate through all the peers returned.

If you could give me the infohash of the torrent you're having issues with (assuming it's legal) I'll happily help you troubleshoot it. I had no issues with the sintel infohash or any others I tested it with.

All the best,
Brad.

Actually I am trying to make a torrent search engine. I work is almost done. Except the seeder and leechers information.
Can I get your contact? We can chat there.

@SlientBot1 How can I get total number of seeders and leechers of a infohash from a tracker with using nodejs?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kocoten1992 picture kocoten1992  路  6Comments

aalhama picture aalhama  路  7Comments

marcu87 picture marcu87  路  5Comments

feross picture feross  路  9Comments

HughIsaacs2 picture HughIsaacs2  路  3Comments