如何添加一个类函数来获取torrent中已完成的对等体?

时间:2011-02-19 12:59:29

标签: php httpwebrequest bittorrent utorrent monotorrent

我创建了一个脚本,可以生成有关torrent文件的信息!但我缺乏创建种子和同伴显示功能!有人告诉我他们在洪流中定义的完整字段中。请使用 bencode.php 显示生成的信息,我的班级功能代码会取出数据,这个名为 torrent.php 的脚本会以可读的形式对其进行转换!

<?php
include_once('bencode.php');

class Torrent
{
    // Private class members
    private $torrent;
    private $info;

    // Public error message, $error is set if load() returns false
    public $error;

    // Load torrent file data
    // $data - raw torrent file contents
    public function load( &$data )
    {
        $this->torrent = BEncode::decode( $data );

        if ( $this->torrent->get_type() == 'error' )
        {
            $this->error = $this->torrent->get_plain();
            return false;
        }
        else if ( $this->torrent->get_type() != 'dictionary' )
        {
            $this->error = 'The file was not a valid torrent file.';
            return false;
        }

        $this->info = $this->torrent->get_value('info');
        if ( !$this->info )
        {
            $this->error = 'Could not find info dictionary.';
            return false;
        }

        return true;
    }

    // Get comment
    // return - string
    public function getComment() {
        return $this->torrent->get_value('comment') ? $this->torrent->get_value('comment')->get_plain() : null;
    }

    // Get creatuion date
    // return - php date
    public function getCreationDate() {
        return  $this->torrent->get_value('creation date') ? $this->torrent->get_value('creation date')->get_plain() : null;
    }

    // Get created by
    // return - string
    public function getCreatedBy() {
        return $this->torrent->get_value('created by') ? $this->torrent->get_value('created by')->get_plain() : null;
    }

    // Get name
    // return - filename (single file torrent)
    //          directory (multi-file torrent)
    // see also - getFiles()
    public function getName() {
        return $this->info->get_value('name')->get_plain();
    }

    // Get piece length
    // return - int
    public function getPieceLength() {
        return $this->info->get_value('piece length')->get_plain();
    }

    // Get pieces
    // return - raw binary of peice hashes
    public function getPieces() {
        return $this->info->get_value('pieces')->get_plain();
    }

    // Get private flag
    // return - -1 public, implicit
    //           0 public, explicit
    //           1 private
    public function getPrivate() {
        if ( $this->info->get_value('private') )
        {
            return $this->info->get_value('private')->get_plain();
        }
        return -1;
    }

    // Get a list of files
    // return - array of Torrent_File
    public function getFiles() {
        // Load files
        $filelist = array();
        $length = $this->info->get_value('length');

        if ( $length )
        {
            $file = new Torrent_File();
            $file->name = $this->info->get_value('name')->get_plain();
            $file->length =  $this->info->get_value('length')->get_plain();
            array_push( $filelist, $file );
        }
        else if ( $this->info->get_value('files') )
        {
            $files = $this->info->get_value('files')->get_plain();
            while ( list( $key, $value ) = each( $files ) )
            {
                $file = new Torrent_File();

                $path = $value->get_value('path')->get_plain();
                while ( list( $key, $value2 ) = each( $path ) )
                {
                    $file->name .= "/" . $value2->get_plain();
                }
                $file->name = ltrim( $file->name, '/' );
                $file->length =  $value->get_value('length')->get_plain();

                array_push( $filelist, $file );
            }
        }

        return $filelist;
    }

    // Get a list of trackers
    // return - array of strings
    public function getTrackers() {
        // Load tracker list
        $trackerlist = array();

        if ( $this->torrent->get_value('announce-list') )
        {
            $trackers = $this->torrent->get_value('announce-list')->get_plain();
            while ( list( $key, $value ) = each( $trackers ) )
            {
                if ( is_array( $value->get_plain() ) ) {
                    while ( list( $key, $value2 ) = each( $value ) )
                    {
                        while ( list( $key, $value3 ) = each( $value2 ) )
                        {
                            array_push( $trackerlist, $value3->get_plain() );
                        }
                    }
                } else {
                    array_push( $trackerlist, $value->get_plain() );
                }
            }
        }
        else if ( $this->torrent->get_value('announce') )
        {
            array_push( $trackerlist, $this->torrent->get_value('announce')->get_plain() );
        }

        return $trackerlist;
    }


    // Helper function to make adding a tracker easier
    // $tracker_url - string
    public function addTracker( $tracker_url )
    {
        $trackers = $this->getTrackers();
        $trackers[] = $tracker_url;
        $this->setTrackers( $trackers );
    }

    // Replace the current trackers with the supplied list
    // $trackerlist - array of strings
    public function setTrackers( $trackerlist )
    {
        if ( count( $trackerlist ) >= 1 )
        {
            $this->torrent->remove('announce-list');
            $string = new BEncode_String( $trackerlist[0] );
            $this->torrent->set( 'announce', $string );
        }

        if ( count( $trackerlist ) > 1 )
        {
            $list = new BEncode_List();


            while ( list( $key, $value ) = each( $trackerlist ) )
            {
                $list2 = new BEncode_List();
                $string = new BEncode_String( $value );
                $list2->add( $string );
                $list->add( $list2 );
            }

            $this->torrent->set( 'announce-list', $list );
        }
    }

    // Update the list of files
    // $filelist - array of Torrent_File
    public function setFiles( $filelist )
    {
        // Load files
        $length = $this->info->get_value('length');

        if ( $length )
        {
            $filelist[0] = str_replace( '\\', '/', $filelist[0] );
            $string = new BEncode_String( $filelist[0] );
            $this->info->set( 'name', $string );
        }
        else if ( $this->info->get_value('files') )
        {
            $files = $this->info->get_value('files')->get_plain();
            for ( $i = 0; $i < count( $files ); ++$i )
            {
                $file_parts = split( '/', $filelist[$i] );
                $path = new BEncode_List();
                foreach ( $file_parts as $part )
                {
                    $string = new BEncode_String( $part );
                    $path->add( $string );
                }
                $files[$i]->set( 'path', $path );
            }
        }
    }

    // Set the comment field
    // $value - string
    public function setComment( $value )
    {
        $type = 'comment';
        $key = $this->torrent->get_value( $type );
        if ( $value == '' ) {
            $this->torrent->remove( $type );
        } elseif ( $key ) {
            $key->set( $value );
        } else {
            $string = new BEncode_String( $value );
            $this->torrent->set( $type, $string );
        }
    }

    // Set the created by field
    // $value - string
    public function setCreatedBy( $value )
    {
        $type = 'created by';
        $key = $this->torrent->get_value( $type );
        if ( $value == '' ) {
            $this->torrent->remove( $type );
        } elseif ( $key ) {
            $key->set( $value );
        } else {
            $string = new BEncode_String( $value );
            $this->torrent->set( $type, $string );
        }
    }


    // Set the creation date
    // $value - php date
    public function setCreationDate( $value )
    {
        $type = 'creation date';
        $key = $this->torrent->get_value( $type );
        if ( $value == '' ) {
            $this->torrent->remove( $type );
        } elseif ( $key ) {
            $key->set( $value );
        } else {
            $int = new BEncode_Int( $value );
            $this->torrent->set( $type, $int );
        }
    }

    // Change the private flag
    // $value - -1 public, implicit
    //           0 public, explicit
    //           1 private
    public function setPrivate( $value )
    {
        if ( $value == -1 ) {
            $this->info->remove( 'private' );
        } else {
            $int = new BEncode_Int( $value );
            $this->info->set( 'private', $int );
        }
    }

    // Bencode the torrent
    public function bencode()
    {
        return $this->torrent->encode();
    }

    // Return the torrent's hash
    public function getHash()
    {
        return strtoupper( sha1( $this->info->encode() ) );
    }
}

// Simple class to encapsulate filename and length
class Torrent_File
{
    public $name;
    public $length;
}

?>

请帮帮我!

提前致谢!

2 个答案:

答案 0 :(得分:0)

迟到但你说的创作课程来自:

https://github.com/torrage/Torrage

它的最初目的不是要检索那种数据。

一个让你获得种子和同伴的类,其他数据包括:

https://github.com/adriengibrat/torrent-rw

答案 1 :(得分:-1)

该信息未存储在.torrent文件中。它是高度动态的数据,可以在“忙碌”的洪流上每微秒改变一次。每次有人下载时,服务器都不会使用最新的统计信息构建自定义.torrent文件。

想一想。你在星期一下载了一个.torrent文件,但只在下周五看一下。这些统计数据现在已经过了一周,而且陈旧。

但是,您可以在.torrent中获取跟踪器信息,并查询这些跟踪器的统计信息。