package WWW::YahooJapan::KanaAddress; require 5.006; use warnings; use strict; use Carp; use version; our $VERSION = qv('0.1.2'); use LWP::UserAgent; use URI::Escape; my %_tdfk_dict = qw/ 北海道 ほっかいどう 青森県 あおもりけん 岩手県 いわてけん 宮城県 みやぎけん 秋田県 あきたけん 山形県 やまがたけん 福島県 ふくしまけん 茨城県 いばらきけん 栃木県 とちぎけん 群馬県 ぐんまけん 埼玉県 さいたまけん 千葉県 ちばけん 東京都 とうきょうと 神奈川県 かながわけん 富山県 とやまけん 新潟県 にいがたけん 石川県 いしかわけん 福井県 ふくいけん 山梨県 やまなしけん 長野県 ながのけん 岐阜県 ぎふけん 静岡県 しずおかけん 愛知県 あいちけん 三重県 みえけん 滋賀県 しがけん 京都府 きょうとふ 大阪府 おおさかふ 兵庫県 ひょうごけん 奈良県 ならけん 和歌山県 わかやまけん 鳥取県 とっとりけん 島根県 しまねけん 岡山県 おかやまけん 広島県 ひろしまけん 山口県 やまぐちけん 徳島県 とくしまけん 香川県 かがわけん 愛媛県 えひめけん 高知県 こうちけん 福岡県 ふくおかけん 佐賀県 さがけん 長崎県 ながさきけん 熊本県 くまもとけん 大分県 おおいたけん 宮崎県 みやざきけん 鹿児島県 かごしまけん 沖縄県 おきなわけん /; my $search_url_tpl = 'http://search.map.yahoo.co.jp/search?p=%s&ei=euc-jp'; my $kana_url_tpl = 'http://map.yahoo.co.jp/address?ac=%s'; # a constructor sub new{ my $class = shift; my %opt = ref($_[0]) ? %{$_[0]} : @_; my $ua = $opt{ua} || LWP::UserAgent->new(); bless {ua => $ua}, $class; }; # strip tags from HTML sub _strip_tag{ my $self = shift; my @strs = @_; return map { my $s = $_; $s =~ s/<[^>]+>//g; $s; } @strs; } # search from Yahoo Maps by free word. # the return value is raw html. sub _do_freeword_search{ my $self = shift; my $word = shift; my $url = sprintf($search_url_tpl, uri_escape($word)); my $ua = $self->{ua}; my $res = $ua->get($url); return $res->content(); } # change CHO-AZA into CHO-AZA of Yahoo expression. # params: TO-DO-FU-KEN, SHI-KU(Yahoo exp.), CHO-AZA # return: CHO-AZA(Yahoo exp.) sub _correct_choaza{ my $self = shift; my ($tdfk, $corrected_shiku, $choaza) = @_; my $html = $self->_do_freeword_search($tdfk . $corrected_shiku . $choaza); # use first element of the search result. my $corrected_address = undef; if ($html =~ m{]+http://map\.yahoo\.co\.jp/pl\?p=[^>]+>(.+?)}) { $corrected_address = $1; }else{ die "can't find $corrected_shiku, $choaza ."; } # chop SHI-KU my $regex = quotemeta($tdfk. $corrected_shiku); $corrected_address =~ s/^$regex//; # chop address-number $corrected_address =~ s/[0-9]+$//; # chop chome $corrected_address =~ s/[0-9]+丁目$//; return $corrected_address; } # change SHI-KU into SHI-KU of Yahoo expression. # params: TO-DO-FU-KEN, SHI-KU # return: SHI-KU ID, SHI-KU(Yahoo exp.) sub _correct_shiku{ my $self = shift; my ($tdfk, $shiku) = @_; my $html = $self->_do_freeword_search($tdfk . $shiku); my @codes = (); while ($html =~ m{]+map\.yahoo\.co\.jp/address\?ac=(\d+)[^>]+>(.+?)}g) { push(@codes, [$1, $2]); } die "can't determine codes: " . join(',', @codes) if(@codes != 1); my ($code, $corrected_shiku) = @{ $codes[0] }; # chop TO-DO-HU-KEN part my $regexp = quotemeta($tdfk); $corrected_shiku =~ s/^$regexp//; return ($code, $corrected_shiku); } # get address(kanji) and kana mapping in given SHI-KU (or TO-DO-HU-KEN) # params: SHI-KU ID (or TO-DO-HU-KEN ID) # return: kanji-kana mapping hash reference: {kanji1 => kana1, kanji2 => kana2} sub _get_kana_dict{ my $self = shift; my $shiku_code = shift; my $url = sprintf($kana_url_tpl, $shiku_code); my $ua = $self->{ua}; my $res = $ua->get($url); my $c = $res->content(); my %ret = (); while ($c =~ m{