How to convert torrent info hash for scrape?

2019-02-15 02:40发布

I have a torrent hash from the magnet link. For example: fda164e7af470f83ea699a529845a9353cc26576 When I try to get information about leechers and peers I should request: http://tracker.publicbt.com/scrape?info_hash=??? How should I convert info hash for this request? Is it url encoding or becoding? how? In PHP.

3条回答
放我归山
2楼-- · 2019-02-15 03:07

Got this python snippet from a colleague,

r = ''
s = 'fda164e7af470f83ea699a529845a9353cc26576'
for n in range(0, len(s), 2):
    r += '%%%s' % s[n:n+2].upper()
print r

Output: %FD%A1%64%E7%AF%47%0F%83%EA%69%9A%52%98%45%A9%35%3C%C2%65%76

Works like a charm.

Edit: Works like a charm for getting the tracker to give back status 200 (ok) but still doesn't work for retrieving the torrent details...

查看更多
家丑人穷心不美
3楼-- · 2019-02-15 03:10

In case someone is having trouble and comes across this thread in the future: the trick to this whole issue is to use the bool $raw_output argument of the PHP: sha1 function, setting it to "true".

The BDecode/DEncode classes can be found HERE. This project, called Trackon, also includes many other helpful classes to interact with torrent trackers and files.

So, in PHP, something like this will work to obtain the correct info hash for scraping the tracker for details:

include('./path/to/BDecode.php');
include('./path/to/BEncode.php');

function getHash($torFile){
    $tfile = BDecode(file_get_contents($torFile));
    $infohash = sha1(BEncode($tfile["info"]), TRUE);

    return urlencode($infohash);
}

Then merely call it like so:

$hash = getHash('./path/to/.torrent');

Hope this helps someone out there. I was still scratching my head after reading many posts about how to obtain the correct info hash. I understand why this wasn't mentioned anywhere now though, this argument was added in PHP 5. If you're not running PHP 5, you will have to convert the sha1 hash to raw binary after you calculate it.

查看更多
再贱就再见
4楼-- · 2019-02-15 03:13

It's a raw hexadecimal representation. Use pack() with H to convert it. Then URL encode it.

查看更多
登录 后发表回答