2011-04-02 14:57:17 +00:00
|
|
|
|
#!/opt/local/bin/php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
chdir('../..');
|
|
|
|
|
require_once 'common.php';
|
|
|
|
|
|
2011-04-02 16:44:45 +00:00
|
|
|
|
// Минимальный интервал между запросами
|
|
|
|
|
define('VKTIMEOUT', 10);
|
|
|
|
|
define('QUEUE_PACK', 30);
|
|
|
|
|
define('EMPTY_QUEUE_TIMEOUT', 60);
|
|
|
|
|
|
|
|
|
|
// Получаем имя бота
|
|
|
|
|
if (!isset($argv[1]) || !Config::get('bot:'. $argv[1])) {
|
|
|
|
|
die('Wrong bot name: '. @$argv[1]);
|
|
|
|
|
}
|
|
|
|
|
$bot_name = ucfirst($argv[1]);
|
|
|
|
|
|
|
|
|
|
// Инициализация бота по имени
|
|
|
|
|
$vk = new Vkontakte($bot_name);
|
|
|
|
|
|
|
|
|
|
// Данные о работе бота
|
|
|
|
|
$stats = array(
|
|
|
|
|
'started_job' => time(),
|
|
|
|
|
'eneded_job' => time(),
|
|
|
|
|
'pid' => getmypid(),
|
|
|
|
|
'good_results' => 0,
|
|
|
|
|
'bad_results' => 0,
|
|
|
|
|
'queue_size' => 0,
|
|
|
|
|
'last_request' => ''
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Устанавливаем коннект с БД
|
2011-04-02 14:57:17 +00:00
|
|
|
|
$db = Db::getInstance();
|
|
|
|
|
|
2011-04-02 16:44:45 +00:00
|
|
|
|
// Бот работает все время
|
|
|
|
|
while (true) {
|
|
|
|
|
$queue = $db->getRows($db->q("SELECT * FROM beathaven.queue WHERE status=0 OR status=2 ORDER BY priority DESC, times_failed ASC LIMIT ". QUEUE_PACK));
|
2011-04-02 14:57:17 +00:00
|
|
|
|
if (!$queue || count($queue) == 0) {
|
2011-04-02 16:44:45 +00:00
|
|
|
|
sleep(EMPTY_QUEUE_TIMEOUT);
|
2011-04-02 14:57:17 +00:00
|
|
|
|
} else {
|
2011-04-02 16:44:45 +00:00
|
|
|
|
$stats['queue_size'] = count($queue);
|
2011-04-02 14:57:17 +00:00
|
|
|
|
foreach ($queue as $t) {
|
2011-04-02 16:44:45 +00:00
|
|
|
|
$t1 = microtime(true);
|
|
|
|
|
echo "#{$t['track_id']} {$t['track_title']} -- ";
|
|
|
|
|
$ok = $vk->getTracks($t['track_title']);
|
2011-04-02 14:57:17 +00:00
|
|
|
|
|
2011-04-02 16:44:45 +00:00
|
|
|
|
if ($ok) {
|
|
|
|
|
echo "OK\n";
|
|
|
|
|
$db->q("UPDATE beathaven.queue SET status=1 WHERE track_id=". $t['track_id']);
|
|
|
|
|
$file_name = Config::get('app:Parser:good_html_dir'). $t['track_id'] .'.html';
|
|
|
|
|
$stats['good_results']++;
|
2011-04-02 14:57:17 +00:00
|
|
|
|
} else {
|
2011-04-02 16:44:45 +00:00
|
|
|
|
echo "FAILED\n";
|
2011-04-02 14:57:17 +00:00
|
|
|
|
$db->q("UPDATE beathaven.queue SET status = 2, times_failed = times_failed + 1 WHERE track_id=". $t['track_id']);
|
2011-04-02 16:44:45 +00:00
|
|
|
|
$file_name = Config::get('app:Parser:bad_html_dir'). $t['track_id'] .'.html';
|
|
|
|
|
$stats['bad_results']++;
|
2011-04-02 14:57:17 +00:00
|
|
|
|
}
|
2011-04-02 16:44:45 +00:00
|
|
|
|
file_put_contents($file_name, $vk->getHtml());
|
|
|
|
|
chmod($file_name, 0777);
|
|
|
|
|
|
|
|
|
|
$stats['last_request'] = $t['track_title'];
|
|
|
|
|
$stats['queue_size']--;
|
|
|
|
|
$stats['eneded_job'] = time();
|
|
|
|
|
|
|
|
|
|
$bot_stats_file_name = Config::get('app:Parser:bot_stats_dir'). $bot_name .'.json';
|
|
|
|
|
file_put_contents($bot_stats_file_name, json_encode($stats));
|
|
|
|
|
chmod($bot_stats_file_name, 0777);
|
|
|
|
|
|
2011-04-02 14:57:17 +00:00
|
|
|
|
$t2 = microtime(true);
|
2011-04-02 16:44:45 +00:00
|
|
|
|
if ($t2 - $t1 < VKTIMEOUT) {
|
|
|
|
|
sleep(ceil(VKTIMEOUT - ($t2 - $t1)));
|
2011-04-02 14:57:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-02 16:44:45 +00:00
|
|
|
|
}
|