spider.phps

#!/usr/bin/php
<?
// Settings and etc
error_reporting(E_ALL);
$update_time = 60*60; // Time between stats updates on torrents

require_once './public_html/functions.inc.php';
require_once './public_html/mysql.inc.php';
require_once './public_html/duration.php';

$sql = mysql_query('SELECT torrents.hash, torrents.time_lastscrape, trackers.scrape ' .
  'FROM torrents, trackers ' .
  'WHERE torrents.id_tracker = trackers.id ' .
  'AND torrents.time_lastscrape < "' . ( time() - $update_time ) . '" ' .
  'ORDER BY torrents.id_tracker') or die(mysql_error());

while($row = mysql_fetch_assoc($sql))
{
  $stats = torrent_scrape_url($row['scrape'], $row['hash']);
  echo "Torrent: " . $row['hash'] . " " . str_pad("Peers: " . join('/', $stats), 20) . "(Updated: " . duration( time() - $row['time_lastscrape'] ) . ")\n";
  if($stats['seeds'] !== NULL || $stats['peers'] !== NULL)
  {
    // New stats for the torrent was avalible - update
    mysql_query("UPDATE torrents " .
      "SET seeds = '" . $stats['seeds'] . "', peers = '" . $stats['peers'] . "', time_lastscrape = UNIX_TIMESTAMP() " .
      "WHERE hash = '" . $row['hash'] . "'") or die(mysql_error());
  } else
  {
    // No stats were avalible for the torrent
    mysql_query("UPDATE torrents " .
      "SET seeds = '-1', peers = '-1', time_lastscrape = UNIX_TIMESTAMP() " .
      "WHERE hash = '" . $row['hash'] . "'") or die(mysql_error());
  }
}

?>