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