fa2da23ea41ab9546d3fd948d62113012d455a25
[cascardo/www-eztv.git] / lib / WWW / EZTV / Episode.pm
1 package WWW::EZTV::Episode;
2 use Moose;
3 with 'WWW::EZTV::UA';
4
5 # ABSTRACT: Show episode
6
7 has show     => is => 'ro', isa => 'WWW::EZTV::Show', required => 1;
8 has title    => is => 'ro', isa => 'Str', required => 1;
9 has url      => is => 'ro', isa => 'Mojo::URL', required => 1;
10 has links    => 
11     is      => 'ro',
12     handles => {
13         find_link => 'first',
14         has_links => 'size',
15     };
16
17 has _parsed  => is => 'ro', lazy => 1, builder => '_parse';
18
19 has name     => is => 'ro', lazy => 1, 
20     default  => sub { shift->_parsed->{name} };
21
22 has season     => is => 'ro', lazy => 1, 
23     default  => sub { shift->_parsed->{season} };
24
25 has number     => is => 'ro', lazy => 1, 
26     default  => sub { shift->_parsed->{number} };
27
28 has version     => is => 'ro', lazy => 1, 
29     default  => sub { shift->_parsed->{version} };
30
31 has quality     => is => 'ro', lazy => 1, 
32     default  => sub { shift->_parsed->{quality} || 'standard' };
33
34 has size     => is => 'ro', lazy => 1,
35     default  => sub { shift->_parsed->{size} };
36
37 sub _parse {
38     my $title = shift->title;
39
40     $title =~ /^\s*
41       (?<name>.+?)
42       \s+
43       (?<chapter>
44         S (?<season>\d+) E (?<number>\d+)
45        |(?<season>\d+) x (?<number>\d+)
46        |(?<number>\d+) of (?<total>\d+)
47       )
48       \s+
49       (?<version>
50         ((?<quality>\d+p)\s+)?
51         (?<team>.*?)
52       )
53       (?: 
54         \s+
55         \((?<size> 
56           \d+ 
57           [^\)]+
58         )\) 
59       )?
60     \s*$/xi;
61
62     return {
63         name    => $+{name} || $title,
64         chapter => $+{chapter},
65         number  => $+{number} +0,
66         season  => ($+{season}||0) +0,
67         total   => ($+{total}||0) +0,
68         version => $+{version} || '',
69         quality => $+{quality} || 'standard',
70         team    => $+{team},
71         size    => $+{size}
72     };
73 }
74
75 1;
76
77 =attr has_links
78
79 How many episodes has this show.
80
81 =cut
82
83 =method find_link
84
85 Find first L<WWW::EZTV::Link> object matching the given criteria. 
86 This method accept an anon function.
87
88 =cut