From 17802f254b869064461d14e6a2fb015b59246a46 Mon Sep 17 00:00:00 2001 From: diegok Date: Wed, 26 Dec 2012 00:47:07 +0100 Subject: [PATCH] Retrive episode links by type and some more docs --- Changes | 4 ++++ lib/WWW/EZTV.pm | 8 ++++++-- lib/WWW/EZTV/Episode.pm | 22 ++++++++++++++++++++-- lib/WWW/EZTV/Link.pm | 35 ++++++++++++++++++++++++++++++++++- lib/WWW/EZTV/Show.pm | 2 +- t/02-find.t | 4 ++++ 6 files changed, 69 insertions(+), 6 deletions(-) diff --git a/Changes b/Changes index 905b5fe..44f8f09 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ {{$NEXT}} + - Retrieve episode link by type (magnet, torrent, direct). + +0.02 2012-12-26 00:06:54 Europe/Madrid + - Better docs formatting - Changes file :) diff --git a/lib/WWW/EZTV.pm b/lib/WWW/EZTV.pm index d2bde1f..2bf91dc 100644 --- a/lib/WWW/EZTV.pm +++ b/lib/WWW/EZTV.pm @@ -36,20 +36,24 @@ sub _build_shows { =head1 SYNOPSIS -First create a WWW::EZTV object to navigate. - use WWW::EZTV; + use v5.10; my $eztv = WWW::EZTV->new; + # Find one show my $show = $eztv->find_show(sub{ $_->name =~ /Walking dead/i }); + # Find one episode my $episode = $show->find_episode(sub{ $_->season == 3 && $_->number == 8 && $_->quality eq 'standard' }); + # Get first torrent url for this episode + say $episode->find_link(sub{ $_->type eq 'torrent' })->url; + =attr url EZTV URL. diff --git a/lib/WWW/EZTV/Episode.pm b/lib/WWW/EZTV/Episode.pm index 0f2ffa2..fa2da23 100644 --- a/lib/WWW/EZTV/Episode.pm +++ b/lib/WWW/EZTV/Episode.pm @@ -2,12 +2,17 @@ package WWW::EZTV::Episode; use Moose; with 'WWW::EZTV::UA'; -# ABSTRACT: EZTV single episode +# ABSTRACT: Show episode has show => is => 'ro', isa => 'WWW::EZTV::Show', required => 1; has title => is => 'ro', isa => 'Str', required => 1; has url => is => 'ro', isa => 'Mojo::URL', required => 1; -has links => is => 'rw'; +has links => + is => 'ro', + handles => { + find_link => 'first', + has_links => 'size', + }; has _parsed => is => 'ro', lazy => 1, builder => '_parse'; @@ -68,3 +73,16 @@ sub _parse { } 1; + +=attr has_links + +How many episodes has this show. + +=cut + +=method find_link + +Find first L object matching the given criteria. +This method accept an anon function. + +=cut diff --git a/lib/WWW/EZTV/Link.pm b/lib/WWW/EZTV/Link.pm index 065544b..12b1a81 100644 --- a/lib/WWW/EZTV/Link.pm +++ b/lib/WWW/EZTV/Link.pm @@ -2,8 +2,41 @@ package WWW::EZTV::Link; use Moose; with 'WWW::EZTV::UA'; -# ABSTRACT: EZTV episode link +# ABSTRACT: Episode link +=attr url + +Link address + +=cut has url => is => 'ro', isa => 'Str', required => 1; + +=attr type + +Link type. It can be: + + - magnet + - torrent + - torrent-redirect (URL that do html/js redirect to a torrent file) + - direct + +=cut +has type => is => 'ro', lazy => 1, builder => '_guess_type'; + +sub _guess_type { + my $self = shift; + + if ( $self->url =~ /magnet:/ ) { + return 'magnet'; + } + elsif ( $self->url =~ /\.torrent$/ ) { + return 'torrent'; + } + elsif ( $self->url =~ /bt-chat.com/ ) { + return 'torrent-redirect'; + } + + return 'direct'; +} 1; diff --git a/lib/WWW/EZTV/Show.pm b/lib/WWW/EZTV/Show.pm index 4b8761c..7ab346b 100644 --- a/lib/WWW/EZTV/Show.pm +++ b/lib/WWW/EZTV/Show.pm @@ -4,7 +4,7 @@ with 'WWW::EZTV::UA'; use WWW::EZTV::Link; use WWW::EZTV::Episode; -# ABSTRACT: EZTV show object +# ABSTRACT: Show object has title => is => 'ro', isa => 'Str', required => 1; has name => is => 'ro', lazy => 1, default => \&_name; diff --git a/t/02-find.t b/t/02-find.t index 37d6a6e..ec2de31 100644 --- a/t/02-find.t +++ b/t/02-find.t @@ -24,6 +24,10 @@ subtest 'Find episodes' => sub { diag( 'Version: ' . $ep->version ); diag( 'Size: ' . $ep->size ); is( $ep->name, 'The Walking Dead', 'Name looks good' ); + + ok( $ep->has_links, 'Episode has links' ); + ok( my $link = $ep->find_link(sub{ $_->type eq 'torrent' }), 'Find a torrent link' ); + ok( $link->url, 'Link has URL' ); }; done_testing(); -- 2.20.1