テレビ王国がUSER-AGENTをみてる?PC, 徒然
LinuxでFriio録画 簡易延長対応(その2)(2009-11-18)以来、特に改変してこなかったepg_book.plだが、wgetでtvpidファイルの取得ができなくなった。
どうも、テレビ王国がHTTP_USER_AGENTを見て動作を変えているようで、IEやOpera,Google Chromeでは問題なく取得できる。
変えたのはwget部分に-U ""を追記しただけだが、前回から細かくいじったかもしれないのでソースを全部書いておく。
ちなみに、延長/変更対応も問題なく動いている…とは思うのだがその恩恵に浴していない。
(…テストではきちんと動作しているのだが、まだ延長/変更に遭遇していない。)
#!/usr/bin/perl
$rec_cmd = "cron_rec.pl";
$dir = "/work";
$book_file = "$dir/epg_book.cron";
$epg_chk = "$dir/epg_chk.log";
$path = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin";
$mailto = "MAILTO=\'\'";
use Encode;
use Time::Local;
$url = $ARGV[0];
if (!$ARGV[0]) {print "usage: epg_book.pl { check | url }\n"; exit;}
if ($ARGV[0] eq 'check') {print `nice -5 crontab -l`; &last; exit;}
`nice -5 wget -U "" -q -4 -O $dir/_epg_book.tvpi \"$url\"`;
`nice -5 nkf -e $dir/_epg_book.tvpi > $dir/_epg_book.tvpi.euc`;
open (IN, "$dir/_epg_book.tvpi.euc");
@lines = <IN>;
close (IN);
foreach $line(@lines){
$line =~ s/\r\n//;
if ($line =~ /multi-program-title: /) {print "nNOT Supported Multi-Program-Title\n"; exit;}
if ($line =~ /station: /){
($tmp, $ch) = split (/: /, $line);
if ($ch eq '') {print "ERROR NULLch\n$line\n"; exit;}
elsif ($ch =~ /DFS00400/) {$rec_ch = '27'; $ltr = 'NHKG';} # NHK総合
elsif ($ch =~ /DFS00408/) {$rec_ch = '26'; $ltr = 'ETV1';} # NHK教育1
elsif ($ch =~ /DFS0040A/) {$rec_ch = '26'; $ltr = 'ETV3';} # NHK教育3
elsif ($ch =~ /DFS07438/) {$rec_ch = '32'; $ltr = 'TVS';} # テレビ埼玉
elsif ($ch =~ /DFS00410/) {$rec_ch = '25'; $ltr = 'NTV';} # 日本テレビ
elsif ($ch =~ /DFS00428/) {$rec_ch = '24'; $ltr = 'EX';} # テレビ朝日
elsif ($ch =~ /DFS00418/) {$rec_ch = '22'; $ltr = 'TBS';} # TBSテレビ
elsif ($ch =~ /DFS00430/) {$rec_ch = '23'; $ltr = 'TX';} # テレビ東京
elsif ($ch =~ /DFS00420/) {$rec_ch = '21'; $ltr = 'CX';} # フジテレビ
elsif ($ch =~ /DFS05C38/) {$rec_ch = '20'; $ltr = 'MX';} # MXテレビ
elsif ($ch =~ /DFS00440/) {$rec_ch = '28'; $ltr = 'UD';} # 放送大学
elsif ($ch =~ /BSDT101/) {$rec_ch = 'B101'; $ltr = 'BS1';} # NHK BS1
elsif ($ch =~ /BSDT102/) {$rec_ch = 'B102'; $ltr = 'BS2';} # NHK BS2
elsif ($ch =~ /BSDT103/) {$rec_ch = 'B11'; $ltr = 'BShi';} # NHK BS hi
elsif ($ch =~ /BSDT141/) {$rec_ch = 'B8'; $ltr = 'BS4';} # BS日テレ
elsif ($ch =~ /BSDT151/) {$rec_ch = 'B1'; $ltr = 'BS5';} # BS朝日
elsif ($ch =~ /BSDT161/) {$rec_ch = 'B2'; $ltr = 'BS6';} # BS-TBS
elsif ($ch =~ /BSDT171/) {$rec_ch = 'B4'; $ltr = 'BS7';} # BSジャパン
elsif ($ch =~ /BSDT181/) {$rec_ch = 'B9'; $ltr = 'BS8';} # BSフジ
elsif ($ch =~ /BSDT191/) {$rec_ch = 'B3'; $ltr = 'BS9';} # WOWOW
elsif ($ch =~ /BSDT211/) {$rec_ch = 'B5'; $ltr = 'BS11';} # BS11
elsif ($ch =~ /BSDT222/) {$rec_ch = 'B7'; $ltr = 'BS12';} # TwellV
else {print "ERROR\n$ch\n"; exit;}
} # if
elsif ($line =~ /year: /){($tmp, $year) = split (/: /, $line);}
elsif ($line =~ /month: /){($tmp, $month) = split (/: /, $line);}
elsif ($line =~ /date: /){($tmp, $date) = split (/: /, $line);}
elsif ($line =~ /start: /){
($tmp, $start) = split (/: /, $line);
($SHH, $SMM) = split (/:/, $start);
} # elsif
elsif ($line =~ /end: /) {
($tmp, $end) = split (/: /, $line);
($EHH, $EMM) = split (/:/, $end);
} # elsif
elsif ($line =~ /program-id: /) {($tmp, $prog_id) = split (/: /, $line);}
elsif ($line =~ /program-title: /){($tmp, $title) = split (/: /, $line);}
elsif ($line =~ /program-subtitle: /){($tmp, $subtitle) = split (/: /, $line);}
} # foreach
$title = "$year-$month-$date $SHH$SMM $ltr $title";
if ($subtitle ne '') {$title = "$title ~ $subtitle";}
else {
if ($lines[$#lines] ne ' ') {
$ttl = "$title ~ $lines[$#lines]";
if ((length $ttl) < 99) {$title = $ttl;}
} # if
} # else
$title = decode('iso-2022-jp', $title);
$title =~ s/&#([0-9]+);/chr($1)/ge;
$title =~ s/&#[xX]([0-9A-Fa-f]+);/chr(hex $1)/ge;
$title = encode('euc-jp', $title);
$title =~ s/\\/¥/g;
$title =~ s/\////g;
$title =~ s/\,/,/g;
$title =~ s/\;/;/g;
$title =~ s/\:/:/g;
$title =~ s/\*/*/g;
$title =~ s/\?/?/g;
$title =~ s/\"/”/g;
$title =~ s/\</</g;
$title =~ s/\>/>/g;
$title =~ s/\|/|/g;
if ($EHH >= $SHH) {$rec_sec = ($EHH - $SHH)*3600 + ($EMM - $SMM)*60;}
elsif ($EHH < $SHH) {$rec_sec = ($EHH + 24 - $SHH)*3600 + ($EMM - $SMM)*60;}
else {print "ERROR\n$SHH\n$EHH\n"; exit;}
if (0 < $extend*60) {
$rec_sec = $rec_sec + $extend*60;
$title = "$title 延長$extend\分";
}
`nice -5 crontab -l > $book_file`;
print "\n$month月$date日 $SHH:$SMM~$EHH:$EMM $ch $title $prog_id\n";
$month--;
$tm = timelocal(0, $SMM, $SHH, $date, $month, $year);
($SMM, $SHH, $date, $month, $year) = (localtime($tm - 60))[1...5];
$month++;
$year = $year + 1900;
$program = "$SMM $SHH $date $month \* $rec_cmd $rec_ch $rec_sec \"$title\" $prog_id";
open FH, "$book_file";
@lines = <FH>;
close FH;
foreach $line(@lines) {
if ($line =~ /^PATH=/) {next;}
elsif ($line =~ /^MAILTO=/) {next;}
chomp $line;
if ($line =~ /$SMM $SHH $date $month \* $rec_cmd $rec_ch $rec_sec/) {
if ($line eq $program) {$del = 1; $del_line = $line; $change = 0; next;}
else {$change = 1;}
}
else {push @book, $line;}
} # foreach
if ($del != 1){push @book, $program;}
$YYYY = `nice -5 date "+%Y"`;
chomp $YYYY;
$M = `nice -5 date "+%m"`;
chomp $M;
$D = `nice -5 date "+%d"`;
chomp $D;
$now = `nice -5 date "+%M %H %d %m"`;
chomp $now;
push @book, $now;
if ($M == 12 && $D > 14){
foreach (@book){
$ret = shift @book;
$ret =~ s/([0-9]+\s[0-9]+\s[0-9]+)\s1\s/$1 13 /;
push @book, $ret;
} # foreach
} # if
elsif ($M == 1 && $D < 14){
foreach (@book){
$ret = shift @book;
$ret =~ s/([0-9]+\s[0-9]+\s[0-9]+)\s12\s/$1 0 /;
push @book, $ret;
} # foreach
} # if
@book = sort { (split(/ /,$a))[0] <=> (split(/ /,$b))[0] } @book;
@book = sort { (split(/ /,$a))[1] <=> (split(/ /,$b))[1] } @book;
@book = sort { (split(/ /,$a))[2] <=> (split(/ /,$b))[2] } @book;
@book = sort { (split(/ /,$a))[3] <=> (split(/ /,$b))[3] } @book;
if ($M == 12 && $D > 14){
foreach (@book){
$ret = shift @book;
$ret =~ s/([0-9]+\s[0-9]+\s[0-9]+)\s13\s/$1 1 /;
push @book, $ret;
} # foreach
} # if
elsif ($M == 1 && $D < 14){
foreach (@book){
$ret = shift @book;
$ret =~ s/([0-9]+\s[0-9]+\s[0-9]+)\s0\s/$1 12 /;
push @book, $ret;
} # foreach
} # if
$ret = '';
print "\n### 旧予約 ###\n";
until ($ret eq $now) {
$ret = shift @book;
if ($ret =~ /[0-9]+\s[0-9]+\s[\*]\s[\*]?\s[0-7]/){push @book, $ret;}
elsif ($ret != $now){print "$ret\n";}
} # until
print "\n### crontab ###\n";
open FH, ">$book_file";
print FH "$path\n";
print FH "$mailto\n";
foreach (@book){print FH "$_\n"; print "$_\n";}
close FH;
`nice -5 crontab $book_file`;
`nice -5 rm $dir/_epg_book*`;
if ($del == 1){
print "\n### 削 除 ###\n";
print "$del_line\n";
print "$title\n";
} # if
if ($del != 1 && $program != '' && $change == ''){
print "\n########## 予 約 ###########\n";
print "$program\n";
} # if
elsif ($del != 1 && $program != '' && $change == 1){
print "\n########## 変 更 ###########\n";
print "$program\n";
} # elsif
&last;
sub last {
print "\n### ディスク容量 ###\n";
$ret = `nice -5 df -h`;
print "$ret";
if (-s $epg_chk) {
print "\n### EPG不一致 ###\n";
print `nice -5 cat $epg_chk`. "\n";
} # if
} # sub last
exit;
