php - Torrent scrapper -
i'm using scrap torrents , show seeds of them, use script found on internet , uses socket connect tracker , data, run on localhost xampp server when not server works, not catch seeds of stream , script generates error message
script msg:
error: not open http connection. connection error: yes script:
code 1 (udptracker.php):
<?php /* torrent udp scraper v1.2 2010 johannes zinnau johannes@johnimedia.de licensed under creative commons attribution-sharealike 3.0 unported license http://creativecommons.org/licenses/by-sa/3.0/ nice if send me changes on class, can include them if 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 $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 $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 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 $hash){ $retd = unpack("nseeders/ncompleted/nleechers",substr($ret,$index,12)); $retd['infohash'] = $hash; $torrents[$hash] = $retd; $index = $index + 12; } return($torrents); } } ?>
code 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):
<?php /* torrent scraper base class v1.0 2010 johannes zinnau johannes@johnimedia.de licensed under creative commons attribution-sharealike 3.0 unported license http://creativecommons.org/licenses/by-sa/3.0/ nice if send me changes on class, can include them if 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 blocking requests. in case need find torrent friendly server. take here: http://filesharefreak.com/2009/10/09/the-best-hosting-providers-for-private-trackers. can try servers in countries like, dont know, sweden?
Comments
Post a Comment