Skip to content

Commit

Permalink
改造v0.01:放送中番組の情報取得
Browse files Browse the repository at this point in the history
  • Loading branch information
flysaki committed Aug 15, 2023
1 parent 9c2fbe9 commit ff86105
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
61 changes: 34 additions & 27 deletions htdocs/api/epginfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_once ('classloader.php');
require_once ('require.php');
require_once ('module.php');
require_once ('epginfo_tvmaid_maya.php');

// BonDriverとチャンネルを取得
list($BonDriver_dll, $BonDriver_dll_T, $BonDriver_dll_S, // BonDriver
Expand Down Expand Up @@ -52,33 +53,45 @@ function getEpgInfo($ch, $chnum, $sid, $onid, $tsid){

// -----------------------------------

if (!empty($EDCB_http_url)){
// チャンネル名
$channel_name = $ch[$chnum];

// 番組表 API
@$epg = simplexml_load_file($EDCB_http_url.'api/EnumEventInfo?onair=&onid='.$onid.'&sid='.$sid.'&tsid='.$tsid);
// 番組情報の取得
$pgFound = (function() use($channel_name) {
$epg = MayaEpg::getEpgData();
$pgFoundArr = array_filter($epg, function ($pg) use ($channel_name) {
return $pg['name'] === $channel_name;
});

// チャンネル名
if (isset($epg->items->eventinfo[0]->service_name)){
$channel = mb_convert_kana(strval($epg->items->eventinfo[0]->service_name), 'asv');
} else {
$channel = 'チャンネル名を取得できませんでした';
if(!count($pgFoundArr)){
return null;
}

return current($pgFoundArr);
})();


if ($pgFound){
//print_r($pgFound);
$epg = null;
// チャンネル名
$channel = $channel_name;
$program_name = $pgFound['title'] ?: '';


// 開始/終了時間の解析
$starttimestamp = MayaEpg::getTimeFromMayaTime($pgFound['start']); //タイムスタンプに変換
$duration = $pgFound['duration'];
$endtimestamp = MayaEpg::getTimeFromMayaTime($pgFound['end']); // 秒数を足す
// 表示用
$starttime = date("H:i", $starttimestamp);
$endtime = date("H:i", $endtimestamp);

// 現在の番組
if (isset($epg->items->eventinfo[0]->startTime)){
$starttime = $epg->items->eventinfo[0]->startDate.' '.$epg->items->eventinfo[0]->startTime;
} else {
$starttime = date('Y/m/d').'00:00:00';
}
if (isset($epg->items->eventinfo[0]->duration)){
$duration = $epg->items->eventinfo[0]->duration;
} else {
$duration = '0000';
}
if (isset($epg->items->eventinfo[0]->event_name)){
if ($program_name){
//文字列に変換してさらに半角に変換して改行をbrにする
$program_name = str_replace("\n", "<br>\n", mb_convert_kana(strval($epg->items->eventinfo[0]->event_name), 'asv'));
} else {
$program_name = str_replace("\n", "<br>\n", mb_convert_kana(strval($program_name), 'asv'));
} else { // TODO: 放送休止対応、次の番組対応 (23/08/15)
if (isset($epg->items->eventinfo[0]->service_name)){
$program_name = '放送休止';
} else {
Expand Down Expand Up @@ -128,12 +141,6 @@ function getEpgInfo($ch, $chnum, $sid, $onid, $tsid){
}
}

// 開始/終了時間の解析
$starttimestamp = strtotime($starttime); //タイムスタンプに変換
$endtimestamp = $starttimestamp + $duration; // 秒数を足す
$starttime = date("H:i", $starttimestamp);
$endtime = date("H:i", $endtimestamp);

// 次の番組の開始/終了時間の解析
$next_starttimestamp = strtotime($next_starttime); //タイムスタンプに変換
$next_endtimestamp = $next_starttimestamp + $next_duration; // 秒数を足す
Expand Down
35 changes: 35 additions & 0 deletions htdocs/api/epginfo_tvmaid_maya.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
class MayaEpg{
public static array $data = [];

static function getEpgDataFromMayaDb(){
$sqlTimeStampStr = (time() + 62135629200) . '0000000';

$PDO = new PDO('sqlite:../../TvMaid30/TvmaidMAYA/user/tvmaid-5.db');
$stmt = $PDO->prepare("select service.fsid, name, title, desc, start, end, duration from service
left join (
select id, fsid, title, desc, start, end, duration from event
where start < ? and end > ?)
as _event on service.fsid = _event.fsid
where driver = (select driver from tuner where name = 'siano') order by service.id"
);
$stmt->execute([$sqlTimeStampStr, $sqlTimeStampStr]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

static function fetchEpgData(){
self::$data = self::getEpgDataFromMayaDb();
}

public static function getEpgData(){
if(!self::$data){
self::fetchEpgData();
}

return self::$data;
}

public static function getTimeFromMayaTime($mayaTime){
return intval($mayaTime) / 10000000 - 62135629200;
}
}

0 comments on commit ff86105

Please sign in to comment.