Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get total number of seeders and leechers in a swarm. #1529

Closed
bubundas17 opened this issue Oct 22, 2018 · 8 comments
Closed

How can I get total number of seeders and leechers in a swarm. #1529

bubundas17 opened this issue Oct 22, 2018 · 8 comments

Comments

@bubundas17
Copy link

@bubundas17 bubundas17 commented Oct 22, 2018

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.

@SilentBot1

This comment has been minimized.

Copy link
Member

@SilentBot1 SilentBot1 commented Oct 23, 2018

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.

@SilentBot1 SilentBot1 closed this Oct 23, 2018
@bubundas17

This comment has been minimized.

Copy link
Author

@bubundas17 bubundas17 commented Oct 23, 2018

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.

@SilentBot1

This comment has been minimized.

Copy link
Member

@SilentBot1 SilentBot1 commented Oct 23, 2018

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!

@bubundas17

This comment has been minimized.

Copy link
Author

@bubundas17 bubundas17 commented Oct 24, 2018

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!

@bubundas17

This comment has been minimized.

Copy link
Author

@bubundas17 bubundas17 commented Oct 27, 2018

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.

@SilentBot1

This comment has been minimized.

Copy link
Member

@SilentBot1 SilentBot1 commented Oct 27, 2018

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.

@bubundas17

This comment has been minimized.

Copy link
Author

@bubundas17 bubundas17 commented Oct 28, 2018

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.

@bubundas17

This comment has been minimized.

Copy link
Author

@bubundas17 bubundas17 commented Oct 28, 2018

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

@lock lock bot locked as resolved and limited conversation to collaborators Jan 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.