| 1 | package WWW::Tube8; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use Carp qw( croak ); |
|---|
| 6 | |
|---|
| 7 | use version; our $VERSION = qv('1.0.0'); |
|---|
| 8 | |
|---|
| 9 | use LWP::UserAgent; |
|---|
| 10 | |
|---|
| 11 | use base qw(Class::Accessor::Fast); |
|---|
| 12 | __PACKAGE__->mk_accessors( |
|---|
| 13 | qw( flv thumb get_3gp |
|---|
| 14 | url id title title_inurl |
|---|
| 15 | category category_url duration related_videos ) |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | sub new { |
|---|
| 19 | my $class = shift; |
|---|
| 20 | my $opt = shift; |
|---|
| 21 | croak "opt needs hash ref" if ref $opt ne 'HASH'; |
|---|
| 22 | my $self = bless $opt, $class; |
|---|
| 23 | |
|---|
| 24 | $self->{ua} = LWP::UserAgent->new( agent => 'WWW::Tube8' ) |
|---|
| 25 | unless $self->{ua}; |
|---|
| 26 | |
|---|
| 27 | croak "url is required" unless $self->url; |
|---|
| 28 | croak "url is wrong (perhaps not movie page of tube8.com)" |
|---|
| 29 | if $self->url !~ m!^http://www.tube8.com/[^/]+/([^/]+)/(\d+)/!; |
|---|
| 30 | $self->title_inurl($1); |
|---|
| 31 | $self->id($2); |
|---|
| 32 | $self->_get_info; |
|---|
| 33 | |
|---|
| 34 | return $self; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | sub _get_info { |
|---|
| 38 | my $self = shift; |
|---|
| 39 | |
|---|
| 40 | my $res = $self->{ua}->get( $self->url ); |
|---|
| 41 | croak "can't get tube8 page" unless $res->is_success; |
|---|
| 42 | my $tube8_page = $res->content; |
|---|
| 43 | |
|---|
| 44 | while ( $tube8_page |
|---|
| 45 | =~ s/so\.addVariable\('(videoUrl|imageUrl)','([^']+\.(?:flv|jpg))'\);// |
|---|
| 46 | ) |
|---|
| 47 | { |
|---|
| 48 | my ( $key, $value ) = ( $1, $2 ); |
|---|
| 49 | if ( $key eq 'videoUrl' ) { |
|---|
| 50 | $self->flv($value); |
|---|
| 51 | } |
|---|
| 52 | else { |
|---|
| 53 | $self->thumb( 'http://www.tube8.com' . $value ); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | $self->get_3gp( |
|---|
| 57 | $tube8_page =~ /<a href="([^"]+\.3gp)">Download video for mobile/ ); |
|---|
| 58 | $self->title( $tube8_page =~ /<strong>Title<\/strong>: ([^<]+)/ ); |
|---|
| 59 | $self->duration( $tube8_page =~ /<strong>Duration<\/strong>: ([^<]+)/ ); |
|---|
| 60 | $tube8_page |
|---|
| 61 | =~ /<strong>Category<\/strong>: <a href='([^']+)'><b>([^<]+)<\/b><\/a>/; |
|---|
| 62 | $self->category_url($1); |
|---|
| 63 | $self->category($2); |
|---|
| 64 | $self->_get_related($tube8_page); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | sub _get_related { |
|---|
| 68 | my $self = shift; |
|---|
| 69 | my $tube8_page = shift; |
|---|
| 70 | |
|---|
| 71 | my @related_videos; |
|---|
| 72 | while ( $tube8_page |
|---|
| 73 | =~ s!<a href="([^"]+)"><span class="VideoTitles">([^<]+)</span></a>!! |
|---|
| 74 | ) |
|---|
| 75 | { |
|---|
| 76 | push @related_videos, { url => $1, title => $2, }; |
|---|
| 77 | } |
|---|
| 78 | $self->set('related_videos', @related_videos); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | 1; |
|---|
| 82 | |
|---|
| 83 | __END__ |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | =head1 NAME |
|---|
| 87 | |
|---|
| 88 | WWW::Tube8 - Get video informations from tube8.com |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | =head1 SYNOPSIS |
|---|
| 92 | |
|---|
| 93 | use LWP::UserAgent; |
|---|
| 94 | use WWW::Tube8; |
|---|
| 95 | |
|---|
| 96 | my $ua = LWP::UserAgent->new( |
|---|
| 97 | timeout => 30, |
|---|
| 98 | ); |
|---|
| 99 | |
|---|
| 100 | my $t8 = WWW::Tube8->new({ |
|---|
| 101 | url => 'http://www.tube8.com/category/hoge-hoge-/00000/', |
|---|
| 102 | ua => $ua, # optional |
|---|
| 103 | }); |
|---|
| 104 | |
|---|
| 105 | print $t8->flv . "\n"; |
|---|
| 106 | print $t8->thumb . "\n"; |
|---|
| 107 | print $t8->get_3gp . "\n"; |
|---|
| 108 | print $t8->url . "\n"; |
|---|
| 109 | print $t8->id . "\n"; |
|---|
| 110 | print $t8->title . "\n"; |
|---|
| 111 | print $t8->title_inurl . "\n"; |
|---|
| 112 | print $t8->category . "\n"; |
|---|
| 113 | print $t8->category_url . "\n"; |
|---|
| 114 | print $t8->duration . "\n"; |
|---|
| 115 | for my $rv ( @{ $t8->related_videos } ) { |
|---|
| 116 | print "$rv->{title}\t$rv->{url}\n"; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | =head1 METHOD |
|---|
| 121 | |
|---|
| 122 | =over |
|---|
| 123 | |
|---|
| 124 | =item new(I<$hash_ref>) |
|---|
| 125 | |
|---|
| 126 | Creates a new WWW::Tube8 instance. |
|---|
| 127 | required param is url only. |
|---|
| 128 | you can get video infomations like follow. |
|---|
| 129 | |
|---|
| 130 | =item flv |
|---|
| 131 | |
|---|
| 132 | =item thumb |
|---|
| 133 | |
|---|
| 134 | =item get_3gp |
|---|
| 135 | |
|---|
| 136 | =item url |
|---|
| 137 | |
|---|
| 138 | =item id |
|---|
| 139 | |
|---|
| 140 | =item title |
|---|
| 141 | |
|---|
| 142 | =item title_inurl |
|---|
| 143 | |
|---|
| 144 | =item category |
|---|
| 145 | |
|---|
| 146 | =item category_url |
|---|
| 147 | |
|---|
| 148 | =item duration |
|---|
| 149 | |
|---|
| 150 | =item related_videos |
|---|
| 151 | |
|---|
| 152 | =back |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | =head1 AUTHOR |
|---|
| 156 | |
|---|
| 157 | Copyright (c) 2009, Dai Okabayashi C<< <bayashi@cpan.org> >> |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | =head1 LICENCE |
|---|
| 161 | |
|---|
| 162 | This module is free software; you can redistribute it and/or |
|---|
| 163 | modify it under the same terms as Perl itself. See L<perlartistic>. |
|---|
| 164 | |
|---|
| 165 | =cut |
|---|
| 166 | |
|---|