Enable curser.
[utils.git] / ANN.pm
CommitLineData
23d4abb2 1package Anime::ANN;
2
66cb73cd 3use POSIX;
4use Digest::MD5;
23d4abb2 5use LWP::UserAgent;
6use HTML::Entities;
7
8$ver = "0.1";
66cb73cd 9$ua = LWP::UserAgent->new;
10$ua->agent("ANNData/$ver ");
11$usecache = 1;
23d4abb2 12
13sub _get
14{
66cb73cd 15 my($uri, $cname, $res);
23d4abb2 16 ($uri) = @_;
17
66cb73cd 18 $cname = $ENV{"HOME"} . "/.ann/";
19 mkdir $cname unless -e $cname;
20 $cname .= "cache/";
21 mkdir $cname unless -e $cname;
22 $cname .= Digest::MD5::md5_hex $uri;
23 if($usecache && -e $cname) {
24 my(@s);
25 @s = stat $cname;
26 if((time - $s[9]) < 86400) {
27 my($buf);
28 open CACHE, "<:utf8", $cname;
29 $res .= $buf while read CACHE, $buf, 1024;
30 close CACHE;
31 return $res;
32 }
33 }
34
23d4abb2 35 $res = $ua->request(HTTP::Request->new("GET", "$uri"));
66cb73cd 36
37 if(open CACHE, ">:utf8", $cname) {
38 print CACHE $res->content;
39 close CACHE;
40 }
41
42 return undef unless $res->is_success;
23d4abb2 43 return $res->content;
44}
45
46sub getlist
47{
48 my($name, $il, $html, @ret);
49 ($name) = @_;
50
3b5018db 51 $name = ($name =~ /^(the\s+)?(.*)$/i)[1];
23d4abb2 52 $il = uc(($name =~ /^(.)/)[0]);
53 $il = "9" if (!($il =~ /[A-Z]/));
66cb73cd 54 if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
55 return undef;
56 }
23d4abb2 57
58 # The only way to recognize entries that seems sure is to look
59 # after the "HOVERLINE" class.
60
3b5018db 61 while($html =~ /<A\s.*CLASS=HOVERLINE\s.*HREF=\"([^\"]+)\".*>(\<small\>.*\<\/small\>)?([^<]+)<\//ig) {
62 if((substr "" . lc $3 , 0, length $name) eq lc $name) {
63 push @ret, $3;
3e60094e 64 }
65 }
66 # push @ret, $1 while $html =~ /<A\s.*CLASS=HOVERLINE\s.*>.*<FONT.*>([^<>]*$name[^<>]*)<\/FONT/ig;
23d4abb2 67
66cb73cd 68 return @ret;
23d4abb2 69}
70
71sub getid
72{
73 my($name, $il, $html, $url);
74 ($name) = @_;
75
3b5018db 76 $name = ($name =~ /^(the\s+)?(.*)$/i)[1];
23d4abb2 77 $il = uc(($name =~ /^(.)/)[0]);
78 $il = "9" if (!($il =~ /[A-Z]/));
66cb73cd 79 if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
80 return undef;
81 }
23d4abb2 82
83 # The only way to recognize entries that seems sure is to look
84 # after the "HOVERLINE" class.
85
3b5018db 86 while($html =~ /<A\s.*CLASS=HOVERLINE\s.*HREF=\"([^\"]+)\".*>(\<small\>.*\<\/small\>)?([^<]+)<\//ig) {
87 if((substr "" . lc $3 , 0, length $name) eq lc $name) {
3e60094e 88 return ($1 =~ /id=(\d+)$/)[0];
89 }
90 }
23d4abb2 91
3e60094e 92 return undef;
23d4abb2 93}
94
a9da6366 95sub geturl
96{
97 my($id);
98 ($id) = @_;
99
100 return "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
101}
102
23d4abb2 103sub getthemes
104{
105 my($html, $kind, @ret);
106 ($html, $kind) = @_;
107
3b5018db 108 if($html =~ /$kind theme:<\/strong>\s*\n/igc) {
23d4abb2 109 my(@parts, $ct, $buf);
3b5018db 110 while($html =~ /\G\s*\<div\>(([^<>]|\<i\>|<\/i>)+)<\/div>/igc) {
23d4abb2 111 $buf = $1;
3b5018db 112 # 0 1 2 3 4 5 6 7 8 9 10 11
113 if(@parts = ($buf =~ /(\#(\d+):)?\s*\"([^\"\(]+\S)(\s*\((\<i\>(.*)<\/i>( - \s*)?)?([^<>]+)?\))?\"\s+by\s+([^\(]*[^\(\s])(\s*\(eps (\d+)-(\d+)?\))?/i)) {
23d4abb2 114 $ct = {};
115 $ct->{"num"} = $parts[1] if defined $parts[1];
116 if(defined $parts[5]) {
a9da6366 117 $ct->{"tit"} = decode_entities($parts[5]);
23d4abb2 118 $ct->{"jat"} = decode_entities($parts[2]) if defined $parts[2];
119 } else {
a9da6366 120 $ct->{"tit"} = decode_entities($parts[2]) if defined $parts[2];
23d4abb2 121 }
a9da6366 122 $ct->{"ent"} = decode_entities($parts[7]) if defined $parts[7];
123 $ct->{"prf"} = decode_entities($parts[8]) if defined $parts[8];
23d4abb2 124 $ct->{"fep"} = $parts[10] if defined $parts[10];
125 $ct->{"lep"} = $parts[11] if defined $parts[11];
126 push @ret, $ct;
127 }
128 }
129 }
130
131 return \@ret;
132}
133
134sub getseries
135{
b41d8fa9 136 my($id, $buf, $html, %ret);
23d4abb2 137 ($id) = @_;
138
66cb73cd 139 if(!($html = _get geturl $id)) {
140 return undef;
141 }
23d4abb2 142
a9da6366 143 $ret{"url"} = geturl $id;
edcecb65 144 ($buf) = ($html =~ /\<title\>([^<]*) - Anime News Network<\/title>/);
604af5b7 145 if($buf =~ /\([^\)]+\)$/) {
146 ($ret{"name"}, $ret{"type"}) = ($buf =~ /^(.*[^\s])\s*\(([^\)]+)\)$/);
147 } else {
148 $ret{"name"} = $buf;
149 }
3b5018db 150 if(($buf) = ($html =~ /vintage:<\/strong>\s*\n\s*<span>([^<]+)</is)) {
23d4abb2 151 $ret{"vtg"} = $buf;
152 }
3b5018db 153 if(($buf) = ($html =~ /number of episodes:<\/strong>\s*\n\s*<span>([^<]+)</is)) {
23d4abb2 154 $ret{"eps"} = $buf;
155 }
3b5018db 156 if(($buf) = ($html =~ /genres:<\/strong>\s*\n\s*([^<]+)</is)) {
b41d8fa9 157 $ret{"gnr"} = [split /, /, $buf];
edeeab7e 158 }
23d4abb2 159 $buf = getthemes $html, "opening";
160 $ret{"op"} = $buf if(@{$buf});
161 $buf = getthemes $html, "ending";
162 $ret{"ed"} = $buf if(@{$buf});
163
164 return \%ret;
165}