gpio: Fixed usage message.
[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->decoded_content;
39         close CACHE;
40     }
41     
42     return undef unless $res->is_success;
43     return $res->decoded_content;
44 }
45
46 sub getlist
47 {
48     my($name, $il, $html, @ret);
49     ($name) = @_;
50     
51     $name = ($name =~ /^(the\s+)?(.*)$/i)[1];
52     $il = uc(($name =~ /^\W*(.)/)[0]);
53     $il = "9" if (!($il =~ /[A-Z]/));
54     if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
55         return undef;
56     }
57     
58     # The only way to recognize entries that seems sure is to look
59     # after the "HOVERLINE" class.
60     
61     while($html =~ /<A\s+CLASS="HOVERLINE"\s+HREF="([^"]+)"[^>]*>(<FONT[^>]*>)?(<small>[^<]*<\/small>)?\s*([^<]+)<\//ig) {
62         if((substr "" . lc $4 , 0, length $name) eq lc $name) {
63             push @ret, $4;
64         }
65     }
66     # push @ret, $1 while $html =~ /<A\s.*CLASS=HOVERLINE\s.*>.*<FONT.*>([^<>]*$name[^<>]*)<\/FONT/ig;
67     
68     return @ret;
69 }
70
71 sub getid
72 {
73     my($name, $il, $html, $url);
74     ($name) = @_;
75     
76     $name = ($name =~ /^(the\s+)?(.*)$/i)[1];
77     $il = uc(($name =~ /^\W*(.)/)[0]);
78     $il = "9" if (!($il =~ /[A-Z]/));
79     if(!($html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il")) {
80         return undef;
81     }
82     
83     # The only way to recognize entries that seems sure is to look
84     # after the "HOVERLINE" class.
85     
86     while($html =~ /<A\s+CLASS="HOVERLINE"\s+HREF="([^"]+)"[^>]*>(<FONT[^>]*>)?(<small>[^<]*<\/small>)?\s*([^<]+)<\//ig) {
87         if((substr "" . lc $4 , 0, length $name) eq lc $name) {
88             return ($1 =~ /id=(\d+)$/)[0];
89         }
90     }
91     
92     return undef;
93 }
94
95 sub geturl
96 {
97     my($id);
98     ($id) = @_;
99     
100     return "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
101 }
102
103 sub getthemes
104 {
105     my($html, $kind, @ret);
106     ($html, $kind) = @_;
107     
108     if($html =~ /$kind theme:<\/strong>\s*\n/igc) {
109         my(@parts, $ct, $buf);
110         while($html =~ /\G\s*\<div class=\"tab\"\>(([^<>]|\<i\>|<\/i>)+)(<span[^<>]*>[^<>]*<img[^<>]*>[^<>]*<\/span>)?<\/div>/igc) {
111             $buf = $1;
112             #                     0  1            2           3     4     5        6          7                      8              9          10   1112
113             if(@parts = ($buf =~ /(\#(\d+):)?\s*\"([^\"\(]+\S)(\s*\((\<i\>(.*)<\/i>( - \s*)?)?([^<>]+)?\))?\"\s+by\s+([^\(]*[^\(\s])(\s*\(eps? (\d+)(-(\d+))?\))?/i)) {
114                 $ct = {};
115                 $ct->{"num"} = $parts[1] if defined $parts[1];
116                 if(defined $parts[5]) {
117                     $ct->{"tit"} = decode_entities($parts[5]);
118                     $ct->{"jat"} = decode_entities($parts[2]) if defined $parts[2];
119                 } else {
120                     $ct->{"tit"} = decode_entities($parts[2]) if defined $parts[2];
121                 }
122                 $ct->{"ent"} = decode_entities($parts[7]) if defined $parts[7];
123                 $ct->{"prf"} = decode_entities($parts[8]) if defined $parts[8];
124                 $ct->{"fep"} = $parts[10] if defined $parts[10];
125                 $ct->{"lep"} = $parts[12] if defined $parts[12];
126                 push @ret, $ct;
127             }
128         }
129     }
130     
131     return \@ret;
132 }
133
134 sub getseries
135 {
136     my($id, $buf, $html, %ret);
137     ($id) = @_;
138     
139     if(!($html = _get geturl $id)) {
140         return undef;
141     }
142     
143     $ret{"url"} = geturl $id;
144     ($buf) = ($html =~ /\<title\>([^<]*) - Anime News Network<\/title>/);
145     if($buf =~ /\([^\)]+\)$/) {
146         ($ret{"name"}, $ret{"type"}) = ($buf =~ /^(.*[^\s])\s*\(([^\)]+)\)$/);
147     } else {
148         $ret{"name"} = $buf;
149     }
150     if(($buf) = ($html =~ /vintage:<\/strong>\s*\n\s*<span>([^<]+)</is)) {
151         $ret{"vtg"} = $buf;
152     }
153     if(($buf) = ($html =~ /number of episodes:<\/strong>\s*\n\s*<span>([^<]+)</is)) {
154         $ret{"eps"} = $buf;
155     }
156     if(($buf) = ($html =~ /genres:<\/strong>\s*\n\s*([^<]+)</is)) {
157         $ret{"gnr"} = [split /, /, $buf];
158     }
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 }