Changes URL to eztv.ag and tries to fix DOM parsing.
[cascardo/www-eztv.git] / lib / WWW / EZTV.pm
1 package WWW::EZTV;
2
3 use Moose;
4 with 'WWW::EZTV::UA';
5 use WWW::EZTV::Show;
6
7 # ABSTRACT: EZTV scrapper
8
9 has url       => ( is => 'ro', lazy => 1, default => sub { Mojo::URL->new('https://eztv.ag/') } );
10 has url_shows => ( is => 'ro', lazy => 1, default => sub { shift->url->clone->path('/showlist/') } );
11
12 has shows =>
13     is      => 'ro',
14     lazy    => 1,
15     builder => '_build_shows',
16     handles => {
17         find_show    => 'first',
18         has_shows    => 'size',
19     };
20
21 sub _build_shows {
22     my $self = shift;
23
24     $self->get_response($self->url_shows)->dom->find('table tr[name="hover"]')->map(sub {
25         my $tr = shift;
26         my $link = $tr->at('td:nth-child(1) a');
27         WWW::EZTV::Show->new(
28             title  => $link->all_text,
29             url    => $self->url->clone->path($link->attr('href')),
30             status => lc($tr->at('td:nth-child(2)')->all_text),
31             rating => $tr->at('td:nth-child(3) b')->text + 0
32         );
33     });
34 }
35
36 1;
37
38 =head1 SYNOPSIS
39
40     use WWW::EZTV;
41     use v5.10;
42
43     my $eztv = WWW::EZTV->new;
44
45     # Find one show
46     my $show = $eztv->find_show(sub{ $_->name =~ /Walking dead/i });
47
48     # Find one episode
49     my $episode = $show->find_episode(sub{ 
50         $_->season == 3 && 
51         $_->number == 8 && 
52         $_->quality eq 'standard' 
53     });
54
55     # Get first torrent url for this episode
56     say $episode->find_link(sub{ $_->type eq 'torrent' })->url;
57
58 =attr url
59
60 EZTV URL.
61
62 =cut
63
64 =attr url_shows
65
66 EZTV shows URL.
67
68 =cut
69
70 =attr shows
71
72 L<Mojo::Collection> of L<WWW::EZTV::Show> objects.
73
74 =cut
75
76 =attr has_shows
77
78 How many shows exists.
79
80 =cut
81
82 =method find_show
83
84 Find first L<WWW::EZTV::Show> object matching the given criteria. 
85 This method accept an anon function.
86
87 =cut
88
89 =head1 BUGS
90
91 This is an early release, so probable there are plenty of bugs around.
92 If you found one, please report it on RT or at the github repo:
93
94 L<https://github.com/diegok/www-eztv>
95
96 Pull requests are also very welcomed, but please include tests demostrating
97 what you've fixed.
98
99 =cut
100