• バックアップ一覧
  • ソース を表示
  • サンプルプログラム/ウェブページ更新監視 は削除されています。
    • 1 (2007-03-29 (木) 11:55:47)

ウェブページ更新監視

 #!/usr/bin/perl
 
 # ============================================================================
 # ウェブページ更新監視プログラム
 # ============================================================================
 
 use strict;
 use Text::Diff;
 use Data::Dumper;
 use IO::File;
 use Date::Simple qw(today);
 use DateTime;              
 use LWP::UserAgent;
 use Getopt::Long;
 use Net::SMTP;
 use Jcode;
 use FindBin qw($Bin);
 
 # ============================================================================
 # 初期設定
 # ============================================================================
 # HTMLファイルを保存するディレクトリ
 my $save_dir = $Bin.'/save';
 # 取得するページのURL
 my %urls = (
 user_agent =>
 'http://www.nttdocomo.co.jp/service/imode/make/content/spec/useragent/index.html',
 screen_area =>
 'http://www.nttdocomo.co.jp/service/imode/make/content/spec/screen_area/index.html'
 );
 # 送信元
 my $from_mail = 'watch@'.`hostname`; chomp $from_mail;
 my $from_name = 'WATCH Prog';
 # 送信先
 my @to_mails = qw(
 foo@exapmle.com
 );
 # sendmailのパス
 my $sendmail_path = '/usr/sbin/sendmail';
 
 # ============================================================================
 # メイン
 # ============================================================================
 # ----------------------------------------------------------------------------
 # 前処理
 # ----------------------------------------------------------------------------
 umask 0111;
 
 # ----------------------------------------------------------------------------
 # 引数処理
 # ----------------------------------------------------------------------------
 my $debug;
 my $send_mail;
 GetOptions('debug' => \$debug,'send_mail' => \$send_mail);
 die "usage: $0 [--send_mail] [--debug]\n" if !$debug && !$send_mail;
 
 # ----------------------------------------------------------------------------
 # HTMLファイルを取得・保存
 # ----------------------------------------------------------------------------
 my $today = today();
 my $ua = LWP::UserAgent->new;
 $ua->agent("watch program");
 while (my ($name,$url) = each(%urls)) {
    my $req = HTTP::Request->new(get => $url);
    my $res = $ua->request($req);
    if ($res->is_success) {
        my $out = new IO::File "> $save_dir/${name}_${today}.html" or die $!;
        print $out $res->content;
        $out->close;
    } else {
        print $res->status_line, "\n";
    }
 }
 
 # ----------------------------------------------------------------------------
 # diffを実行
 # ----------------------------------------------------------------------------
 my %diff;
 while (my ($name,$url) = each(%urls)) {
    # 前日以前のHTMLファイルを探し、しばらく前まで日付を遡っても見つからない
    # ならエラー
    my $hit;
    my $yesterday = today() - 1;
    for (my $i=2,my $limiter=60;$i<$limiter;$i++,$yesterday = today() - $i) {
        if (-f "$save_dir/${name}_${yesterday}.html") {
            $hit = 1; last;
        }
    }
    die "sorry. no old day file of $name" unless $hit;
 
    # diffを実行
    my $diff = diff(
        "$save_dir/${name}_${yesterday}.html",
        "$save_dir/${name}_${today}.html",
        { STYLE => "Unified" });
    if ($diff) {
        $diff{$name} = Jcode->new($diff,'sjis')->euc;
    } else {
    }
 }
 
 # ----------------------------------------------------------------------------
 # メール送信
 # ----------------------------------------------------------------------------
 while (my ($name,$diff) = each(%diff)) {
    my $mesg = <<"END";
 the web page is modified. - $urls{$name}
 ---------------------------------------------------------------------------
 $diff
 END
    if ($send_mail) {
        send_mail( (
            from_mail   => $from_mail,
            from_name   => $from_name,
            to_mail     => [@to_mails],
            subject     => 'WATCH Prog',
            mesg        => $mesg,
            ));
    } elsif ($debug) {
        print "# ======================================================================\n";
        print "# $name\n";
        print "# $urls{$name}\n";
        print "# ======================================================================\n";
        print "$diff\n";
    }
 }
 
 exit 0;
 
 sub send_mail {
    my (%arg) = @_;
    my $from = sprintf '"%s"<%s>',
        jcode($arg{from_name})->mime_encode(),
        $arg{from_mail};
    my @to = (ref $arg{to_mail} eq 'ARRAY')? @{$arg{to_mail}} : ($arg{to_mail});
    my $to = join ',', @to;
    my $subject = Jcode->new($arg{subject},'euc')->mime_encode();
    my $dt = DateTime->now();
    my $date = sprintf "%s, %d %s %04d %02d:%02d:%02d +0900 (JST)",
        $dt->day_abbr, $dt->day, $dt->month_abbr, $dt->year,
        $dt->hour, $dt->minute, $dt->second;
    my $mesg = Jcode->new($arg{mesg},'euc')->jis;
 
    my $smtp = Net::SMTP->new('localhost');
 
    $smtp->mail($from);
    $smtp->to($to);
 
    # ヘッダ
    $smtp->data();
    $smtp->datasend("Date: $date\n");
    $smtp->datasend("From: $from\n");
    $smtp->datasend("To: $to\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("Content-Transfer-Encoding: 7bit\n");
    $smtp->datasend("Content-Type: text/plain;charset=\"ISO-2022-JP\"\n\n");
    $smtp->datasend("\n");
    # 本文
    $smtp->datasend("$mesg\n");
    # メール送信
    $smtp->dataend();
    $smtp->quit;
 }

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS