不错的 BitTorrent 跟踪器 API? [英] Nice BitTorrent tracker API?

查看:28
本文介绍了不错的 BitTorrent 跟踪器 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否知道任何易于使用、简洁的 Python 或 Perl API 来与 BitTorrent Trackers 进行交互?例如,我拿一个torrent文件,枚举文件中的所有tracker,并询问tracker与下载文件相关的peers的统计数据?

Do you know of any easy to use, neat API for Python or Perl to interact with BitTorrent Trackers? E.g., I take a torrent file, enumerate all trackers in the file and ask the tracker for the statistics of the peers related with the downloaded file?

BitTorrent Tracker 规范并不太复杂,但我不想重新发明轮子 :)

BitTorrent Tracker specification is not too complicated, but I don't want to reinvent the wheel :)

请注意,我不想下载数据,只是为了获取一些统计信息(Net::BitTorrent 远远超出我的需要)

Note that I don't want to download the data, just to grab some stats (Net::BitTorrent far more than I need)

推荐答案

我编写了 Perl 脚本来从 .torrent 文件中获取数据、汇集跟踪器并获取一些统计信息(文件哈希、连接到跟踪器的 IP、文件大小、等等.).没有什么大科学,只是一些 Perl-fu.要运行它,您需要:安装 Perl 模块 Bencodecurltransmission-show.调试垃圾发送到 stderr,正确的输出发送到 stdout.

I made the Perl script to get data from .torrent files, pool the trackers and get some statistics (file hash, IP connected to the tracker, file size, etc.). No big science, just some Perl-fu. To run it, you need: Perl module Bencode, curl and transmission-show installed. Debug garbage is sent to stderr and the proper output to stdout.

#!/usr/bin/perl

use Bencode qw( bencode bdecode );
use Data::Dumper;

use warnings;
use strict;

my $G_PEER_ID = "hfgdbvnchdgfhvnfbghf";
my $G_MAX_TIME = 20;

sub peer_decode
{
    my $d = shift;
    my @a = split '', $d;
#    printf ">>%d %d<<\n", length($d), scalar(@a);

    my @ret;

    while(@a) {
        my $ip = sprintf "%d.%d.%d.%d" ,
                unpack('C',shift(@a)),
                unpack('C',shift(@a)),
                unpack('C',shift(@a)),
                unpack('C',shift(@a));
        my $port = sprintf "%d", 256 * unpack('C',shift(@a))
                                     + unpack('C',shift(@a));

#        printf "%d $ip $port\n",scalar(@a);
        push @ret, $ip;
    }
    return \@ret;
}


sub get_tracker_data_from_file
{
    my $fname = shift;

    my $ret = {};

    my $c = `transmission-show $fname`;

    print STDERR "$c\n";

    if ( $c =~ /^\s+Hash:\s*(\S+)/mg ) {
        $ret->{'hash'} = $1;
    }

    if ( $c =~ /^\s+Total Size:\s*(.+)$/mg ) {
        $ret->{'size'} = $1;
    }

    my @g;
    @g = ($c =~ /Tier \#\d+[\n\r\s]+(\S+)/gm);
    if ( @g ) {
        $ret->{'tiers'} = \@g;
    }

    return $ret;

}

sub get_peer_ips
{
    my $hash = shift;
    my $tracker = shift;

    my $ret = undef;

    $hash =~ s/(..)/\%$1/g;
    $tracker =~ s/\/$//;

    my $c = "curl -m $G_MAX_TIME -s '$tracker?info_hash=$hash&peer_id=$G_PEER_ID&uploaded=0&downloaded=0&left=1'";
    print STDERR "$c\n";

    my $w = `$c`;
    return undef if not $w;
    printf STDERR "%s\n" , Dumper($w);
    return undef if $w =~ /<\s*html\s*>/gi;

    $w = bdecode($w, 1);

    if ( defined $w->{'peers'} ) {
        $ret = peer_decode($w->{'peers'});
    }
    return $ret;
}

# -- main

my @files = @ARGV;

if ( not @files ) {
    print <<END
    usage: $0 <file1.torrent> <file2.torrent> ...

    (c) http://stackoverflow.com/users/497208
END
}

for my $fname ( @files ) {
    printf STDERR "File: %s\n", $fname;

    my $tr = get_tracker_data_from_file($fname);
    printf STDERR "%s\n", Dumper $tr;

    my $hash = undef;
    $hash = $tr->{'hash'} if defined $tr->{'hash'};
    exit if not defined $hash;

    my $size = undef;
    if ( defined $tr->{'size'} ) {
        $size = $tr->{'size'};
    }
    else {
        $size = "?";
    }

    if ( defined $tr->{'tiers'} ) {
    #    shift @{$tr->{'tiers'}} for (1..5);
        for my $tracker ( @{$tr->{'tiers'}} ) {

            my $ips = get_peer_ips( $hash, $tracker);
            printf STDERR "%s\n", Dumper $ips;

            if ( defined $ips ) {
                for my $ip ( @$ips ) {
                    my $c = sprintf "%s; %16s; %s; %s", $hash, $ip, $size, $tracker;
                    printf STDERR "$c\n";
                    printf "$c\n";
                }
            }
        }
    }
}

这篇关于不错的 BitTorrent 跟踪器 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆