简体   繁体   English

洪流刮板

[英]Torrent scraper

i'm using scrape torrents and show the seeds of them, I use the script that I found on the internet and it uses socket to connect the tracker and get the data, it run on my localhost with xampp server but when it does not server works, it does not catch the seeds of the stream and the script generates an error message我正在使用抓取种子并显示它们的种子,我使用我在互联网上找到的脚本,它使用套接字连接跟踪器并获取数据,它使用 xampp 服务器在我的本地主机上运行,​​但是当它没有服务器时有效,它不会捕获流的种子,并且脚本会生成错误消息

Script msg:脚本消息:

Error: Could not open HTTP connection.错误:无法打开 HTTP 连接。 Connection error: yes The script:连接错误:是 脚本:

Code 1 (udptracker.php):代码 1 (udptracker.php):

<?php
    /*  Torrent UDP Scraper
        v1.2
        
        2010 by Johannes Zinnau
        johannes@johnimedia.de
        
        Licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
        http://creativecommons.org/licenses/by-sa/3.0/
        
        It would be very nice if you send me your changes on this class, so that i can include them if they are improve it.
        Thanks!
        
        Usage:
        try{
            $timeout = 2;
            
            $scraper = new udptscraper($timeout);
            $ret = $scraper->scrape('udp://tracker.tld:port',array('0000000000000000000000000000000000000000'));
            
            print_r($ret);
        }catch(ScraperException $e){
            echo('Error: ' . $e->getMessage() . "<br />\n");
            echo('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
        }
    */

include "tscraper.php";

    class udptscraper extends tscraper{


        /*  $url: Tracker url like: udp://tracker.tld:port or udp://tracker.tld:port/announce
            $infohash: Infohash string or array (max 74 items). 40 char long infohash. 
            */
        public function scrape($url,$infohash){
            if(!is_array($infohash)){ $infohash = array($infohash); }
            foreach($infohash as $hash){
                if(!preg_match('#^[a-f0-9]{40}$#i',$hash)){ throw new ScraperException('Invalid infohash: ' . $hash . '.'); }
            }
            if(count($infohash) > 74){ throw new ScraperException('Too many infohashes provided.'); }
            if(!preg_match('%udp://([^:/]*)(?::([0-9]*))?(?:/)?%si', $url, $m)){ throw new ScraperException('Invalid tracker url.'); }
            $tracker = 'udp://' . $m[1];
            $port = isset($m[2]) ? $m[2] : 80;


            $transaction_id = mt_rand(0,65535);
            $fp = fsockopen($tracker, $port, $errno, $errstr);
            if(!$fp){ throw new ScraperException('Could not open UDP connection: ' . $errno . ' - ' . $errstr,0,true); }
            stream_set_timeout($fp, $this->timeout);


            $current_connid = "\x00\x00\x04\x17\x27\x10\x19\x80";


            //Connection request
            $packet = $current_connid . pack("N", 0) . pack("N", $transaction_id);
            fwrite($fp,$packet);


            //Connection response
            $ret = fread($fp, 16);
            if(strlen($ret) < 1){ throw new ScraperException('No connection response.',0,true); }
            if(strlen($ret) < 16){ throw new ScraperException('Too short connection response.'); }
            $retd = unpack("Naction/Ntransid",$ret);
            if($retd['action'] != 0 || $retd['transid'] != $transaction_id){
                throw new ScraperException('Invalid connection response.');
            }
            $current_connid = substr($ret,8,8);


            //Scrape request
            $hashes = '';
            foreach($infohash as $hash){ $hashes .= pack('H*', $hash); }
            $packet = $current_connid . pack("N", 2) . pack("N", $transaction_id) . $hashes;
            fwrite($fp,$packet);


            //Scrape response
            $readlength = 8 + (12 * count($infohash));
            $ret = fread($fp, $readlength);
            if(strlen($ret) < 1){ throw new ScraperException('No scrape response.',0,true); }
            if(strlen($ret) < 8){ throw new ScraperException('Too short scrape response.'); }
            $retd = unpack("Naction/Ntransid",$ret);
            // Todo check for error string if response = 3
            if($retd['action'] != 2 || $retd['transid'] != $transaction_id){
                throw new ScraperException('Invalid scrape response.');
            }
            if(strlen($ret) < $readlength){ throw new ScraperException('Too short scrape response.'); }
            $torrents = array();
            $index = 8;
            foreach($infohash as $hash){
                $retd = unpack("Nseeders/Ncompleted/Nleechers",substr($ret,$index,12));
                $retd['infohash'] = $hash;
                $torrents[$hash] = $retd;
                $index = $index + 12;
            }


            return($torrents);
        }
    }
?>

Code 2:代码 2:

<?php
include "httptscraper.php";

$scrap1 = "$row[torrent]";
preg_match('#magnet:\?xt=urn:btih:(?<hash>.*?)&dn=(?<filename>.*?)&tr=(?<trackers>.*?)$#', $scrap1, $magnet_link);
$magnet_link['trackers'] = explode('&', urldecode(str_replace('tr=','', $magnet_link['trackers'])));
$resultados2 = $magnet_link['trackers'][0];
$resultados1 = $magnet_link['hash'];

try{
            $timeout = 2;
            
            $scraper = new udptscraper($timeout);
            $ret = $scraper->scrape($resultados2,array($resultados1));
            
            $array = $ret[$resultados1];
            $array2 = $array['seeders'];
            print_r($array2);
        }catch(ScraperException $e){
            echo "Sem Info";
            //echo('Error: ' . $e->getMessage() . "<br />\n");
            //echo('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
        }
?>

Code 3 (tscraper.php):代码 3 (tscraper.php):

<?php
/*  Torrent Scraper Base Class
    v1.0
    
    2010 by Johannes Zinnau
    johannes@johnimedia.de
    
    Licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
    http://creativecommons.org/licenses/by-sa/3.0/
    
    It would be very nice if you send me your changes on this class, so that i can include them if they are improve it.
    Thanks!
    
    Usage:
    See udptscraper.php or httptscraper.php
*/


class ScraperException extends Exception {
    private $connectionerror;


    public function __construct($message,$code=0,$connectionerror=false){
        $this->connectionerror = $connectionerror;
        parent::__construct($message, $code);
    }


    public function isConnectionError(){
        return($this->connectionerror);
    }
}


abstract class tscraper {
    protected $timeout;


    public function __construct($timeout=2){
        $this->timeout = $timeout;
    }
}


?>

You server seems to be blocking the requests.您的服务器似乎正在阻止请求。 In this case you will need to find a torrent friendly server.在这种情况下,您需要找到一个适合 torrent 的服务器。 Take a look here: http://filesharefreak.com/2009/10/09/the-best-hosting-providers-for-private-trackers .看看这里: http : //filesharefreak.com/2009/10/09/the-best-hosting-providers-for-private-trackers You can also try some servers in countries like, i dont know, Sweden?你也可以尝试一些国家的服务器,我不知道,瑞典?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM