2007年06月14日(Thu)
■ (gcal2text.pl改変)Tie::iCalでGoogle Calendarのデータ利用
塚本さんが作成されたgcal2text.plを改変してみました。
このコードは、次の問題点に対応してみました。
iCalには終日の予定は、例えば8月24日の予定は「開始:2006/08/24、終了:2006/08/25」、8月24日から8月25日の予定は「開始:2006/08/24、終了:2006/08/26」のように、終了が最終日の翌日で入ってきます。作成時に意図していたのは、8月24日の予定は「2006/08/24」とだけ出力することなのですが、これを私が考慮していないので「2006/08/24 - 2006/08/25」と出力されてしまいます。
gcal2text.pl(改変版)
use Date::Manip;
use Tie::iCal;
use Jcode;
# 出力文字コードの指定
my $jcode = 'sjis';
# ファイルの読み込み
my $file = shift(@ARGV);
my %events;
tie %events, 'Tie::iCal', $file || die qq(Can't read "$file".);
my @items = map { $events{$_}->[1] } keys(%events);
untie(%events);
# ソート、テキストへの変換
@items = sort { $a->{'DTSTART'}->[1] cmp $b->{'DTSTART'}->[1] } @items;
$jcode = undef unless (Jcode->can($jcode));
my @texts;
foreach my $item (@items) {
my $from_date = sprintf("%04d/%02d/%02d", $1, $2, $3) if ($item->{'DTSTART'}->[1] =~ /^(\d{4})(\d{2})(\d{2})/);
my $till_date = sprintf("%04d/%02d/%02d", $1, $2, $3) if ($item->{'DTEND'}->[1] =~ /^(\d{4})(\d{2})(\d{2})/);
my $from_time = sprintf(" %02d:%02d", $1, $2, $3) if ($item->{'DTSTART'}->[1] =~ /T(\d{2})(\d{2})(\d{2})/);
my $till_time = sprintf(" %02d:%02d", $1, $2, $3) if ($item->{'DTEND'}->[1] =~ /T(\d{2})(\d{2})(\d{2})/);
my $time;
#同じ日の予定(YYYY/MM/DD HH:MM - HH:MM)
if( $from_date eq $till_date ){
$time = "$from_date $from_time - $till_time";
#終日予定
} elsif(($from_time eq '') && ($till_time eq '')) {
$from = ParseDate($from_date);
$till = ParseDate($till_date);
$till = DateCalc($till, "-1 days");
$flg = Date_Cmp($from, $till);
#複数日終日予定 (YYYY/MM/DD - YYYY/MM/DD)
if($flg<0){
$till_date = sprintf("%04d/%02d/%02d",$1, $2, $3) if( $till =~ /^(\d{4})(\d{2})(\d{2})/);
$time = "$from_date - $till_date";
#1日のみの終日予定(YYYY/MM/DD)
} elsif($flg==0) {
$time = "$from_date";
}
#複数日予定(YYYY/MM/DD HH:MM - YYYY/MM/DD HH:MM)
} else {
$time = "$from_date $from_time - $till_date $till_time";
}
$time =~ s/ +/ /g;
my $subj = $item->{'SUMMARY'};
my $desc = $item->{'DESCRIPTION'};
$desc = '' if (ref($desc) eq 'ARRAY' and @{$desc} == 0);
$desc =~ s/\\n/\n/g if (not ref($desc));
$desc =~ s/(^\n+|\n+$)//gs;
my $text = "■ $subj ($time)\n\n$desc\n\n";
$text = jcode($text)->$jcode if ($jcode);
$text =~ s/\s+\n\n$/\n\n/s;
push(@texts, $text);
}
# 出力
print @texts;
[ツッコミを入れる]
