From 66cb73cd2c475094d76911764addaaf3cc97ea3b Mon Sep 17 00:00:00 2001 From: fredrik Date: Mon, 15 Aug 2005 01:40:43 +0000 Subject: [PATCH] Added cache and better error handling. git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/utils@310 959494ce-11ee-0310-bf91-de5d638817bd --- ANN.pm | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/ANN.pm b/ANN.pm index 2ae48cd..dfa4b41 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. @@ -97,7 +125,9 @@ sub getseries my($id, $buf, $html, %ret); ($id) = @_; - $html = _get geturl $id; + if(!($html = _get geturl $id)) { + return undef; + } $ret{"url"} = geturl $id; ($buf) = ($html =~ /\Anime News Network - ([^<]*)<\/TITLE>/); -- 2.11.0