Created
January 11, 2009 13:18
-
-
Save yusukebe/45698 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Moose; | |
use CGI; | |
use DateTime::Duration; | |
use URI; | |
use Web::Scraper; | |
our $VERSION = '0.01'; | |
has 'url' => ( is => 'rw', isa => 'Str', required => 1 ); | |
has 'id' => ( is => 'rw', isa => 'Int' ); | |
has 'title' => ( is => 'rw', isa => 'Str' ); | |
has 'category' => ( is => 'rw', isa => 'Str' ); | |
has '_scraper' => ( | |
is => 'rw', | |
isa => 'Web::Scraper', | |
default => sub { | |
scraper { | |
process 'param[name~="FlashVars"]', params => '@value'; | |
process 'div.optionsPlacement', options => 'HTMl'; | |
result 'params', 'options'; | |
} | |
} | |
); | |
has '_query' => ( is => 'rw', isa => 'CGI' ); | |
has '_options' => ( is => 'rw', isa => 'Str' ); | |
__PACKAGE__->meta->make_immutable; | |
no Moose; | |
sub BUILD { | |
my ( $self, $args ) = @_; | |
if($self->url =~ m|tube8.com/(.+?)/(.+?)/(\d+)|){ | |
$self->category($1); | |
$self->title($2); | |
$self->id($3); | |
}else{ | |
die "url is not www.tube8.com url"; | |
} | |
} | |
sub flv_url { | |
my $self = shift; | |
$self->_scrape; | |
return $self->_query->param("videoUrl"); | |
} | |
sub duration { | |
my $self = shift; | |
$self->_scrape; | |
if ( $self->_options =~ /Duration.*?(\d{2}):(\d{2})/ ) { | |
return DateTime::Duration->new( | |
minutes => $1, | |
seconds => $2 | |
); | |
} | |
} | |
sub _scrape { | |
my $self = shift; | |
if ( $self->_query ) { | |
return 1; | |
} | |
my $ref = $self->_scraper->scrape( URI->new( $self->url ) ); | |
$self->_query( CGI->new($ref->{params}) ); | |
$self->_options( $ref->{options} ); | |
} | |
1; | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment