Extract type from the name.
[utils.git] / ANN.pm
CommitLineData
23d4abb2 1package Anime::ANN;
2
3use LWP::UserAgent;
4use HTML::Entities;
5
6$ver = "0.1";
7
8sub _get
9{
10 my($uri, $ua, $res);
11 ($uri) = @_;
12
13 $ua = LWP::UserAgent->new;
14 $ua->agent("ANNData/$ver ");
15
16 $res = $ua->request(HTTP::Request->new("GET", "$uri"));
17
18 die "could not fetch $uri\n" unless $res->is_success;
19 return $res->content;
20}
21
22sub getlist
23{
24 my($name, $il, $html, @ret);
25 ($name) = @_;
26
27 $il = uc(($name =~ /^(.)/)[0]);
28 $il = "9" if (!($il =~ /[A-Z]/));
29 $html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il";
30
31 # The only way to recognize entries that seems sure is to look
32 # after the "HOVERLINE" class.
33
34 push @ret, $1 while $html =~ /<A\s.*CLASS=HOVERLINE\s.*>.*<FONT.*>([^<>]*$name[^<>]*)<\/FONT/ig;
35
36 return(@ret);
37}
38
39sub getid
40{
41 my($name, $il, $html, $url);
42 ($name) = @_;
43
44 $il = uc(($name =~ /^(.)/)[0]);
45 $il = "9" if (!($il =~ /[A-Z]/));
46 $html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?list=$il";
47
48 # The only way to recognize entries that seems sure is to look
49 # after the "HOVERLINE" class.
50
51 (($url) = ($html =~ /<A\s.*CLASS=HOVERLINE\s.*HREF=\"([^\"]+)\".*$name/i)) || return;
52
53 return ($url =~ /\?id=(\d+)$/)[0];
54}
55
56sub getthemes
57{
58 my($html, $kind, @ret);
59 ($html, $kind) = @_;
60
61 if($html =~ /$kind theme:<\/b>\n/igc) {
62 my(@parts, $ct, $buf);
63 while($html =~ /\G\<br\>&nbsp; &nbsp; (([^<>]|\<i\>|<\/i>)+)/igc) {
64 $buf = $1;
65 # 0 1 2 3 4 5 6 7 8 9 10 11
66 if(@parts = ($buf =~ /(\#(\d+):)?\s*\"([^\"\(]+)(\s+\((\<i\>(.*)<\/i>(;\s*)?)?([^<>]+)?\))?\"\s+by\s+([^\(]*[^\(\s])(\s*\(eps (\d+)-(\d+)?\))?/i)) {
67 $ct = {};
68 $ct->{"num"} = $parts[1] if defined $parts[1];
69 if(defined $parts[5]) {
70 $ct->{"tit"} = $parts[5];
71 $ct->{"jat"} = decode_entities($parts[2]) if defined $parts[2];
72 } else {
73 $ct->{"tit"} = $parts[2] if defined $parts[2];
74 }
75 $ct->{"ent"} = $parts[7] if defined $parts[7];
76 $ct->{"prf"} = $parts[8] if defined $parts[8];
77 $ct->{"fep"} = $parts[10] if defined $parts[10];
78 $ct->{"lep"} = $parts[11] if defined $parts[11];
79 push @ret, $ct;
80 }
81 }
82 }
83
84 return \@ret;
85}
86
87sub getseries
88{
89 my($id, $buf, $html, %ret);
90 ($id) = @_;
91
92 $html = _get "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
93
94 $ret{"url"} = "http://www.animenewsnetwork.com/encyclopedia/anime.php?id=$id";
604af5b7 95 ($buf) = ($html =~ /\<TITLE\>Anime News Network - ([^<]*)<\/TITLE>/);
96 if($buf =~ /\([^\)]+\)$/) {
97 ($ret{"name"}, $ret{"type"}) = ($buf =~ /^(.*[^\s])\s*\(([^\)]+)\)$/);
98 } else {
99 $ret{"name"} = $buf;
100 }
23d4abb2 101 if(($buf) = ($html =~ /vintage:<\/b>\n([^<]+)</is)) {
102 $ret{"vtg"} = $buf;
103 }
104 if(($buf) = ($html =~ /number of episodes:<\/b>\n([^<]+)</is)) {
105 $ret{"eps"} = $buf;
106 }
107 $buf = getthemes $html, "opening";
108 $ret{"op"} = $buf if(@{$buf});
109 $buf = getthemes $html, "ending";
110 $ret{"ed"} = $buf if(@{$buf});
111
112 return \%ret;
113}