Initial import.
[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
51 $il = uc(($name =~ /^(.)/)[0]);
52 $il = "9" if (!($il =~ /[A-Z]/));
66cb73cd 53 if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
54 return undef;
55 }
23d4abb2 56
57 # The only way to recognize entries that seems sure is to look
58 # after the "HOVERLINE" class.
59
3e60094e 60 while($html =~ /<A\s.*CLASS=HOVERLINE\s.*HREF=\"([^\"]+)\".*>([^<]+)<\//ig) {
61 if((substr "" . lc $2 , 0, length $name) eq lc $name) {
62 push @ret, $2;
63 }
64 }
65 # push @ret, $1 while $html =~ /<A\s.*CLASS=HOVERLINE\s.*>.*<FONT.*>([^<>]*$name[^<>]*)<\/FONT/ig;
23d4abb2 66
66cb73cd 67 return @ret;
23d4abb2 68}
69
70sub getid
71{
72 my($name, $il, $html, $url);
73 ($name) = @_;
74
75 $il = uc(($name =~ /^(.)/)[0]);
76 $il = "9" if (!($il =~ /[A-Z]/));
66cb73cd 77 if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
78 return undef;
79 }
23d4abb2 80
81 # The only way to recognize entries that seems sure is to look
82 # after the "HOVERLINE" class.
83
3e60094e 84 while($html =~ /<A\s.*CLASS=HOVERLINE\s.*HREF=\"([^\"]+)\".*>([^<]+)<\//ig) {
85 if((substr "" . lc $2 , 0, length $name) eq lc $name) {
86 return ($1 =~ /id=(\d+)$/)[0];
87 }
88 }
23d4abb2 89
3e60094e 90 return undef;
23d4abb2 91}
92
a9da6366 93sub geturl
94{
95 my($id);
96 ($id) = @_;
97
98 return "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
99}
100
23d4abb2 101sub getthemes
102{
103 my($html, $kind, @ret);
104 ($html, $kind) = @_;
105
106 if($html =~ /$kind theme:<\/b>\n/igc) {
107 my(@parts, $ct, $buf);
108 while($html =~ /\G\<br\>&nbsp; &nbsp; (([^<>]|\<i\>|<\/i>)+)/igc) {
109 $buf = $1;
ed5f0d88 110 # 0 1 2 3 4 5 6 7 8 9 10 11
111 if(@parts = ($buf =~ /(\#(\d+):)?\s*\"([^\"\(]+\S)(\s*\((\<i\>(.*)<\/i>(;\s*)?)?([^<>]+)?\))?\"\s+by\s+([^\(]*[^\(\s])(\s*\(eps (\d+)-(\d+)?\))?/i)) {
23d4abb2 112 $ct = {};
113 $ct->{"num"} = $parts[1] if defined $parts[1];
114 if(defined $parts[5]) {
a9da6366 115 $ct->{"tit"} = decode_entities($parts[5]);
23d4abb2 116 $ct->{"jat"} = decode_entities($parts[2]) if defined $parts[2];
117 } else {
a9da6366 118 $ct->{"tit"} = decode_entities($parts[2]) if defined $parts[2];
23d4abb2 119 }
a9da6366 120 $ct->{"ent"} = decode_entities($parts[7]) if defined $parts[7];
121 $ct->{"prf"} = decode_entities($parts[8]) if defined $parts[8];
23d4abb2 122 $ct->{"fep"} = $parts[10] if defined $parts[10];
123 $ct->{"lep"} = $parts[11] if defined $parts[11];
124 push @ret, $ct;
125 }
126 }
127 }
128
129 return \@ret;
130}
131
132sub getseries
133{
b41d8fa9 134 my($id, $buf, $html, %ret);
23d4abb2 135 ($id) = @_;
136
66cb73cd 137 if(!($html = _get geturl $id)) {
138 return undef;
139 }
23d4abb2 140
a9da6366 141 $ret{"url"} = geturl $id;
604af5b7 142 ($buf) = ($html =~ /\<TITLE\>Anime News Network - ([^<]*)<\/TITLE>/);
143 if($buf =~ /\([^\)]+\)$/) {
144 ($ret{"name"}, $ret{"type"}) = ($buf =~ /^(.*[^\s])\s*\(([^\)]+)\)$/);
145 } else {
146 $ret{"name"} = $buf;
147 }
23d4abb2 148 if(($buf) = ($html =~ /vintage:<\/b>\n([^<]+)</is)) {
149 $ret{"vtg"} = $buf;
150 }
151 if(($buf) = ($html =~ /number of episodes:<\/b>\n([^<]+)</is)) {
152 $ret{"eps"} = $buf;
153 }
edeeab7e 154 if(($buf) = ($html =~ /genres:<\/b>\n([^<]+)</is)) {
b41d8fa9 155 $ret{"gnr"} = [split /, /, $buf];
edeeab7e 156 }
23d4abb2 157 $buf = getthemes $html, "opening";
158 $ret{"op"} = $buf if(@{$buf});
159 $buf = getthemes $html, "ending";
160 $ret{"ed"} = $buf if(@{$buf});
161
162 return \%ret;
163}