X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=ANN.pm;h=aefd04efca61573395bb688e492644f6f00f531e;hb=edeeab7e12944f6a0935f4c27108032f0b8ec0e0;hp=fbf08e55a88d0d943c556006168fd93890ff8a2e;hpb=23d4abb2e29634e8d2e11879be91cec9cfd6ad13;p=utils.git diff --git a/ANN.pm b/ANN.pm index fbf08e5..aefd04e 100644 --- a/ANN.pm +++ b/ANN.pm @@ -1,21 +1,45 @@ package Anime::ANN; +use POSIX; +use Digest::MD5; use LWP::UserAgent; use HTML::Entities; $ver = "0.1"; +$ua = LWP::UserAgent->new; +$ua->agent("ANNData/$ver "); +$usecache = 1; sub _get { - my($uri, $ua, $res); + my($uri, $cname, $res); ($uri) = @_; - $ua = LWP::UserAgent->new; - $ua->agent("ANNData/$ver "); - + $cname = $ENV{"HOME"} . "/.ann/"; + mkdir $cname unless -e $cname; + $cname .= "cache/"; + mkdir $cname unless -e $cname; + $cname .= Digest::MD5::md5_hex $uri; + if($usecache && -e $cname) { + my(@s); + @s = stat $cname; + if((time - $s[9]) < 86400) { + my($buf); + open CACHE, "<:utf8", $cname; + $res .= $buf while read CACHE, $buf, 1024; + close CACHE; + return $res; + } + } + $res = $ua->request(HTTP::Request->new("GET", "$uri")); - - die "could not fetch $uri\n" unless $res->is_success; + + if(open CACHE, ">:utf8", $cname) { + print CACHE $res->content; + close CACHE; + } + + return undef unless $res->is_success; return $res->content; } @@ -26,14 +50,16 @@ sub getlist $il = uc(($name =~ /^(.)/)[0]); $il = "9" if (!($il =~ /[A-Z]/)); - $html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il"; + if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) { + return undef; + } # The only way to recognize entries that seems sure is to look # after the "HOVERLINE" class. push @ret, $1 while $html =~ /.*([^<>]*$name[^<>]*)<\/FONT/ig; - return(@ret); + return @ret; } sub getid @@ -43,7 +69,9 @@ sub getid $il = uc(($name =~ /^(.)/)[0]); $il = "9" if (!($il =~ /[A-Z]/)); - $html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il"; + if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) { + return undef; + } # The only way to recognize entries that seems sure is to look # after the "HOVERLINE" class. @@ -53,6 +81,14 @@ sub getid return ($url =~ /\?id=(\d+)$/)[0]; } +sub geturl +{ + my($id); + ($id) = @_; + + return "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id"; +} + sub getthemes { my($html, $kind, @ret); @@ -67,13 +103,13 @@ sub getthemes $ct = {}; $ct->{"num"} = $parts[1] if defined $parts[1]; if(defined $parts[5]) { - $ct->{"tit"} = $parts[5]; + $ct->{"tit"} = decode_entities($parts[5]); $ct->{"jat"} = decode_entities($parts[2]) if defined $parts[2]; } else { - $ct->{"tit"} = $parts[2] if defined $parts[2]; + $ct->{"tit"} = decode_entities($parts[2]) if defined $parts[2]; } - $ct->{"ent"} = $parts[7] if defined $parts[7]; - $ct->{"prf"} = $parts[8] if defined $parts[8]; + $ct->{"ent"} = decode_entities($parts[7]) if defined $parts[7]; + $ct->{"prf"} = decode_entities($parts[8]) if defined $parts[8]; $ct->{"fep"} = $parts[10] if defined $parts[10]; $ct->{"lep"} = $parts[11] if defined $parts[11]; push @ret, $ct; @@ -86,19 +122,30 @@ sub getthemes sub getseries { - my($id, $buf, $html, %ret); + my($id, $buf, $html, %ret, @cp); ($id) = @_; - $html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id"; + if(!($html = _get geturl $id)) { + return undef; + } - $ret{"url"} = "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id"; - ($ret{"name"}) = ($html =~ /\Anime News Network - ([^<]*)<\/TITLE>/); + $ret{"url"} = geturl $id; + ($buf) = ($html =~ /\Anime News Network - ([^<]*)<\/TITLE>/); + if($buf =~ /\([^\)]+\)$/) { + ($ret{"name"}, $ret{"type"}) = ($buf =~ /^(.*[^\s])\s*\(([^\)]+)\)$/); + } else { + $ret{"name"} = $buf; + } if(($buf) = ($html =~ /vintage:<\/b>\n([^<]+)\n([^<]+)\n([^<]+)