Changes and simple fix
[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 1;
36
37 =head1 SYNOPSIS
38
39 First create a WWW::EZTV object to navigate.
40
41     use WWW::EZTV;
42
43     my $eztv = WWW::EZTV->new;
44
45     my $show = $eztv->find_show(sub{ $_->name =~ /Walking dead/i });
46
47     my $episode = $show->find_episode(sub{ 
48         $_->season == 3 && 
49         $_->number == 8 && 
50         $_->quality eq 'standard' 
51     });
52
53 =attr url
54
55 EZTV URL.
56
57 =cut
58
59 =attr url_shows
60
61 EZTV shows URL.
62
63 =cut
64
65 =attr shows
66
67 L<Mojo::Collection> of L<WWW::EZTV::Show> objects.
68
69 =cut
70
71 =attr has_shows
72
73 How many shows exists.
74
75 =cut
76
77 =method find_show
78
79 Find first L<WWW::EZTV::Show> object matching the given criteria. 
80 This method accept an anon function.
81
82 =cut
83
84 =head1 BUGS
85
86 This is an early release, so probable there are plenty of bugs around.
87 If you found one, please report it on RT or at the github repo:
88
89 L<https://github.com/diegok/www-eztv>
90
91 Pull requests are also very welcomed, but please include tests demostrating
92 what you've fixed.
93
94 =cut
95