Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
UDP Torrent Scraper [Working]
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / V4 / Support & Suggestions

2020-12-01 05:55:03
In progress.

/ajax/ajax_torrent_scrape_get.php?id=397

Current:

[{"torrent_id":397,"seeders":0,"leechers":0}]

Next step:

Use bdecode class to retrieve announce-list from BLOB data (in my case, as I dont use announce column for months now), this will otherwise be much easier for TT users. Once i grab that, the script should start working fine, as it turns out my query was incorrect at some point.


$announce = bdecode($db->decode_data($datatable['data'])) or die('bencoding error');
if (array_key_exists('info', $announce)) {
   if ($announce['info']['private'] === 1) {
       print("Private Tracker");
   } else {
       printf("Public Tracker", html_escape($announce['announce']));
   }
}


Great, we have our tracker URl - udp[url='http://p4p.arenabg.ch:1337/announce']://p4p.arenabg.ch:1337/announce[/url]

Now, we see that we are no longer even using torrents.seeders / torrents.leechers (because i personally use the peer table for stats instead for months now). So a way around that is to simply plug in my own bdecode class (its not from TT, its in-house).

Let's see if we get parsing and a result based on the first key in the 'announce' array and restart the scraping response for this torrent ID.

Lets just do this to get a head start on whats going on


$torrentid = isset($_GET['id'])?intval($_GET['id']):0; // Get torrent ID ?id=

$stmt = $conn->prepare("SELECT t.torrent_id, t.info_hash, b.data FROM torrents t LEFT JOIN torrents_blob b ON t.torrent_id = b.torrent_id WHERE t.torrent_id = :torrent_id LIMIT 1"); // NEW - BLOB
$stmt->bindValue(':torrent_id', $torrentid, PDO::PARAM_INT);
$stmt->execute();

   while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

       $announce = 'udp://p4p.arenabg.ch:1337/announce';

       // Work here
       $ret = $scraper->scrape($announce, $row['info_hash']);

   }
   foreach ($ret as $key => $value) {
       $stmt = $conn->prepare("UPDATE torrents SET leechers='" . $value['leechers'] . "', seeders='" . $value['seeders'] . "', times_completed='" . $value['completed'] . "', last_action=NOW(), visible='yes' WHERE torrent_id = :torrentid");
       $stmt->bindValue(':torrentid', $torrentid, PDO::PARAM_INT);
       $stmt->execute();
   }
}
catch (ScraperException $e) {
   echo ('Error: ' . $e->getMessage() . "<br />\n");
   echo ('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
}


Error: No connection response.
Connection error: yes


Interesting.. time to find out why. Whoops, i forgot to upload the file in the first place lol

BOOM! Scraped a UDP tracker and we have 27 seeders & 1 leecher

[ATTACH type="full" alt="Working-Scraper.png"]5989[/ATTACH]

Lets check our GET script:

[{"torrent_id":397,"seeders":27,"leechers":1}]

Yup! All good now.

Seeders and Leechers in the photo was set to 0 / 0 before i even started. So it was working for my site the entire time, the problem was I converted over to BLOB data and it never caught on.

Conclusion:

WE HAVE A WORKING UDP SCRAPER!!

This is just simply mysql (not mysqli) so keep that in mind. This is converted from PDO.

Save as udpscraper.php and upload to root /

<?php
// TT UDP Scraper - Nov 30, 2020
require_once 'backend/functions.php';
$torrentid = isset($_GET['id'])?intval($_GET['id']):0; // Get torrent ID ?id=
$limit     = 1;

$query = SQL_Query_exec("SELECT id, announce, info_hash FROM torrents WHERE external = 'yes' AND id = '$torrentid' LIMIT 1");
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;
   }
}
class udptscraper extends tscraper
{
   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";
       $packet         = $current_connid . pack("N", 0) . pack("N", $transaction_id);
       fwrite($fp, $packet);
       $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);
       $hashes         = '';
       foreach ($infohash as $hash) {
           $hashes .= pack('H*', $hash);
       }
       $packet = $current_connid . pack("N", 2) . pack("N", $transaction_id) . $hashes;
       fwrite($fp, $packet);
       $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);
       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);
   }
}
try {
   $timeout = 2;
   $scraper = new udptscraper($timeout);
   while ($row = mysql_fetch_assoc($query)) {
       $ret = $scraper->scrape($row['announce'], $row['info_hash']);
   }
   foreach ($ret as $key => $value) {
       SQL_Query_exec("UPDATE torrents SET leechers='" . $value['leechers'] . "', seeders='" . $value['seeders'] . "', times_completed='" . $value['completed'] . "', last_action=NOW(), visible='yes' WHERE id = '$torrentid'");
   }
}
catch (ScraperException $e) {
   echo ('Error: ' . $e->getMessage() . "<br />\n");
   echo ('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
}
?>


Now point your site to

yoursite.com/udpscraper.php?id=TORRENT_ID

----


My situation is different, as this is what I have to do to pull from BLOB data

require_once '../../bencoding.php';
require_once '../../site.php';
try {
   $timeout = 2;
   $scraper = new udptscraper($timeout);
   while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       
       // Added support for reading announce url from BLOB data - Nov 30, 2020
       $blobdata  = bdecode($db->decode_data($row['data'])) or die('bencoding error');
       if (array_key_exists('announce', $blobdata)) {
           $announce = $blobdata['announce'];
       }else{
           throw new ScraperException('No announce url found in BLOB data.', 0, true);
       }
       
       $ret = $scraper->scrape($announce, $row['info_hash']);        
   }
   foreach ($ret as $key => $value) {
       $stmt = $conn->prepare("UPDATE torrents SET leechers='" . $value['leechers'] . "', seeders='" . $value['seeders'] . "', times_completed='" . $value['completed'] . "', last_action=NOW(), visible='yes' WHERE torrent_id = :torrent_id");
       $stmt->bindValue(':torrent_id', $torrentid, PDO::PARAM_INT);
       $stmt->execute();
   }
}


And that code above, for my BLOB data works beautifully ;) both problems solved today.
Working-Scraper.png
Working-Scraper.png
2020-12-01 12:03:09
@[USER=20382]STorrents2020[/USER]
I don't know what is for version of TT you use for that scraper, but I test it on my live server and try to use it without success, and secound in PDO version from m-jay we don't have this folder and files:  require_once 'backend/functions.php';

In my case I have blank white page

Someone who can test it and leave respond here


www.exyusubs.com
TorrentTrader V3.8.3 (SE)
OS: AlmaLinux 9.9 (BC)
Apache version: 2.4.71
PHP version: 8.5.13 (PHP-FPM)
MySQL version: 10.11.18-MariaDB
2020-12-01 14:50:08
I don’t use torrenttrader anymore I’m on a custom platform that used to be torrenttrader, and 2 weeks of very hard work put us on our own code base (so I posted it as V4), some things are the same but mostly not. I use my own database class for example.

White page means success in my case, I never added a “scraping complete” message, I forgot to mention that.

Run the scraper on V3 (non-MySQLi), and check database or torrent detail page.

I’ll do this up as MySQLi tonight for those of you using that. M-jay can do it up for his db class (basically search and replace stuff).

Works fine
2020-12-01 21:43:27
heres a quick test i did on pdo that works fine on xampp but live server is not having non of it
controllers/Test.php
<?php
class Test extends Controller
{

   public function __construct()
   {
       $this->db = new Database;
   }

   public function index()
   {
       dbconn();
       try {
           $timeout = 12;
           $scraper = new Udptscraper($timeout);
           $ret = $scraper->scrape('udp://9.rarbg.to:2890/announce', '546d6aed41217195ed2c82e9c561b7badbd6b590');
           foreach ($ret as $array) {
               $keys = array_keys( $array);
               echo $array[ $keys[0] ].'&nbsp;seeders<br>';
               echo $array[ $keys[1] ].'&nbsp;completed<br>';
               echo $array[ $keys[2] ].'&nbsp;leechers<br>';
           }
       } catch (ScraperException $e) {
           echo ('Error: ' . $e->getMessage() . "<br />\n");
           echo ('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
       }
   }

}


had to use array keys for test setting value in foreach broke it for some strange reason
this is external so should work without edit, servers not having none of it, everything i try works on xampp
2020-12-02 00:15:41
"m-jay, post: 69653, member: 20288":

heres a quick test i did on pdo that works fine on xampp but live server is not having non of it
controllers/Test.php
<?php
class Test extends Controller
{

   public function __construct()
   {
       $this->db = new Database;
   }

   public function index()
   {
       dbconn();
       try {
           $timeout = 12;
           $scraper = new Udptscraper($timeout);
           $ret = $scraper->scrape('udp://9.rarbg.to:2890/announce', '546d6aed41217195ed2c82e9c561b7badbd6b590');
           foreach ($ret as $array) {
               $keys = array_keys( $array);
               echo $array[ $keys[0] ].'&nbsp;seeders<br>';
               echo $array[ $keys[1] ].'&nbsp;completed<br>';
               echo $array[ $keys[2] ].'&nbsp;leechers<br>';
           }
       } catch (ScraperException $e) {
           echo ('Error: ' . $e->getMessage() . "<br />\n");
           echo ('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
       }
   }

}


had to use array keys for test setting value in foreach broke it for some strange reason
this is external so should work without edit, servers not having none of it, everything i try works on xampp


Nice!! That’s so cool seeing it plugged into a controller, very neat. That’s never done before as far as I know. You nailed it.

Your foreach contents is a bit wrong as I see it, your defining array keys when they are already defined by the loop itself (that’s why I don’t use while(), you have to tell it what to do too much, and often we ARE using arrays lol so while has a certain place.

try array_push() and push the keys to a empty array just before foreach(), like $blah = array()

then define like $blah = $row[‘something’] in the loop. I’ll fix it up shortly for you, I’m using php 7.4.12 so maybe they changed something there?
2020-12-02 00:27:44
$foo = array();
foreach ($ret as $array => $keys)  {
  $keys[‘value’] = $array[‘othervalue’];
array_push($ret, $foo)
}

// call $ret now
2020-12-02 00:33:08
Looking at your Test class, it looks like a LOT of code is missing, like data streams and routine logic, quite a lot happening in the class so I think it’s best to look at that, I know nothing about abstract extending and abstract functions, it’s possible it just needs a copy over
2023-07-21 08:47:29
can it SCRAPE from INFOHASH??
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 10
  • Members: 0
  • Guests online: 10
  • Comments: 3
  • Messages: 1,728