#!/bin/sh #from EPG xml data to iEPG version=0.1.8-pre7 export LANG=ja_JP.UTF-8 xml_esc() { a=${1//'&apos;'/\'} a=${a//'&amp;'/\&} a=${a//'&lt;'/\<} a=${a//'&gt;'/\>} a=${a//'&quot;'/\"} echo "$a" return 0 } sjsafe() { echo "$1" |iconv -f UTF-8 -t $iepg_charset >/dev/null 2>&1 ;iv=$? if [ $iv = 0 ];then xml_esc "$1" return 0 fi sjf_list="Ⅰ Ⅱ Ⅲ Ⅳ Ⅴ Ⅵ Ⅶ Ⅷ Ⅸ Ⅹ ① ② ③ ④ ⑤ ⑥ ⑦ ⑪ - ⁉" sjf_sub=(I II III IV V VI VII VIII IX X \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(11\) ‐ !?) s_str="" n=0 while [ "${1:$n:1}" ];do c="${1:$n:1}" i=0 sj_ok=1 for s in $sjf_list ;do if [ "$s" = "$c" ];then s_str="$s_str${sjf_sub[$i]}" sj_ok=0 break fi i=$(($i+1)) done if [ $sj_ok = 1 ];then echo "$c" |iconv -f UTF-8 -t $iepg_charset >/dev/null 2>&1 ;iv=$? if [ $iv = 0 ];then s_str="$s_str$c" else s_str="${s_str}?" fi fi n=$(($n+1)) done xml_esc "$s_str" return 0 } if [ -f /etc/iepgd.conf ];then . /etc/iepgd.conf fi is_root=${is_root:-/var/www/html} st_id_list=${st_id_list:-$is_root/STATION.ID} org_charset=shift_jis iepg_charset=${iepg_charset:-$org_charset} if [ "$QUERY_STRING" ];then iepg_id=${QUERY_STRING#id=} else iepg_id=$1 fi st_id_iepg=${iepg_id:0:6} pref_id=`grep ^$st_id_iepg: $st_id_list |cut -d':' -f4` if [ "$pref_id" ];then if [ $pref_id = 1 ];then pref_id=23 elif [ $pref_id = 2 ];then pref_id=40 elif [ $pref_id = 3 ];then pref_id=33 elif [ $pref_id = 4 ];then pref_id=10 elif [ $pref_id = 5 ];then pref_id=45 elif [ $pref_id = 6 ];then pref_id=48 fi xml_dir=$is_root/pref.$pref_id ls $xml_dir >/dev/null 2>&1 ;pref_id_v=$? if [ $pref_id_v = 0 ];then pref_id_err=0 else pref_id_err=1 fi else pref_id_err=2 fi id_err=0 if [ "${iepg_id//[0-9]/}" ]||[ ${#iepg_id} != 18 ]||[ $pref_id_err != 0 ];then err_title="iEPG error" err_pref="iEPG error occured" if [ "$iepg_id" ];then if [ "${iepg_id//[0-9]/}" ];then if [ "$iepg_id" = admin ];then err_title="administrator mode" err_pref="iepgd.cgi outputs environment variables" err_re="$err_title<br><pre>`export`</pre>" else id_err=1 err_re="invalid value" fi err_re="$iepg_id, $err_re" else id_err=2 if [ ${#iepg_id} -gt 18 ];then err_re="$iepg_id, value too long" elif [ ${#iepg_id} -lt 18 ];then err_re="$iepg_id, value too short" elif [ $pref_id_err = 1 ];then err_re="${iepg_id:0:6}, this station ID is unsupported" elif [ $pref_id_err = 2 ];then err_re="${iepg_id:0:6}, unknown station ID" else err_re="${iepg_id:0:6}, unknown error" fi fi else id_err=3 err_re="value not set" fi echo "$err_re" >&2 ex_YmdHM=`date '+%Y-%m-%d %H:%M'` err_mesg="<!DOCTYPE html>\r\n<html><head><title>$err_title</title></head>" err_mesg=$err_mesg"<body>\r\n$ex_YmdHM: $err_pref<br>\r\n" err_mesg=$err_mesg"REMOTE_ADDR=$REMOTE_ADDR:iepg_id=$iepg_id<br>\r\n" err_mesg=$err_mesg"$err_re<br>\r\n<br>\r\n" err_mesg=$err_mesg"${0##*/}-$version</body></html>\r\n" content_length_wc=`echo -ne "$err_mesg" |wc -c` echo -ne "Content-Length: $content_length_wc\r\n" echo -ne "Connction: close\r\n" echo -ne "Content-type: text/html\r\n\r\n" echo -ne "$err_mesg" exit $id_err fi rqs_st=${iepg_id:6:12} rqs_Ymd=${rqs_st:0:8} if [ ${st_id_iepg:0:1} = 1 ];then sc=$st_id_iepg ch_ext="$st_id_iepg" elif [ ${st_id_iepg:0:1} = 2 ];then st_id_xbs=4${st_id_iepg:3:3} sc=bs ch_ext="$st_id_xbs.epgdata.ontvjapan" else sc=$st_id_iepg ch_ext="$st_id_iepg" fi all_xml=`echo $xml_dir/epg.*.$sc.xml` #last_xml=`ls -r $xml_dir/epg.*.$sc.xml |head -1` last_xml=${all_xml##* } last_p=`grep \<prog.*channel=\"${ch_ext} $last_xml |tail -1` last_date=${last_p#*stop=\"} last_date=${last_date%% *} last_YmdHM=${last_date:0:12} #first_xml=`ls $xml_dir/epg.*.$sc.xml |head -1` first_xml=${all_xml%% *} first_p=`grep -m 1 \<prog.*channel=\"${ch_ext} $first_xml` if [ "$first_p" ];then first_date=${first_p#*start=\"} first_date=${first_date%% *} first_YmdHM=${first_date:0:12} else first_ch_list_data=`grep :$st_id_iepg: $xml_dir/ch.list.$pref_id.* 2>/dev/null |head -n 1` if [ "$first_ch_list_data" ];then first_ch_list=${first_ch_list_data%%:*} first_ch_list_date=${first_ch_list##*.} first_YmdHM=${first_ch_list_date}0400 else first_YmdHM=$rqs_st fi fi xml_ok=0 epg_ok=0 st_id_ok=0 exp_err=0 if [ $rqs_st -lt $first_YmdHM ];then org_xml="${first_xml##*/}" exp_err=1 elif [ $rqs_st -ge $last_YmdHM ];then org_xml="${last_xml##*/}" exp_err=2 else out_Ymd=`date -d "-10day $rqs_Ymd" '+%Y%m%d'` xml_list=`ls -r $xml_dir/epg.*.$sc.xml 2>/dev/null |grep -B 20 epg.$out_Ymd.*.$sc.xml` if [ ! "$xml_list" ];then for t in `ls -r $xml_dir/epg.*.$sc.xml 2>/dev/null |cut -d'.' -f3` ;do check_Ymd=${t:0:8} if [ $check_Ymd -lt $out_Ymd ];then out_Ymd=$check_Ymd break fi done xml_list=`ls -r $xml_dir/epg.*.$sc.xml 2>/dev/null |grep -B 20 epg.$out_Ymd.*.$sc.xml` if [ ! "$xml_list" ];then xml_list=`ls -r $xml_dir/epg.*.$sc.xml 2>/dev/null` fi fi for t in $xml_list ;do first_p=`grep -m 1 programme.*start.*$ch_ext $t` last_p=`grep programme.*start.*$ch_ext $t |tail -1` first_date=${first_p#*start=\"} first_date=${first_date%% *} first_YmdHM=${first_date:0:12} last_date=${last_p#*stop=\"} last_date=${last_date%% *} last_YmdHM=${last_date:0:12} if [ $rqs_st -ge $first_YmdHM ]&&[ $rqs_st -lt $last_YmdHM ];then org_xml=$t xml_ok=1 break fi done while read t ;do st_id_ok=1 epg_st=${t#*\"} epg_st=${epg_st%% *} epg_et=${t#*stop=\"} epg_et=${epg_et%% *} if [ ${epg_st:-0} -le ${rqs_st}00 ]&&[ ${rqs_st}00 -lt ${epg_et:-0} ];then epg_data=`grep -A5 start=\"$epg_st.*'="'$ch_ext $org_xml` epg_ok=1 prog_title=`echo "$epg_data" |grep '<title lang="ja_JP'` prog_title=${prog_title#*\>} prog_title=${prog_title%\<*} prog_title=`sjsafe "$prog_title"` ex_prog_YmdHM=${epg_st:0:12} end_YmdHM=${epg_et:0:12} abs_data=`echo "$epg_data" |grep '<desc lang="ja_JP">'` abs_data=${abs_data#*\>} abs_data=${abs_data%\<*} abs_data=`sjsafe "$abs_data"` break fi done <<EOF_jTf55 `grep \<prog.*channel=\"${ch_ext} $org_xml` EOF_jTf55 fi if [ $epg_ok = 0 ];then if [ $exp_err = 1 ];then err_re="${iepg_id:6:12}, program before $first_YmdHM" elif [ $exp_err = 2 ];then err_re="${iepg_id:6:12}, program after $last_YmdHM" else if [ $xml_ok = 0 ];then err_re="xml file not found" else if [ $st_id_ok = 0 ];then err_re="$st_id_iepg unknown station ID" else epg_ok=2 err_re="EPG data not found" while read t ;do st_id_ok=1 epg_st=${t#*\"} epg_st=${epg_st%% *} epg_et=${t#*stop=\"} epg_et=${epg_et%% *} if [ ${epg_et:-0} -ge ${rqs_st}00 ];then err_start_date=$epg_st break fi done <<EOF_iepg_54812 `grep \<prog.*channel=\"${ch_ext} $org_xml` EOF_iepg_54812 while read t ;do st_id_ok=1 epg_st=${t#*\"} epg_st=${epg_st%% *} epg_et=${t#*stop=\"} epg_et=${epg_et%% *} if [ ${epg_st:-0} -lt ${rqs_st}00 ];then err_end_date=$epg_et break fi done <<EOF_iepg_54812 `tac $org_xml |grep \<prog.*channel=\"${ch_ext}` EOF_iepg_54812 ex_prog_YmdHM=$err_start_date end_YmdHM=$err_end_date prog_title="unknown program" abs_data="$err_re" fi fi fi fi if [ $epg_ok = 0 ];then ex_YmdHM=`date '+%Y-%m-%d %H:%M'` err_mesg="$ex_YmdHM: iEPG error occured. $err_re<br>\r\n\ REMOTE_ADDR=$REMOTE_ADDR:iepg_id=$iepg_id<br>\r\n\ st_id_iepg=$st_id_iepg: $first_YmdHM - $last_YmdHM: st_id_ok=$st_id_ok<br>\r\n\ xml_ok=$xml_ok: org_xml=$org_xml: epg_ok=$epg_ok\r\n" err_return="<!DOCTYPE html>\r\n <html><head><title>iEPG error</title></head><body>\r\n $err_mesg</body></html>\r\n" content_length_wc=`echo -ne "$err_return" |wc -c` echo -ne "Content-Length: $content_length_wc\r\n" echo -ne "Connction: close\r\n" echo -ne "Content-type: text/html\r\n\r\n" echo -ne "$err_return" exit 1 fi ############### iepg return ############### iepg_st_id=`grep $st_id_iepg $st_id_list |cut -d':' -f3` iepg_st_id=${iepg_st_id#*:} if [ "$epg_data" ];then prog_genre=`echo "$epg_data" |grep cat.*en'">'` prog_genre=${prog_genre#*\>} prog_genre=${prog_genre%\<*} genre_list="news:69 information:144 etc:153 sports:16 music:32 variety:96 anime:112 drama:48 cinema:80" prog_gcode=`echo "$genre_list" |grep $prog_genre |cut -d':' -f2` else prog_gcode=153 fi iepg_return="Content-type: application/x-tv-program-info; charset=$org_charset\r\n\ version: 1\r\n\ station: $iepg_st_id\r\n\ year: ${ex_prog_YmdHM:0:4}\r\n\ month: ${ex_prog_YmdHM:4:2}\r\n\ date: ${ex_prog_YmdHM:6:2}\r\n\ start: ${ex_prog_YmdHM:8:2}:${ex_prog_YmdHM:10:2}\r\n\ end: ${end_YmdHM:8:2}:${end_YmdHM:10:2}\r\n\ program-title: $prog_title\r\n\ genre: ${prog_gcode:-153}\r\n\ subgenre: ${genre_id#*:}\r\n\ \r\n\ $abs_data" content_length_wc=`echo -ne "$iepg_return" |iconv -f utf-8 -t $iepg_charset |wc -c` echo -ne "Content-Length: $content_length_wc\r\n" echo -ne "Connection: close\r\n" echo -ne "Content-type: application/x-tv-program-info; charset=$iepg_charset\r\n\r\n" echo -ne "$iepg_return" |iconv -f utf-8 -t $iepg_charset exit 0 --- txt.cgi-0.1.5