Skip to content

Instantly share code, notes, and snippets.

/scheduler.php
Created Mar 15, 2015

Embed
What would you like to do?
Chinachu_Prototype_rev.5_20120218
<?php
// Chinachu Prototype rev.5 (2012/02/18)
// GPLv3
// Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
/* ユーザー設定 */
define('JSON_RESULT_SCHEDULE' , __dir__ . '/schedule.json');
define('JSON_RESULT_SCHEDULE_IGNORE' , __dir__ . '/schedule_ignore.json');
define('OUTPUT_COMMAND_RECORD' , __dir__ . '/log/recoding.log');
define('DIR_RECORDED' , __dir__ . '/recorded/');
define('RESERVE_LIST_TITLE' , __dir__ . '/reserve_titles.txt');
define('RESERVE_LIST_EXTRA' , __dir__ . '/reserve_extras.csv');
define('IGNORE_LIST_TITLE' , __dir__ . '/ignore_titles.txt');
define('IGNORE_TIME_BEGIN' , 4);
define('IGNORE_TIME_END' , 22);
define('IGNORE_DURATION_LOW' , 600);
define('IGNORE_DURATION_HIGH' , 10801);
define('IGNORE_REPEAT' , true);
/* 無視リスト読み込み */
if (file_exists(IGNORE_LIST_TITLE) === true) {
$ignore_titles = explode(',', trim(file_get_contents(IGNORE_LIST_TITLE)));
} else {
$ignore_titles = false;
}
/* 予約リスト読み込み */
if (file_exists(RESERVE_LIST_TITLE) === true) {
$reserve_titles = explode(',', trim(file_get_contents(RESERVE_LIST_TITLE)));
} else {
$reserve_titles = false;
}
/* 手動予約指定ファイル読み込み */
if (file_exists(RESERVE_LIST_EXTRA) === true) {
$reserve_extras = explode(PHP_EOL, trim(file_get_contents(RESERVE_LIST_EXTRA)));
} else {
$reserve_extras = array();
}
/* 環境設定 */
ini_set('user_agent', 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
/* 番組表の取得(仮) あとで:epgdumpr2 */
$html = (
trim(file_get_contents('http://tv.yahoo.co.jp/search/?q=&t=3&a=23&g=07&oa=1&s=1&d=' . date('Ymd'))) .
trim(file_get_contents('http://tv.yahoo.co.jp/search/?q=&t=3&a=23&g=07&oa=1&s=11&d=' . date('Ymd'))) .
trim(file_get_contents('http://tv.yahoo.co.jp/search/?q=&t=3&a=23&g=07&oa=1&s=21&d=' . date('Ymd'))) .
trim(file_get_contents('http://tv.yahoo.co.jp/search/?q=&t=3&a=23&g=07&oa=1&s=31&d=' . date('Ymd'))) .
trim(file_get_contents('http://tv.yahoo.co.jp/search/?q=&t=3&a=23&g=07&oa=1&s=41&d=' . date('Ymd')))
);
/* 配列初期化 */
$schedules = array();
$schedules_ignore = array();
$titles = array();
$times = array();
$channels = array();
$details = array();
$flags = array();
$urls = array();
$ex_times = array();
$ex_channels = array();
/* 番組データの抽出 */
$lines = explode(PHP_EOL, $html);
for ($i = 0; $i < count($lines); $i++) {
if (preg_match('/<p class="yjLS pb5p">.*<a href=".+">(.+)<\/a/', $lines[$i], $matche)) {
$titles[] = $matche[1];
if (preg_match('/^<p class="yjMS pb5p">(.+)<\/p>$/', $lines[($i + 2)], $matche)) {
$details[] = $matche[1];
} else {
$details[] = '';
}
}
if (preg_match('/<p><em>([0-9]+:[0-9]+.{1,8}[0-9]+:[0-9]+)<\/em>/', $lines[$i], $matche)) {
$times[] = str_replace('~', '-', $matche[1]);
}
if (preg_match('/<span class="pr35">(.{1,50})<\/span>/', $lines[$i], $matche)) {
$channels[] = $matche[1];
}
if (preg_match('/<p class="yjLS pb5p">(.*)<a href=".+">.+<\/a/', $lines[$i], $matche)) {
if (mb_strstr($matche[1], 'repeat') !== false) {
$flags[] = 'repeat';
} elseif (mb_strstr($matche[1], 'new') !== false) {
$flags[] = 'new';
} else {
$flags[] = '';
}
}
if (preg_match('/<p class="yjLS pb5p">.+href="(.+)">.+<\/a/', $lines[$i], $matche)) {
$urls[] = 'http://tv.yahoo.co.jp' . $matche[1];
}
}
for ($i = 0; $i < count($reserve_extras); $i++) {
list($start, $end, $channel, $title, $detail) = explode(',', $reserve_extras[$i]);
$times[] = $start . '-' . $end;
$channels[] = $channel;
$titles[] = $title;
$details[] = $detail;
$flags[] = 'extra';
$urls[] = NULL;
}
if (count($titles) === 0) exit();
/* test */
exec('atq', $test);
for ($i = 0; $i < count($test); $i++) {
$atqid = preg_replace('/^([0-9]+).*$/', '$1', $test[$i]);
exec('at -c ' . $atqid . ' | grep recfsusb2n', $at);
if ((count($at) !== 0) && (mb_strstr($at[0], 'recfsusb2') !== false)) {
exec('atrm ' . $atqid);
}
}
/* 番組ごとにループ */
for ($i = 0; $i < count($titles); $i++) {
$schedule_ignore = false;
/* チャンネルの識別 */
if ($flags[$i] !== 'extra') {
$channel_ignore = false;
switch ($channels[$i]):
case 'TOKYO MX1(地上波)':
case 'TOKYO MX2(地上波)':
$channel = 20;
$callsign = 'JOMX-DTV';
break;
case 'フジテレビ(地上波)':
$channel = 21;
$callsign = 'JOCX-DTV';
break;
case 'TBS1(地上波)':
case 'TBS2(地上波)':
$channel = 22;
$callsign = 'JORX-DTV';
break;
case 'テレビ東京1(地上波)':
case 'テレビ東京2(地上波)':
$channel = 23;
$callsign = 'JOTX-DTV';
break;
case 'テレビ朝日(地上波)':
$channel = 24;
$callsign = 'JOEX-DTV';
break;
case '日テレ1(地上波)':
case '日テレ2(地上波)':
$channel = 25;
$callsign = 'JOAX-DTV';
break;
default:
$channel_ignore = true;// 識別できないチャンネルは超無視
endswitch;
if ($channel_ignore === true) continue;// 超無視
} else {
list($channel, $callsign) = explode(':', $channels[$i]);
$channel = (int) $channel;
}
/* 放送時刻の識別 */
if ($flags[$i] !== 'extra') {
list($start , $end) = explode('-', $times[$i]);
list($start_h, $start_m) = explode(':', $start);
list($end_h , $end_m) = explode(':', $end);
if ($start_h >= 24) {
$start_h = $start_h - 24;
$start = $start_h . ':' . $start_m;
$start_u = strtotime(date('Y-m-d', strtotime('+1 day')) . ' ' . $start_h . ':' . $start_m . ':00');
} else {
$start_u = strtotime(date('Y-m-d') . ' ' . $start_h . ':' . $start_m . ':00');
}
if ($end_h >= 24) {
$end_h = $end_h - 24;
$end = $end_h . ':' . $end_m;
$end_u = strtotime(date('Y-m-d', strtotime('+1 day')) . ' ' . $end_h . ':' . $end_m . ':00');
} else {
$end_u = strtotime(date('Y-m-d') . ' ' . $end_h . ':' . $end_m . ':00');
}
$seconds = $end_u - $start_u;// 放送時間の計算(秒)
if (($start_h > IGNORE_TIME_BEGIN) && ($start_h < IGNORE_TIME_END)) $schedule_ignore = true;// 無視する時間帯
if (($seconds <= IGNORE_DURATION_LOW) || ($seconds >= IGNORE_DURATION_HIGH)) $schedule_ignore = true;// 無視する放送時間
} else {
list($start, $end) = explode('-', $times[$i]);
list($start_y, $start_mo, $start_d, $start_h, $start_m) = explode(':', $start);
list($end_y , $end_mo , $end_d , $end_h , $end_m) = explode(':', $end);
$start_u = strtotime($start_y . '-' . $start_mo . '-' . $start_d . ' ' . $start_h . ':' . $start_m . ':00');
$end_u = strtotime($end_y . '-' . $end_mo . '-' . $end_d . ' ' . $end_h . ':' . $end_m . ':00');
$seconds = $end_u - $start_u;// 放送時間の計算(秒)
}
/* 放送フラグの確認 */
$repeat = ($flags[$i] === 'repeat');
$new = ($flags[$i] === 'new');
$extra = ($flags[$i] === 'extra');
if ((IGNORE_REPEAT === true) && ($repeat === true)) {
$schedule_ignore = true;//再放送を無視する
}
/* タイトルの識別 */
$title = $titles[$i];
if ($extra === false) {
if ($ignore_titles !== false) {
foreach($ignore_titles as $key => $value) {
if (mb_stristr($title, $value) !== false) {
$schedule_ignore = true;// 無視する
}
}
}
if ($reserve_titles !== false) {
foreach($reserve_titles as $key => $value) {
if (mb_stristr($title, $value) !== false) {
$schedule_ignore = false;// 無視を解除
}
}
}
}
/* 録画時間の衝突を回避 */
if ((count($schedules) > 0) && ($extra === false)) {
$last = end($schedules);
if (0 > $start_u - $last['end']) $schedule_ignore = true;// 無視する
}
/* スケジュールデータ生成 */
$schedule = array(
'channel' => $channel,
'callsign' => $callsign,
'title' => $title,
'start' => $start_u,
'end' => $end_u,
'seconds' => $seconds,
'detail' => $details[$i],
'source' => $urls[$i],
'isRepeat' => $repeat,
'isNew' => $new,
'isExtra' => $extra
);
/* 録画予約 */
if ($schedule_ignore === false) {
//$filename= date('Y-m-d-Hi', $start_u) . '_' . $callsign . '_' . $title . '.m2ts';
$filename= date('Y-m-d-Hi', $start_u) . '_' . $callsign . '.m2ts';
$filepath= DIR_RECORDED . $filename;
$logpath = OUTPUT_COMMAND_RECORD;
$duration= $seconds - 10;//録画終了時刻を削る
$atdcom = 'at ' . date('H:i m/d/Y', ($start_u - 60)) . ' > /dev/null 2>&1';
//$command = "echo 'sleep 50;recfsusb2n -bv " . $channel . ' ' . ($seconds - 25) . ' "' . $filename
// . '" > ' . OUTPUT_COMMAND_RECORD . " 2>&1'"
// . '| ' . $atdcom;
$command = "echo '"
. "echo \"${filepath}\" > ${logpath}" . '\n'
. "sleep 50" . '\n'
. "timeout -s QUIT ${duration} recfsusb2n -bv ${channel} - - 2>> ${logpath}"
. "| tee \"${filepath}\""
. "| timeout -s KILL ${duration} cvlc - --sout \"#standard{access=http,mux=ts,dst=:20772/dtvts}\" > /dev/null 2>&1" . '\n'
. "exit 1" . '\n'
. "' | ${atdcom}";
exec($command);
$schedule['filename'] = $filename;
$schedule['filepath'] = $filepath;
$schedules[] = $schedule;
} else {
$schedules_ignore[] = $schedule;
continue;// 無視
}
}
file_put_contents(JSON_RESULT_SCHEDULE , json_encode($schedules));
file_put_contents(JSON_RESULT_SCHEDULE_IGNORE, json_encode($schedules_ignore));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.