Changeset 35 for trunk

Show
Ignore:
Timestamp:
21.07.2005 12:58:19 (3 years ago)
Author:
m
Message:

Added getStats(). Updated docblock.

Location:
trunk
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/File/Bittorrent/Decode.php

    r28 r35  
    2424*   PHP translation by Gerard Krijgsman <webmaster@animesuki.com> 
    2525*   Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk> 
    26 * 
    2726* Info on the .torrent file format 
    28 * 
    2927* BEncoding is a simple, easy to implement method of associating 
    3028* data types with information in a file. The values in a torrent 
     
    3634* @package File_Bittorrent 
    3735* @category File 
    38 * 
    3936* @author Markus Tacker <m@tacker.org> 
    40 * 
    4137* @version $Id$ 
    4238*/ 
     
    4743require_once 'PEAR.php'; 
    4844require_once 'PHP/Compat.php'; 
     45require_once 'File/Bittorrent/Encode.php'; 
    4946 
    5047/** 
     
    5451 
    5552/** 
    56 * Decode .torrent files 
     53* Encode data in Bittorrent format 
     54* 
     55* Based on 
     56*   Original Python implementation by Petru Paler <petru@paler.net> 
     57*   PHP translation by Gerard Krijgsman <webmaster@animesuki.com> 
     58*   Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk> 
     59* Info on the .torrent file format 
     60* BEncoding is a simple, easy to implement method of associating 
     61* data types with information in a file. The values in a torrent 
     62* file are bEncoded. 
     63* There are 4 different data types that can be bEncoded: 
     64* Integers, Strings, Lists and Dictionaries. 
     65* [http://www.monduna.com/bt/faq.html] 
    5766* 
    5867* @package File_Bittorrent 
    5968* @category File 
     69* @author Markus Tacker <m@tacker.org> 
    6070*/ 
    6171class File_Bittorrent_Decode 
     
    123133    */ 
    124134    var $_position = 0; 
     135 
     136    /** 
     137    * @var string   Info hash 
     138    */ 
     139    var $info_hash; 
    125140 
    126141    /** 
     
    163178        $this->announce_list = array(); 
    164179        $this->_position     = 0; 
     180        $this->info_hash     = ''; 
    165181 
    166182        // Decode .torrent 
     
    168184        $this->_source_length = strlen($this->_source); 
    169185        $decoded = $this->_bdecode(); 
     186 
     187        // Compute info_hash 
     188        $Encoder = new File_Bittorrent_Encode; 
     189        $this->info_hash = sha1($Encoder->encode($decoded['info'])); 
    170190 
    171191        // Pull information form decoded data 
     
    339359    function _decode_list() 
    340360    { 
    341         $return = array(); 
     361        $return = array(); 
    342362        $char = $this->_getChar(); 
    343363        while ($this->_source{$this->_position} != 'e') { 
     
    361381        return $this->_source{$this->_position}; 
    362382    } 
     383 
     384    /** 
     385    * Returns the online stats for the torrent 
     386    * 
     387    * @return array|false 
     388    */ 
     389    function getStats() 
     390    { 
     391        $packed_hash = pack('H*', $this->info_hash); 
     392        $scrape_url = preg_replace('/\/announce$/', '/scrape', $this->announce) . '?info_hash=' . urlencode($packed_hash); 
     393        $scrape_data = file_get_contents($scrape_url); 
     394        $stats = $this->decode($scrape_data); 
     395        if (!isset($stats['files'][$packed_hash])) { 
     396            PEAR::raiseError('File_Bittorrent_Decode::getStats() - Invalid scrape data: "' . $scrape_data . '"'); 
     397            return false; 
     398        } 
     399        return $stats['files'][$packed_hash]; 
     400    } 
    363401} 
    364402