616641866e803eeaf58a4cdf19f6a35c9140685c
[cascardo/www-eztv.git] / lib / WWW / EZTV.pm
1 package WWW::EZTV;
2 use Moose;
3 with 'WWW::EZTV::UA';
4 use WWW::EZTV::Show;
5
6 # ABSTRACT: EZTV scrapper
7
8 has url       => ( is => 'ro', lazy => 1, default => sub { Mojo::URL->new('http://eztv.it/') } );
9 has url_shows => ( is => 'ro', lazy => 1, default => sub { shift->url->clone->path('/showlist/') } );
10
11 has shows => 
12     is      => 'ro',
13     lazy    => 1,
14     builder => '_build_shows',
15     handles => {
16         find_show    => 'first',
17         has_shows    => 'size',
18     };
19
20 sub _build_shows {
21     my $self = shift;
22
23     $self->get_response( $self->url_shows )->dom->find('table.forum_header_border tr[name="hover"]')->map(sub {
24         my $tr = shift;
25         my $link = $tr->at('td:nth-child(1) a');
26         WWW::EZTV::Show->new(
27             title  => $link->all_text,
28             url    => $self->url->clone->path($link->attrs('href')),
29             status => lc($tr->at('td:nth-child(2)')->all_text),
30             rating => $tr->at('td:nth-child(3)')->all_text
31         );
32     });
33 }
34
35 =head1 SYNOPSIS
36
37 First create a WWW::EZTV object to navigate.
38
39     use WWW::EZTV;
40
41     my $eztv = WWW::EZTV->new;
42
43     my $show = $eztv->find_show(sub{ $_->name =~ /Walking dead/i });
44
45     my $episode = $show->find_episode(sub{ 
46         $_->season == 3 && 
47         $_->number == 8 && 
48         $_->quality eq 'standard' 
49     });
50
51 =attr url
52 EZTV URL.
53 =cut
54
55 =attr url_shows
56 EZTV shows URL.
57 =cut
58
59 =attr shows
60 L<Mojo::Collection> of L<WWW::EZTV::Show> objects.
61 =cut
62
63 =attr has_shows
64 How many shows exists.
65 =cut
66
67 =method find_show
68 Find first L<WWW::EZTV::Show> object matching the given criteria. 
69 This method accept an anon function.
70 =cut
71
72 1;