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