« サイト探索  |   index   |  セキュリティコード »

2005年07月10日

GoogleSitemapsジェネレータ

前回のサイト探索からの続きで
Google Sitemaps用のXMLファイルを出力するスクリプトを作ってみた(コマンド編)
強調されている行番号は前回(サイト探索)からの変更点

Code: download

  1. use LWP::Simple qw(get head);
  2. use HTML::LinkExtor;
  3. use strict;
  4.  
  5. my(%seen,%mod_time,%out,$site,$top_url,$freq,$priority);
  6. $freq = 'weekly';
  7. $priority = '1.0';
  8. $top_url = 'http://sieg.xeong.com/';
  9. $top_url = "http://$top_url" unless $top_url =~ /^https?\b/;
  10. ($site = $top_url) =~ s/^(http:\/\/.+\/).*$/$1/;
  11. $site .= "/" unless ($site =~ /\/$/);
  12.  
  13. &print_xml_head;
  14. linker($top_url);
  15. &print_xml_foot;
  16.  
  17. sub linker {
  18.  my $base_url = shift;
  19.  my $parser = HTML::LinkExtor->new(undef, $base_url);
  20.  $parser->parse(get($base_url))->eof;
  21.  my @links = $parser->links;
  22.  for my $linkarray (@links) {
  23.   my @element = @$linkarray;
  24.   my $elt_type = shift @element;
  25.   while(@element) {
  26.    my ($attr_name, $attr_value) = splice(@element, 0,2);
  27.    if( $attr_name eq "href" && $attr_value->scheme =~ /\b(https?)\b/ && $attr_value =~ /^$site/
  28.     && ($attr_value =~ /\.xml?$/i or $attr_value =~ /\.s?html?$/i or $attr_value =~ /\.php.*$/i or $attr_value =~ /\.cgi.*$/i)
  29.    ){
  30.     $attr_value =~ s/^(.+)#.*$/$1/;
  31.     next if(defined $seen{$attr_value});
  32.     my @heads = LWP::Simple::head($attr_value);
  33.     if(@heads) {
  34.      $seen{$attr_value}++;
  35.      my @date = $heads[2] ? gmtime($heads[2]) : gmtime(time);
  36.      $mod_time{$attr_value} = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $date[5]+1900, $date[4]+1, @date[3,2,1,0]);
  37.     }
  38.    }
  39.   }
  40.  }
  41.  
  42.  for (sort keys %seen) {
  43.   my $mod = $mod_time{$_};
  44.   s/^(.+\.cgi).*$/$1/i;
  45.   s/^(.+\.php).*$/$1/i;
  46.   if(defined $out{$_}) {
  47.    next;
  48.   } else {
  49.    $out{$_} = 'true';
  50.    print_xml($_,$mod);
  51.    linker($_);
  52.   }
  53.  }
  54. }
  55.  
  56. sub print_xml_head {
  57.  my %in_url = @_;
  58.  my @date = gmtime(time);
  59.  my $mod_time = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $date[5]+1900, $date[4]+1, @date[3,2,1,0]);
  60.  print <<XML;
  61. <?xml version="1.0" encoding="UTF-8"?>
  62. <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
  63. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64. xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
  65. http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">\n
  66. <url>
  67. <loc>$top_url</loc>
  68. <lastmod>$mod_time</lastmod>
  69. <changefreq>$freq</changefreq>
  70. <priority>$priority</priority>
  71. </url>\n
  72. XML
  73. }
  74.  
  75. sub print_xml {
  76.  print qq(<url>\n<loc>$_[0]</loc>\n<lastmod>$_[1]</lastmod>\n</url>\n);
  77. }
  78.  
  79. sub print_xml_foot {
  80.  print qq(\n</urlset>\n);
  81. }

使い方
6行目で、内容の更新頻度("always", "hourly", "daily", "weekly", "monthly", "yearly", "never")を指定
7行目で、同じサイト内の他のページと比較した優先度(0.0〜1.0)
8行目で探索するURLを指定
Windowsではコマンドラインから、これを実行するとGooglSitemaps用のXMLを表示する

WindowsコマンドプロンプトでのXMLファイル(sitemap.xml)への出力例
perl -w xmlgen.pl > sitemap.xml

環境
以下のperlモジュールが必要
LWP::Simple
HTML::LinkExtor

参照
UTCタイムについて、W3CのDate and Time Formatsを参考にした
Link::Extorについては Perldoc と、河馬屋二千年堂さんの日本語訳
Google Sitemapsについては、Google Sitemaps (BETA) HelpUsing the Sitemap Generatorを参考にした


投稿者 edams : 2005年07月10日 17:01

トラックバック

このエントリーのトラックバックURL: http://sieg.xeong.com/mt-tb.cgi/22
このエントリーを含むはてなブックマークこのエントリーを含むはてなブックマーク


コメント

サイン・インを確認しました、 さん。コメントしてください。 (サイン・アウト)

(いままで、ここでコメントしたとがないときは、コメントを表示する前にこのウェブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)


情報を登録する?