mumumuの日記: 本家ウォッチ番外編:テンプレート生成スクリプトver.0.0.0.1
日記 by
mumumu
#!/usr/bin/perl
# /.本家の一日分の記事を取得してファイルに書き込むスクリプト(ver 0.0.0.1)
# usage: perl getSlashArticle.pl [ date ]
# date format : yyyymmdd
# example : perl getSlashArticle.pl 20031204
#
# Author: mumumu(mumumu2ch@hotmail.com)
use strict;
use URI;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Cookies;
# 一時ファイル名を生成
my $rand = int(rand 65535) + 1;
# 年月日
my $yyyy = 0;
my $mm =0;
my $dd = 0;
# 引数チェック
checkParam( \@ARGV );
# 一時ファイル名を生成
my $rand = int(rand 65535) + 1;
my $tmpfile = "tmp".$ARGV[0].$rand.".txt";
# 出力するディレクトリ
my $dir = $ENV{"HOME"};
# slashdot.orgにログインする際のアカウント
my $account = "mumumu";
my $password = "xxxxxx";
# 最終的に生成するテンプレートファイル
my $templatefile = "$dir/$yyyy$mm$dd.txt";
print "output file : ", $templatefile, "\n\n";
# 取得するURL
my $uri = "http://slashdot.org/index.pl?light=1&issue=$ARGV[0]";
# Contentsを取得し、一時ファイルに書き込む
my $contents = getContents();
open ( TEMP, ">./$tmpfile" ) or die "cannot open tmpfile";
print TEMP $contents->content;
close( TEMP );
# 一時ファイルの中身をParseし、テンプレートを生成する
writeTemplate();
# ファイルを閉じて正常終了
system( "rm -rf ./$tmpfile" );
print "\nAll Process are finished successfully.\n";
exit( 0 );
sub checkParam{
my $input = shift;
my @mday = ( 31,28,31,30,31,30,31,31,30,31,30,31 );
# param number check
if( scalar @$input != 1 )
{
print "Error : no parameter specified( date yyyymmdd is required )\n";
exit (-1);
}
my $param = $input->[0];
print "Input param : ", $param, "\n\n";
# param length check
if( $param !~ /\d{8}/ )
{
print "Error: param's length must be 8(yyyymmdd)\n";
exit (-1);
}
$yyyy = substr( $param, 0, 4 );
$mm = substr( $param, 4, 2 );
$dd = substr( $param, 6, 2 );
# month Check
if( $mm < 1 || $mm > 13 )
{
print "Error : param is not valid( month value is 1-12 )\n";
exit (-1);
}
# 閏年なら2月の日数を29にする
$mday[1] = 29 if( $yyyy%4==0 && ($yyyy%100!=0 || $yyyy%400==0) );
# Day check
if( $dd < 1 || $dd > $mday[ $mm - 1 ] )
{
print "Error : param is not valid( day value is 1-$mday[ $mm - 1 ] )\n";
exit (-1);
}
}
sub getContents{
# リクエストオブジェクトを初期化
my $ua = LWP::UserAgent->new;
$ua->agent( "Contents Get Script/mozilla 5.0" );
my $req = HTTP::Request->new('GET',$uri);
# Cookieをセット
$ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
autosave => 1));
# ユーザーログイン
my $req1 = HTTP::Request->new( 'GET',
"http://slashdot.org/users.pl?op=userlogin&unickname=$account&upasswd=$password" );
my $res1 = $ua->request( $req1 );
print "Login to slashdot.org ....";
if ( $res1->is_error )
{
print "failed\n";
print "Error Response : ", $res1->status_line, "\n";
print "we could not Login to Site.\n";
exit( -1 );
}
print "ok\n";
# HTMLを取得
print "getting HTML with Light Mode....";
my $res = $ua->request( $req );
if ( $res->is_error )
{
print "failed\n";
print "Error Response : ", $res->status_line, "\n";
print "we could not get Contents\n";
exit( -1 );
}
print "ok\n";
# ログアウトする
my $req2 = HTTP::Request->new( 'GET', "http://slashdot.org/my/logout" );
my $res2 = $ua->request( $req2 );
print "Logout from Slashdot org.....";
if ( $res2->is_error )
{
print "failed\n";
print "Error Response : ", $res2->status_line, "\n";
print "we could not Logout from Slashdot.org\n";
exit( -1 );
}
print "ok.\n";
return $res;
}
sub writeTemplate{
print "writing template file.....";
# 必要なファイルを開く
open ( TEMPLATE, ">$templatefile" ) or die "cannot open Template File";
open ( TEMP, "./$tmpfile" ) or die "cannot open tmpfile";
my $mainflg = undef;
my $counter = 0;
# 一時ファイルの中身を解析しつつテンプレートファイルに書き込む
while ( my $line = <TEMP> )
{
# 始めのタイトルまで(広告その他)、または空行を読み飛ばす
if( $line =~ /^$/ || ( $line !~ /<H2>/ && !defined( $mainflg ) ) )
{
next;
}
# 始めのタイトル行(News for Nerds...)は無視
# されるが、ここでヘッダを描いておく
if( $line =~ /<H2>/ && $counter == 0 )
{
$counter++;
print TEMPLATE "本家ウォッチ:$yyyy年$mm月$dd日分<BR>\n";
print TEMPLATE "記事取得日 :".localtime()."<BR>\n";
print TEMPLATE "(注意)記事のタイムスタンプはJST時刻です。また、記事のしきい値は5です<BR>\n";
print TEMPLATE "(お願い)訳や内容の誤り等ありましたらご指摘をお願い致します。\n\n";
next;
}
# 2つ目以降のタイトル行は、それのみを取り出す
# ために特別扱いする
if( $line =~ /<H2>/ && $counter >= 1 )
{
$counter++;
$mainflg = 1;
# タイトルを書く
print TEMPLATE "\n\n<BR><BR>----------------------------------------------------<BR><BR>\n\n";
$line =~ s/(.*)<H2>(.*)<\/H2>/<LI><B>$2<\/B><P>/;
print TEMPLATE $line;
# セクション名は要らないため2行読み飛ばす
<TEMP>;<TEMP>;
next;
}
# スラッシュボックスも読み飛ばすためループを抜ける
if( $line =~ /^<H3>/ )
{
last;
}
# 投稿者の部分も日本語に置き換える
$line =~ s/<B>Posted by(.*) on (.*)<\/B><BR>/ $1 による, $2の投稿<BR>/;
# 部門名も日本語に置き換える
$line =~ s/<FONT SIZE="2"><B>from(.*)dept.<\/B><\/FONT><BR>/ $1 部門より<P>\n\n/;
# コメントの部分も日本語に置き換える
$line =~ s/<B><A HREF="(.*)">(\d*)<\/A> of (.*)<\/A><\/B> comments/<B><A HREF="$1">$2<\/A> \/ $3<\/A><\/B> コメント<B>)<\/B>\n/;
# Read More...を日本語に置き換える
$line =~ s/(.*)Read More...<\/B><\/A>/\n\n <B>(<\/B>$1もっと読む...<\/B><\/A>\n/g;
print TEMPLATE $line;
}
close( TEMP );
close( TEMPLATE );
print "success!!\n";
}
# /.本家の一日分の記事を取得してファイルに書き込むスクリプト(ver 0.0.0.1)
# usage: perl getSlashArticle.pl [ date ]
# date format : yyyymmdd
# example : perl getSlashArticle.pl 20031204
#
# Author: mumumu(mumumu2ch@hotmail.com)
use strict;
use URI;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Cookies;
# 一時ファイル名を生成
my $rand = int(rand 65535) + 1;
# 年月日
my $yyyy = 0;
my $mm =0;
my $dd = 0;
# 引数チェック
checkParam( \@ARGV );
# 一時ファイル名を生成
my $rand = int(rand 65535) + 1;
my $tmpfile = "tmp".$ARGV[0].$rand.".txt";
# 出力するディレクトリ
my $dir = $ENV{"HOME"};
# slashdot.orgにログインする際のアカウント
my $account = "mumumu";
my $password = "xxxxxx";
# 最終的に生成するテンプレートファイル
my $templatefile = "$dir/$yyyy$mm$dd.txt";
print "output file : ", $templatefile, "\n\n";
# 取得するURL
my $uri = "http://slashdot.org/index.pl?light=1&issue=$ARGV[0]";
# Contentsを取得し、一時ファイルに書き込む
my $contents = getContents();
open ( TEMP, ">./$tmpfile" ) or die "cannot open tmpfile";
print TEMP $contents->content;
close( TEMP );
# 一時ファイルの中身をParseし、テンプレートを生成する
writeTemplate();
# ファイルを閉じて正常終了
system( "rm -rf ./$tmpfile" );
print "\nAll Process are finished successfully.\n";
exit( 0 );
sub checkParam{
my $input = shift;
my @mday = ( 31,28,31,30,31,30,31,31,30,31,30,31 );
# param number check
if( scalar @$input != 1 )
{
print "Error : no parameter specified( date yyyymmdd is required )\n";
exit (-1);
}
my $param = $input->[0];
print "Input param : ", $param, "\n\n";
# param length check
if( $param !~ /\d{8}/ )
{
print "Error: param's length must be 8(yyyymmdd)\n";
exit (-1);
}
$yyyy = substr( $param, 0, 4 );
$mm = substr( $param, 4, 2 );
$dd = substr( $param, 6, 2 );
# month Check
if( $mm < 1 || $mm > 13 )
{
print "Error : param is not valid( month value is 1-12 )\n";
exit (-1);
}
# 閏年なら2月の日数を29にする
$mday[1] = 29 if( $yyyy%4==0 && ($yyyy%100!=0 || $yyyy%400==0) );
# Day check
if( $dd < 1 || $dd > $mday[ $mm - 1 ] )
{
print "Error : param is not valid( day value is 1-$mday[ $mm - 1 ] )\n";
exit (-1);
}
}
sub getContents{
# リクエストオブジェクトを初期化
my $ua = LWP::UserAgent->new;
$ua->agent( "Contents Get Script/mozilla 5.0" );
my $req = HTTP::Request->new('GET',$uri);
# Cookieをセット
$ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
autosave => 1));
# ユーザーログイン
my $req1 = HTTP::Request->new( 'GET',
"http://slashdot.org/users.pl?op=userlogin&unickname=$account&upasswd=$password" );
my $res1 = $ua->request( $req1 );
print "Login to slashdot.org ....";
if ( $res1->is_error )
{
print "failed\n";
print "Error Response : ", $res1->status_line, "\n";
print "we could not Login to Site.\n";
exit( -1 );
}
print "ok\n";
# HTMLを取得
print "getting HTML with Light Mode....";
my $res = $ua->request( $req );
if ( $res->is_error )
{
print "failed\n";
print "Error Response : ", $res->status_line, "\n";
print "we could not get Contents\n";
exit( -1 );
}
print "ok\n";
# ログアウトする
my $req2 = HTTP::Request->new( 'GET', "http://slashdot.org/my/logout" );
my $res2 = $ua->request( $req2 );
print "Logout from Slashdot org.....";
if ( $res2->is_error )
{
print "failed\n";
print "Error Response : ", $res2->status_line, "\n";
print "we could not Logout from Slashdot.org\n";
exit( -1 );
}
print "ok.\n";
return $res;
}
sub writeTemplate{
print "writing template file.....";
# 必要なファイルを開く
open ( TEMPLATE, ">$templatefile" ) or die "cannot open Template File";
open ( TEMP, "./$tmpfile" ) or die "cannot open tmpfile";
my $mainflg = undef;
my $counter = 0;
# 一時ファイルの中身を解析しつつテンプレートファイルに書き込む
while ( my $line = <TEMP> )
{
# 始めのタイトルまで(広告その他)、または空行を読み飛ばす
if( $line =~ /^$/ || ( $line !~ /<H2>/ && !defined( $mainflg ) ) )
{
next;
}
# 始めのタイトル行(News for Nerds...)は無視
# されるが、ここでヘッダを描いておく
if( $line =~ /<H2>/ && $counter == 0 )
{
$counter++;
print TEMPLATE "本家ウォッチ:$yyyy年$mm月$dd日分<BR>\n";
print TEMPLATE "記事取得日 :".localtime()."<BR>\n";
print TEMPLATE "(注意)記事のタイムスタンプはJST時刻です。また、記事のしきい値は5です<BR>\n";
print TEMPLATE "(お願い)訳や内容の誤り等ありましたらご指摘をお願い致します。\n\n";
next;
}
# 2つ目以降のタイトル行は、それのみを取り出す
# ために特別扱いする
if( $line =~ /<H2>/ && $counter >= 1 )
{
$counter++;
$mainflg = 1;
# タイトルを書く
print TEMPLATE "\n\n<BR><BR>----------------------------------------------------<BR><BR>\n\n";
$line =~ s/(.*)<H2>(.*)<\/H2>/<LI><B>$2<\/B><P>/;
print TEMPLATE $line;
# セクション名は要らないため2行読み飛ばす
<TEMP>;<TEMP>;
next;
}
# スラッシュボックスも読み飛ばすためループを抜ける
if( $line =~ /^<H3>/ )
{
last;
}
# 投稿者の部分も日本語に置き換える
$line =~ s/<B>Posted by(.*) on (.*)<\/B><BR>/ $1 による, $2の投稿<BR>/;
# 部門名も日本語に置き換える
$line =~ s/<FONT SIZE="2"><B>from(.*)dept.<\/B><\/FONT><BR>/ $1 部門より<P>\n\n/;
# コメントの部分も日本語に置き換える
$line =~ s/<B><A HREF="(.*)">(\d*)<\/A> of (.*)<\/A><\/B> comments/<B><A HREF="$1">$2<\/A> \/ $3<\/A><\/B> コメント<B>)<\/B>\n/;
# Read More...を日本語に置き換える
$line =~ s/(.*)Read More...<\/B><\/A>/\n\n <B>(<\/B>$1もっと読む...<\/B><\/A>\n/g;
print TEMPLATE $line;
}
close( TEMP );
close( TEMPLATE );
print "success!!\n";
}
本家ウォッチ番外編:テンプレート生成スクリプトver.0.0.0.1 More ログイン