Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New substitution variable {user} #7

Merged
merged 3 commits into from
Feb 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/WWW/YouTube/Download.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sub new {
bless \%args, $class;
}

for my $name (qw[video_id video_url title fmt fmt_list suffix]) {
for my $name (qw[video_id video_url title user fmt fmt_list suffix]) {
no strict 'refs';
*{"get_$name"} = sub {
use strict 'refs';
Expand Down Expand Up @@ -64,6 +64,7 @@ sub download {
my $filename = $self->_format_filename($args->{filename}, {
video_id => $data->{video_id},
title => $data->{title},
user => $data->{user},
fmt => $fmt,
suffix => $data->{video_url_map}{$fmt}{suffix} || _suffix($fmt),
resolution => $data->{video_url_map}{$fmt}{resolution} || '0x0',
Expand Down Expand Up @@ -125,6 +126,7 @@ sub prepare_download {

my $content = $self->_get_content($video_id);
my $title = $self->_fetch_title($content);
my $user = $self->_fetch_user($content);
my $video_url_map = $self->_fetch_video_url_map($content);

my $fmt_list = [];
Expand All @@ -147,6 +149,7 @@ sub prepare_download {
video_id => $video_id,
video_url => $hq_data->{url},
title => $title,
user => $user,
video_url_map => $video_url_map,
fmt => $hq_data->{fmt},
fmt_list => $fmt_list,
Expand All @@ -161,6 +164,13 @@ sub _fetch_title {
return decode_entities($title);
}

sub _fetch_user {
my ($self, $content) = @_;

my ($user) = $content =~ /<span class="yt-user-name\s+?" dir="ltr">([^<]+)<\/span>/ or return;
return decode_entities($user);
}

sub _fetch_video_url_map {
my ($self, $content) = @_;

Expand Down Expand Up @@ -335,6 +345,7 @@ C<< filename >> supported format:

{video_id}
{title}
{user}
{fmt}
{suffix}
{resolution}
Expand All @@ -358,6 +369,8 @@ Sets and gets LWP::UserAgent object.

=item B<get_title($video_id)>

=item B<get_user($video_id)>

=item B<get_fmt($video_id)>

=item B<get_fmt_list($video_id)>
Expand Down
35 changes: 35 additions & 0 deletions t/private/video_user.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use strict;
use warnings;
use Test::More tests => 4;
use WWW::YouTube::Download;

my $wyd = WWW::YouTube::Download->new();

sub test_video_user {
my ($url, $expects) = @_;
my $data = $wyd->prepare_download($url);
is( $data->{user}, $expects);
}

test_video_user(
'http://www.youtube.com/watch?v=1sV8Z_Lmpt4',
'GoogleTechTalks',
);

test_video_user(
'http://www.youtube.com/watch?v=o3hu3iG8B2g',
'Real454545',
);

test_video_user(
'http://www.youtube.com/watch?v=Wa3qBsHfZjI',
'Tatsuo YAMASHITA', # username is /user/ytoytoyto BUT displayed name is "Tatsuo YAMASHITA"
);

test_video_user(
'http://www.youtube.com/watch?v=gAWiXbT599E',
'azamsharp',
);


done_testing