Changeset 11371

Show
Ignore:
Timestamp:
05/11/08 00:58:25 (5 years ago)
Author:
hio
Message:

Auto::FetchTitle::Plugin::ExtractHeading?, tiarra.conf でも抽出パターンを記述できるように.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/tiarra/trunk/module/Auto/FetchTitle/Plugin/ExtractHeading.pm

    r10891 r11371  
    2626  my $pkg   = shift; 
    2727  my $this = $pkg->SUPER::new(@_); 
     28 
     29  $this->{extra} = undef; 
     30  $this->_parse_extra_config(); 
     31 
    2832  $this; 
    2933} 
     
    4549 
    4650# ----------------------------------------------------------------------------- 
     51# $this->_parse_extra_config(). 
     52# parse user config. 
     53# 
     54sub _parse_extra_config 
     55{ 
     56  my $this = shift; 
     57  my @config; 
     58  $this->{extra} = \@config; 
     59 
     60  $this->notice(__PACKAGE__."#_parse_extra_config"); 
     61  $this->notice(">> ".join(", ", map{split(' ', $_)}$this->config->extra('all'))); 
     62 
     63  foreach my $token (map{split(' ', $_)}$this->config->extra('all')) 
     64  { 
     65     $this->notice("extra: $token"); 
     66     my $name  = "extra-$token"; 
     67     my $block = $this->config->$name; 
     68     if( !$block ) 
     69     { 
     70       $this->notice("no such extra config: $name"); 
     71       next; 
     72     } 
     73     if( !ref($block) ) 
     74     { 
     75       my $literal = $block; 
     76       $block = Configuration::Block->new($name); 
     77       $block->extract($literal); 
     78     } 
     79     my $has_param; 
     80     my $config = {}; 
     81     $config->{name} = $name; 
     82     $config->{url}  = $block->url; 
     83     if( !$config->{url} ) 
     84     { 
     85       $this->notice("no url on $name"); 
     86       next; 
     87     } 
     88     if( my $recv_limit = $block->get('recv_limit') ) 
     89     { 
     90       while( $recv_limit =~ s/^\s*(\d+)\*(\d+)/$1*$2/e ) 
     91       { 
     92       } 
     93       $config->{recv_limit} = $recv_limit; 
     94       $has_param = 1; 
     95     } 
     96     my @extract; 
     97     foreach my $line ($block->extract('all')) 
     98     { 
     99       $has_param ||= 1; 
     100       my $type; 
     101       my $value = $line; 
     102       if( $value =~ s/^(\w+)(:\s*|\s+)// ) 
     103       { 
     104         $type = $1; 
     105       } 
     106       $type ||= 're'; 
     107       if( $type eq 're' ) 
     108       { 
     109         $value =~ s{^/(.*)/(\w*)\z/}{(?$2:$1)}; 
     110         my $re = eval{ 
     111           local($SIG{__DIE__}) = 'DEFAULT'; 
     112           qr/$value/s; 
     113         }; 
     114         if( my $err = $@ ) 
     115         { 
     116           chomp $err; 
     117           $this->notice("invalid regexp $re on $name, $err"); 
     118           next; 
     119         } 
     120         push(@extract, $re); 
     121       }else 
     122       { 
     123         $this->notice("unknown extract type $type on $name"); 
     124         next; 
     125       } 
     126     } 
     127     if( @extract ) 
     128     { 
     129       $config->{extract} = @extract==1 ? $extract[0] : \@extract; 
     130     } 
     131     if( keys %$config==1 ) 
     132     { 
     133       $this->notice("no config on $name"); 
     134       next; 
     135     } 
     136     push(@config, $config); 
     137  } 
     138 
     139  $this; 
     140} 
     141 
     142# ----------------------------------------------------------------------------- 
    47143# $this->_config(). 
    48144# config for extract-heading. 
     
    50146sub _config 
    51147{ 
     148  my $this = shift; 
    52149  my $config = [ 
     150    @{$this->{extra}}, 
    53151    { 
    54152      # 1. ぷりんと楽譜. 
     
    75173    }, 
    76174    { 
    77       # 4. nhkニュース. 
     175      # 4a. nhkニュース. 
    78176      url     => 'http://www*.nhk.or.jp/news/*', 
    79177      extract => qr{<p class="newstitle">(.*?)</p>}, 
     178    }, 
     179    { 
     180      # 4b. nhk関西のニュース. 
     181      url        => 'http://www*.nhk.or.jp/*/lnews/*', 
     182      recv_limit => 8*1024, 
     183      extract    => qr{<h3>(.*?)</h3>}, 
    80184    }, 
    81185    { 
     
    147251      extract    => qr{<FONT class=TITLE>(.*?)</FONT>}s, 
    148252    }, 
     253    { 
     254      # 15. game? 
     255      url        => 'http://splax.net/jun.html?p=*', 
     256      extract    => sub{ 
     257        my $req = shift; 
     258        if( $req->{url} =~ /\?p=([^&;=#]+)/ ) 
     259        { 
     260          my $q = $1; 
     261          $q =~ s/%([0-9A-F]{2})/pack("H*",$1)/gie; 
     262          $q =~ s/\*([0-9A-F]{2})/pack("H*",$1)/gie; 
     263          $q = Unicode::Japanese->new($q, "sjis")->utf8; 
     264          $q =~ s/\*.*//; 
     265          $q = "「$qの唄」"; 
     266        }else 
     267        { 
     268          return; 
     269        } 
     270      }, 
     271    }, 
     272    { 
     273      # 16. recordchina. 
     274      url        => 'http://www.recordchina.co.jp/group/*', 
     275      recv_limit => 12*1024, 
     276      extract    => qr{<div id="news_detail_title" class="ft04">(.*?)</div>}s, 
     277    }, 
    149278  ]; 
    150279  $config; 
     
    219348 
    220349    my $extract_list = $conf->{extract}; 
     350    if( !$extract_list ) 
     351    { 
     352      $DEBUG and $ctx->_debug($req, "debug: - - no extract"); 
     353      next; 
     354    } 
    221355    if( ref($extract_list) ne 'ARRAY' ) 
    222356    { 
     
    292426        RT 
    293427 
     428=begin tiarra-doc 
     429 
     430info:    本文から見出しを抽出するFetchTitleプラグイン. 
     431default: off 
     432 
     433# Auto::FetchTitle { ... } での設定. 
     434# + Auto::FetchTitle { 
     435#     plugins { 
     436#       ExtractHeading { 
     437#         extra: name1 name2 ... 
     438#         extra-name1 { 
     439#           url:        http://www.example.com/* 
     440#           recv_limit: 10*1024 
     441#           extract:    re:<div id="title">(.*?)</div> 
     442#         } 
     443#       } 
     444#    } 
     445#  } 
     446 
     447=end tiarra-doc 
     448 
     449=cut 
     450