Change attrs() for attr() to work with mojo::ua 4.x
[cascardo/www-eztv.git] / lib / WWW / EZTV / Show.pm
1 package WWW::EZTV::Show;
2 use Moose;
3 with 'WWW::EZTV::UA';
4 use WWW::EZTV::Link;
5 use WWW::EZTV::Episode;
6
7 # ABSTRACT: Show object
8
9 has title    => is => 'ro', isa => 'Str', required => 1;
10 has name     => is => 'ro', lazy => 1, default => \&_name;
11 has year     => is => 'ro', lazy => 1, default => \&_year;
12 has url      => is => 'ro', isa => 'Mojo::URL', required => 1;
13 has status   => is => 'ro', isa => 'Str', required => 1;
14 has rating   => is => 'ro', isa => 'Int', default => sub {0};
15 has episodes => 
16     is      => 'ro',
17     lazy    => 1,
18     builder => '_build_episodes',
19     handles => {
20         find_episode => 'first',
21         has_episodes => 'size',
22     };
23
24 sub _build_episodes {
25     my $self = shift;
26     $self->get_response($self->url)->dom->find('table.forum_header_noborder tr[name="hover"]')->map(sub{
27         my $tr = shift;
28         my $a  = $tr->at('td:nth-child(2) a');
29
30         WWW::EZTV::Episode->new(
31             title    => $a->attr('title'),
32             url      => $self->url->clone->path($a->attr('href')),
33             links    => $tr->find('td:nth-child(3) a')->map(sub{
34                 WWW::EZTV::Link->new( url => shift->attr('href') )
35             }),
36             released => $tr->at('td:nth-child(4)')->all_text,
37             show     => $self
38         );
39     });
40 }
41
42 sub _name {
43     my $self = shift;
44     my $name = $self->title;
45
46     # Chasers War on Everything, The
47     if ( $name =~ /^(.+),\s*([^,]+)$/ ) { $name = "$2 $1" }
48
49     # Remove year: Castle (2009)
50     $name =~ s/\s* \(\d{4}\) \s*/ /x;
51
52     # Trim and cleanup spaces
53     $self->_cleanup_str($name);
54 }
55
56 sub _year {
57     my $self = shift;
58     if ( $self->title =~ /\((\d{4})\)/ ) {
59         return $1;
60     }
61 }
62
63 sub _cleanup_str {
64     my $str = pop;
65     $str =~ s/^\s+|\s+$//g;
66     $str =~ s/\s+/ /g;
67     $str;
68 }
69
70 1;
71
72 =attr title
73 =cut
74
75 =attr name
76 =cut
77
78 =attr year
79 =cut
80
81 =attr url
82 =cut
83
84 =attr status
85 =cut
86
87 =attr rating
88 =cut
89
90 =attr episodes
91
92 Collection of episodes fetched for this show.
93
94 =cut
95
96 =attr has_episodes
97
98 How many episodes has this show.
99
100 =cut
101
102 =method find_episode
103
104 Find first L<WWW::EZTV::Episode> object matching the given criteria. 
105 This method accept an anon function.
106
107 =cut
108