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.
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
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 /
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
And that code above, for my BLOB data works beautifully ;) both problems solved today.
/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.
