c62f236b9c97f3720d45b936bb642cd4457e8258
[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
53 EZTV URL.
54
55 =cut
56
57 =attr url_shows
58
59 EZTV shows URL.
60
61 =cut
62
63 =attr shows
64
65 L<Mojo::Collection> of L<WWW::EZTV::Show> objects.
66
67 =cut
68
69 =attr has_shows
70
71 How many shows exists.
72
73 =cut
74
75 =method find_show
76
77 Find first L<WWW::EZTV::Show> object matching the given criteria. 
78 This method accept an anon function.
79
80 =cut
81
82 =head1 BUGS
83
84 This is an early release, so probable there are plenty of bugs around.
85 If you found one, please report it on RT or at the github repo:
86
87 L<https://github.com/diegok/www-eztv>
88
89 Pull requests are also very welcomed, but please include tests demostrating
90 what you've fixed.
91 1;