Pergunta

Criei um script que gera informações sobre um arquivo torrent!Mas estou faltando criar uma função de exibição de sementes e pares!Alguém me disse que estão no campo preenchido definido no torrent.Por favor, meus códigos de função de classe a partir dos quais exibo as informações geradas usando um bencode.php que retira os dados e este script, chamado torrent.php converte-o em formato legível!

<?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;
}

?>

Por favor, me ajude!

Desde já, obrigado!

Foi útil?

Solução

Um pouco atrasado, mas a classe que você diz ter criado vem de:

https://github.com/torrage/Torrage

Seu propósito original não era recuperar esse tipo de dados.

Uma classe que fornece sementes e pares para torrent, incluindo o restante dos dados, consulte:

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

Outras dicas

Essas informações não são armazenadas no arquivo .torrent.São dados altamente dinâmicos, que podem mudar a cada microssegundo em um torrent ‘ocupado’.O servidor não criará um arquivo .torrent personalizado com estatísticas atualizadas sempre que alguém fizer o download.

Pense nisso por um segundo.Você baixa um arquivo .torrent na segunda-feira, mas só o vê na próxima sexta.As estatísticas já têm uma semana e estão obsoletas.

Você pode, no entanto, pegar as informações do rastreador no .torrent e consultar as estatísticas desses rastreadores.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top