root/lang/perl/Acme-JapaneseAvActress/trunk/lib/Acme/JapaneseAvActress.pm @ 2681

Revision 2681, 4.4 kB (checked in by yusukebe, 5 years ago)

add yomi method

Line 
1package Acme::JapaneseAvActress;
2
3use strict;
4use warnings;
5use URI::Fetch;
6use XML::Simple;
7use HTML::Entities;
8use Cache::File;
9
10our $VERSION = "0.0.1";
11
12sub new {
13    my $class = shift;
14    my $opt   = ref $_[0] eq 'HASH' ? shift: {@_};
15    my %self  = (
16        wikipedia_url =>
17"http://ja.wikipedia.org/wiki/%E7%89%B9%E5%88%A5:Export/AV%E5%A5%B3%E5%84%AA%E4%B8%80%E8%A6%A7",
18        %$opt,
19    );
20
21    return bless \%self, $class;
22}
23
24sub get {
25    my $self = shift;
26    my $content;
27    if ( $self->{default_expires} && $self->{cache_root} ) {
28        my $cache = Cache::File->new(
29            cache_root      => $self->{cache_root},
30            default_expires => $self->{default_expires}
31        );
32        if ( $cache->exists( $self->{wikipedia_url} ) ) {
33            $content =
34              Storable::thaw( $cache->get( $self->{wikipedia_url} ) )
35              ->{Content};
36        }
37        else {
38            my $xml =
39              URI::Fetch->fetch( $self->{wikipedia_url}, Cache => $cache )
40              or die URI::Fetch->errstr;
41            $content = $xml->content;
42        }
43    }
44    else {
45        my $xml = URI::Fetch->fetch( $self->{wikipedia_url} )
46          or die URI::Fetch->errstr;
47        $content = $xml->content;
48    }
49    my ( @actress, $initial );
50    my $flag = 0;
51    my @lines = split( /\n/, $content );
52    foreach my $line (@lines) {
53        my $actress_info = {};
54        $flag = 1 if $line =~ /== あ行 ==/;
55        last if $line =~ /== 関連項目 ==/;
56        next unless $flag;
57        if ( $line =~ /===\s([あ-ん]*?)\s===\Z/ ) {
58            $initial = $1;
59        }
60        elsif ( $line =~ /\[\[(.*?)\]\](.*)/ ) {
61            my $temp;
62            $actress_info->{name} = $1;
63            $temp = $2;
64            if ( $temp =~
65/(?:(|\()(.*?)(?:)|\)).*(?:(|\()\[\[(\d{4})年\]\](?:)|\))/
66              )
67            {
68                $actress_info->{yomi} = $1;
69                $actress_info->{year} = $2;
70            }
71            elsif ( $temp =~ /(?:(|\()\[\[(\d{4})年\]\](?:)|\))/ ) {
72                $actress_info->{yomi} = "";
73                $actress_info->{year} = $1;
74            }
75            elsif ( $temp =~ /(?:(|\()(.*?)(?:)|\))/ ) {
76                $actress_info->{yomi} = $1;
77                $actress_info->{year} = "";
78            }
79            if ( $actress_info->{name} =~ /(.*)\|(.*)/ ) {
80                $actress_info->{name} = $2;
81            }
82            decode_entities( $actress_info->{name} );
83            decode_entities( $actress_info->{yomi} );
84            $actress_info->{initial} = $initial;
85            push( @actress,
86                Acme::JapaneseAvActress::Actress->new($actress_info) );
87        }
88    }
89    $self->{actress} = \@actress;
90    return \@actress;
91}
92
93sub initial {
94    my ( $self, $initial ) = @_;
95    my @temp;
96    foreach my $actress ( @{ $self->{actress} } ) {
97        push( @temp, $actress ) if $actress->initial eq $initial;
98    }
99    return \@temp;
100}
101
102sub year {
103    my ( $self, $year ) = @_;
104    my @temp;
105    foreach my $actress ( @{ $self->{actress} } ) {
106        push( @temp, $actress ) if $actress->year eq $year;
107    }
108    return \@temp;
109}
110
111sub name {
112    my ( $self, $name ) = @_;
113    foreach my $actress ( @{ $self->{actress} } ) {
114        return $actress if $actress->name eq $name;
115    }
116    return;
117}
118sub yomi {
119    my ( $self, $yomi ) = @_;
120    foreach my $actress ( @{ $self->{actress} } ) {
121        my $y = $actress->yomi;
122        $y =~ s/ //;
123        $yomi =~ s/ //;
124        return $actress if $y eq $yomi;
125    }
126    return;
127}
128
129package Acme::JapaneseAvActress::Actress;
130use strict;
131use warnings;
132use base qw(Class::Accessor::Fast);
133
134__PACKAGE__->mk_accessors(qw(name initial yomi year));
135
136
1371;
138
139__END__
140
141=head1 NAME
142
143Acme::JapaneseAvActress - Get Japanese AV Actress Infomation from wikipedia
144
145
146=head1 VERSION
147
148This document describes Acme::JapaneseAvActress version 0.0.1
149
150
151=head1 SYNOPSIS
152
153    use Acme::JapaneseAvActress;
154    use Data::Dumper;
155
156    my $actress = Acme::JapaneseAvActress->new(
157        {
158            'cache_root'      => '/tmp/avactress',
159            'default_expires' => '7days',
160        }
161    );
162    $actress->get;
163    my $actlist = $actress->year("2000");
164    print Dumper($actlist);
165
166=head1 AUTHOR
167
168Yusuke Wada  C<< <yusuke@kamawada.com> >>
169
170
171=head1 LICENCE AND COPYRIGHT
172
173Copyright (c) 2007, Yusuke Wada C<< <yusuke@kamawada.com> >>. All rights reserved.
174
175This module is free software; you can redistribute it and/or
176modify it under the same terms as Perl itself. See L<perlartistic>.
Note: See TracBrowser for help on using the browser.