#!/usr/bin/perl
use File::Find ;
use HTML::Parse ;
use Jcode ;
use Getopt::Std ;
require "/usr/local/lib/site_perl/yukirss.pl" ; # Yuki::RSS
# 個別設定ファイルの読み込み
require "/var/www-support/rss/rss.conf" ;
# $jcode = 'sjis' ; # 出力する文字コード
# $ch_title = "タイトル" ; # チャンネル部のタイトル・説明
# $ch_desc = "サイトの説明" ;
# $exclude = "^\/var\/www\/(close|images)\/" ; # 除外するファイル
# $exts = '(\.php|\.htm|\.html|\.hnf)$' ; # 対象とするファイル拡張子
# $home = "http://tsaitoh.net/" ; # 起点URL
# $recent = 14 ; # 最新とみなす範囲
#
# # 検索対象とするディレクトリ名
# @dirs = ( "/var/www" , "/home/foo/public_html" , "/home/bar/diary" ) ;
#
# # ファイル名から著者名を取り出す正規表現
# %author = ( "^\/home\/foo\/" => "foo" , "^\/var\/www\/" => "www-data" ) ;
#
# # ファイル名からURLに書き換える正規表現
# %url = ( "^\/var\/www\/" => "$home" ,
# "^\/home\/foo\/" => "$home~foo/" ,
# "^\/home\/bar\/diary\/(\\d+)\/" => "$home~bar/diary/?" ) ;
# コマンドライン引数処理
getopts( "d:" ) ;
$recent = $opt_d if ( $opt_d ne "" ) ;
$xmlout = shift( @ARGV ) if ( @ARGV > 0 ) ;
%jcxml = (
'sjis' => 'Shift_JIS' ,
'euc' => 'EUC-JP' ,
'jis' => 'JIS' ,
'utf8' => 'UTF8' ,
'ucs2' => 'UCS2' ,
) ;
# チャンネル用のデータの文字コード変換
Jcode::convert( \$ch_title , $jcode ) ;
Jcode::convert( \$ch_desc , $jcode ) ;
# RSSのチャンネル情報
my $rss = new Yuki::RSS( version => '1.0' , encoding => $jcxml{$jcode} ) ;
$rss->channel(
title => $ch_title ,
link => "$home" ,
abount => $home."rss.xml" ,
description => $ch_desc ) ;
$now = time ;
# File::Find のコールバック関数
sub action {
@st = stat( $_ ) ;
if ( -f _
&& $File::Find::name !~ /$exclude/
&& $File::Find::name =~ /$exts/i
&& ($now - $st[9]) / 3600 / 24 < $recent
) {
$fdate = $st[9] ; # mtime
# ファイルの所有者をとりだす
$author = "nobody" ;
foreach $aure ( keys( %author ) ) {
if ( $File::Find::name =~ /$aure/ ) {
$author = $author{$aure} ;
last ;
}
}
# 絶対PATHを URL に書き換え
$url = "" ;
$desc = "" ;
foreach $ure1 ( keys ( %url ) ) {
$ure2 = $url{$ure1} ;
$ure3 = $File::Find::name ;
if ( $ure3 =~ s/$ure1/$ure2/ ) {
$url = $ure3 ;
if ( $File::Find::name =~ /\.hnf$/i ) {
# HNS 専用の書き換え
if ( open( HNF , $File::Find::name ) ) {
while( ) {
Jcode::convert( \$_ , 'euc' ) ;
if ( /^(NEW|LNEW\s+\S+)\s+(.*)$/ ) {
$desc = $2 ;
Jcode::convert( \$desc , $jcode ) ;
last ;
}
}
close( HNF ) ;
}
$url =~ s/d(\d+)\.hnf$/\1/ ;
} else {
# 通常の HTML ファイル
$h = main::parse_htmlfile( $File::Find::name ) ;
$tsdepth = -1 ;
$h->traverse(
sub { # コールバックの無名関数
my( $node , $start , $depth ) = @_ ;
if ( ref $node ) {
my $s = $node->starttag ;
if ( $s =~ //i ) {
$tsdepth = $start ? $depth : -1 ;
}
} elsif ( $tsdepth >= 0 ) {
$desc .= $node ;
}
1;
} ) ;
Jcode::convert( \$desc , $jcode ) ;
}
last ;
}
}
# print ">".$File::Find::name." => $author | $url\n" ;
@gt = localtime( $fdate ) ;
$rss->add_item( title => $desc ,
link => "$url" ,
description => '' ,
dc_date => sprintf( "%04d-%02d-%02dT%02d:%02d:%02d%s" ,
1900+$gt[5],1+$gt[4],
$gt[3],$gt[2],$gt[1],$gt[0],
"+09:00" ) ) ;
}
}
# ディレクトリ配下のファイルの検索を開始
find( \&action , @dirs ) ;
@{$rss->{items}} = sort { $b->{dc_date} cmp $a->{dc_date}; } @{$rss->{items}} ;
#print @rsssort ;
#exit(1);
#@{$rss->{items}} = @rsssort ;
# CGIでの呼び出しなら、ファイル種別を出力
print "Content-Type: application/xml\n\n"
if ( $0 =~ /\.cgi$/ ) ;
# 出来上がった RSS データの出力
if ( $xmlout eq "" ) {
print $rss->as_string ;
} else {
open( XML , ">$xmlout" ) or die( "Can't open file $_" ) ;
print XML $rss->as_string ;
close( XML ) ;
}
### Local Variables: ###
### mode: perl ###
### tab-width: 4 ###
### End: ###