#!/usr/bin/perl # # Anime recording system foltia # http://www.dcc-jpl.com/soft/foltia/ # # しょぼいカレンダーに予約録画状況を送信するスクリプト # by yuaaa ( http://d.hatena.ne.jp/yuaaa ) # # http://syobocal.g.hatena.ne.jp/keyword/sch_upload # # #Usage: foltia_sch_upload.pl [slot] # user/pass : for cal.syoboi.jp # slot : 0-3 (default: 0) # ### Config Start #何時間分の予定をアップロードするか $cfg_uploadSchedule = 7 *24; #(Hour) #アップロードする際に使用する「デバイス名」 $cfg_deviceName = 'foltia'; #EPG録画の予定を送信する際に、予約を入れたアカウントの名前を「デバイス名」に使用するか #(注意)アップロードされたデータは、しょぼいカレンダーにて公開されます $cfg_useEnvAccountName = 1; # devcolors として渡す文字列。 ## デバイス名と色の設定。 ##「{デバイス名}=#RRGGBB」または「#RRGGBB」の形式で記述し、タブ区切りで複数指定。 $cfg_devcolorsString = ""; #PIDを渡すurl。{XID}がpidに置換されます。delepgp.phpのURLを指定しておくとEPG予約の解除が可能です $cfg_EpgUrl = 'http://foltia.FIXME.example/delepgp.php?pid={XID}'; $cfg_UserAgent = "foltia_sch_upload/0.0.2"; $cfg_UploadUrl = 'http://cal.syoboi.jp/sch_upload'; $cfg_DBEncode = 'euc-jp'; ### Config End use LWP::UserAgent; use HTTP::Request::Common; use Encode; use DBI; use DBD::Pg; use Time::Local; $path = $0; $path =~ s/foltia_sch_upload.pl$//i; if ($pwd ne "./"){ push( @INC, "$path"); } require "foltialib.pl"; #引数チェック $cal_user = $ARGV[0]; $cal_pass = $ARGV[1]; if ($cal_user eq "" || $cal_pass eq "" ){ #引数なしで実行されたら、終了 print "Usage: foltia_sch_upload.pl [slot]\n"; print " user/pass ... for cal.syoboi.jp\n"; print " slot ... 0-3 (default: 0)\n"; exit; } $slot = $ARGV[2]; if ($slot eq "") { $slot = "0"; } #DB初期化 my $data_source = sprintf("dbi:%s:dbname=%s;host=%s;port=%d", $DBDriv,$DBName,$DBHost,$DBPort); $dbh = DBI->connect($data_source,$DBUser,$DBPass) ||die $DBI::error;; $folnow = epoch2foldate( time() ); $folend = calcoffsetdate( $folnow, $cfg_uploadSchedule*60 ); $DBQuery = <<"SQL"; SELECT foltia_station.stationname, foltia_subtitle.tid, foltia_program.title, foltia_subtitle.pid, foltia_subtitle.countno, foltia_subtitle.subtitle, foltia_subtitle.startdatetime, foltia_subtitle.enddatetime, NULL FROM foltia_subtitle, foltia_program, foltia_station, foltia_tvrecord WHERE foltia_tvrecord.tid = foltia_program.tid AND foltia_tvrecord.stationid = foltia_station.stationid AND foltia_program.tid = foltia_subtitle.tid AND foltia_station.stationid = foltia_subtitle.stationid AND foltia_subtitle.enddatetime >= '$folnow' AND foltia_subtitle.enddatetime <= '$folend' UNION SELECT foltia_station.stationname, foltia_subtitle.tid, foltia_program.title, foltia_subtitle.pid, foltia_subtitle.countno, foltia_subtitle.subtitle, foltia_subtitle.startdatetime, foltia_subtitle.enddatetime, foltia_envpolicy.name FROM foltia_tvrecord LEFT OUTER JOIN foltia_subtitle on (foltia_tvrecord.tid = foltia_subtitle.tid ) LEFT OUTER JOIN foltia_program on (foltia_tvrecord.tid = foltia_program.tid ) LEFT OUTER JOIN foltia_station on (foltia_subtitle.stationid = foltia_station.stationid ) LEFT OUTER JOIN foltia_envpolicy on (foltia_subtitle.epgaddedby = foltia_envpolicy.memberid ) WHERE foltia_tvrecord.stationid = 0 AND foltia_station.stationrecch != -10 AND foltia_subtitle.enddatetime >= '$folnow' AND foltia_subtitle.enddatetime <= '$folend' ORDER BY startdatetime ASC LIMIT 200 SQL $sth = $dbh->prepare($DBQuery); $sth->execute(); $post = ""; while ( @rsv= $sth->fetchrow_array() ) { $line = foldate2epoch( $rsv[6] ) . "\t" . foldate2epoch( $rsv[7] ) ; $line .= "\t"; $utf8_title = &cleanString($rsv[2]); $utf8_subtitle = &cleanString($rsv[5]); $utf8_stname = &cleanString($rsv[0]); $utf8_envname = &cleanString($rsv[8]); if ( $rsv[1] != 0 ) { #TID $line .= $cfg_deviceName; $line .= "\t"; $line .= $utf8_title; $line .= "\t"; $line .= $utf8_stname; $line .= "\t"; if ( $rsv[4] > 0 ) { $line .= "#" . $rsv[4] . " "; } $line .= $utf8_subtitle; $line .= "\t"; } else { if ( $utf8_envname eq "" || ! $cfg_useEnvAccountName ) { $line .= $cfg_deviceName; } else { $line .= $utf8_envname; } $line .= "\t"; $line .= $utf8_subtitle; $line .= "\t"; $line .= $utf8_stname; $line .= "\t"; $line .= ''; $line .= "\t"; } $line .= "0\t"; $line .= $rsv[3]; $post .= $line . "\n"; } my $req = POST( $cfg_UploadUrl, [data => $post, slot => $slot, epgurl => $cfg_EpgUrl, devcolors => $cfg_devcolorsString ] ); $req->authorization_basic($cal_user, $cal_pass); my $ua = LWP::UserAgent->new; $ua->agent( $cfg_UserAgent ); my $res = $ua->request($req); print STDERR $res->as_string; sub cleanString { $str = $_[0]; $str =~ s/[\t\r\n]/ /g; Encode::from_to($str, $cfg_DBEncode, 'utf-8'); return $str; }