83 lines
2.4 KiB
PHP
83 lines
2.4 KiB
PHP
|
#!/opt/local/bin/php
|
|||
|
<?php
|
|||
|
|
|||
|
chdir('../..');
|
|||
|
require_once 'common.php';
|
|||
|
|
|||
|
// Минимальный интервал между запросами
|
|||
|
define('QUEUE_PACK', 50);
|
|||
|
define('EMPTY_QUEUE_TIMEOUT', 60);
|
|||
|
|
|||
|
$bot_name = 'Chopin';
|
|||
|
|
|||
|
// Данные о работе бота
|
|||
|
$stats = array(
|
|||
|
'started_job' => time(),
|
|||
|
'eneded_job' => time(),
|
|||
|
'pid' => getmypid(),
|
|||
|
'good_results' => 0,
|
|||
|
'bad_results' => 0,
|
|||
|
'queue_size' => 0,
|
|||
|
'last_request' => ''
|
|||
|
);
|
|||
|
|
|||
|
// Устанавливаем коннект с БД
|
|||
|
$db = Db::getInstance();
|
|||
|
|
|||
|
$vk = new Vkontakte();
|
|||
|
|
|||
|
// Бот работает все время
|
|||
|
while (true) {
|
|||
|
|
|||
|
// Получаем список песен для проверки, помечаем в очереди, что мы их взяли в работу
|
|||
|
$db->q('BEGIN');
|
|||
|
$queue = $db->getRows($db->q("SELECT * FROM beathaven.queue WHERE status=3 LIMIT ". QUEUE_PACK));
|
|||
|
$ids = array();
|
|||
|
foreach ($queue as $t) {
|
|||
|
$ids[] = $t['track_id'];
|
|||
|
}
|
|||
|
$db->q('UPDATE beathaven.queue SET status=4 WHERE track_id IN('. implode(',', $ids) .')');
|
|||
|
$db->q('COMMIT');
|
|||
|
|
|||
|
if (!$queue || count($queue) == 0) {
|
|||
|
sleep(EMPTY_QUEUE_TIMEOUT);
|
|||
|
} else {
|
|||
|
$stats['queue_size'] = count($queue);
|
|||
|
foreach ($queue as $t) {
|
|||
|
$t1 = microtime(true);
|
|||
|
echo "#{$t['track_id']} {$t['track_title']} -- ";
|
|||
|
|
|||
|
$html = file_get_contents(Config::get('app:Parser:good_html_dir'). $t['track_id'] .'.html');
|
|||
|
$vk->setHtml($html);
|
|||
|
$vk->parseHtml();
|
|||
|
$files = $vk->getFiles();
|
|||
|
|
|||
|
$tmp = explode(' - ', $t['track_title']);
|
|||
|
$artist_name = $tmp[0];
|
|||
|
unset($tmp[0]);
|
|||
|
$track_name = implode(' - ', $tmp);
|
|||
|
|
|||
|
$files = VkontakteMP3::check($files, $artist_name, $track_name, $t['track_length']);
|
|||
|
BeatDB::set($t['track_id'], $files);
|
|||
|
echo count($files) ."\n";
|
|||
|
|
|||
|
if (count($files) > 0) {
|
|||
|
$db->q("UPDATE beathaven.queue SET status=6 WHERE track_id=". $t['track_id']);
|
|||
|
$stats['good_results']++;
|
|||
|
} else {
|
|||
|
$db->q("UPDATE beathaven.queue SET status=5, times_failed = times_failed + 1 WHERE track_id=". $t['track_id']);
|
|||
|
$stats['bad_results']++;
|
|||
|
}
|
|||
|
|
|||
|
$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);
|
|||
|
|
|||
|
$t2 = microtime(true);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|