Fix theme regex.
[utils.git] / ANN.pm
1 package Anime::ANN;
2
3 use POSIX;
4 use Digest::MD5;
5 use LWP::UserAgent;
6 use HTML::Entities;
7
8 $ver = "0.1";
9 $ua = LWP::UserAgent->new;
10 $ua->agent("ANNData/$ver ");
11 $usecache = 1;
12
13 sub _get
14 {
15     my($uri, $cname, $res);
16     ($uri) = @_;
17
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     
35     $res = $ua->request(HTTP::Request->new("GET", "$uri"));
36     
37     if(open CACHE, ">:utf8", $cname) {
38         print CACHE $res->content;
39         close CACHE;
40     }
41     
42     return undef unless $res->is_success;
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]/));
53     if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
54         return undef;
55     }
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     
62     return @ret;
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]/));
72     if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
73         return undef;
74     }
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
84 sub geturl
85 {
86     my($id);
87     ($id) = @_;
88     
89     return "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
90 }
91
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\>&nbsp; &nbsp; (([^<>]|\<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]) {
106                     $ct->{"tit"} = decode_entities($parts[5]);
107                     $ct->{"jat"} = decode_entities($parts[2]) if defined $parts[2];
108                 } else {
109                     $ct->{"tit"} = decode_entities($parts[2]) if defined $parts[2];
110                 }
111                 $ct->{"ent"} = decode_entities($parts[7]) if defined $parts[7];
112                 $ct->{"prf"} = decode_entities($parts[8]) if defined $parts[8];
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 {
125     my($id, $buf, $html, %ret);
126     ($id) = @_;
127     
128     if(!($html = _get geturl $id)) {
129         return undef;
130     }
131     
132     $ret{"url"} = geturl $id;
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     }
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     }
145     if(($buf) = ($html =~ /genres:<\/b>\n([^<]+)</is)) {
146         $ret{"gnr"} = [split /, /, $buf];
147     }
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 }