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