Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

New substitution variable {user} #7

Merged
merged 3 commits into from

2 participants

@isync

I've added code to extract the YouTube username as well, so anyone using this here can add info about the video owner (or uploader) to downloaded videos. Also, added some preliminary test code for that.

Again, thumbs up, xaicron, for giving us all a Perl alternative to youtube-dl!

isync added some commits
@isync isync Update lib/WWW/YouTube/Download.pm
Added that the YouTube video user is parsed out and offered as yet another substitution variable in file naming.
1750e69
@isync isync Create video_user.t
Added tests for parsing out of YT user(names), untested, meant as a starting point.
6e1e57f
@xaicron
Owner

Thanks a patch. But, t/private/video_user.t is broken. Please fix it.

@isync isync Update t/private/video_user.t
Was a mock up, now works.
Also: we should ask users if it is okay to connect to the Internet for testing!
917384c
@isync

Done.
Please add a prompt() to the Build.PL to ask users if it is okay to connect to the Internet / YouTube server for testing...

@xaicron xaicron merged commit 3bf3d2a into from
@xaicron
Owner

Okay. However, if the video was deleted then test will be fail...
I will change the test cases that parse static HTML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2013
  1. @isync

    Update lib/WWW/YouTube/Download.pm

    isync authored
    Added that the YouTube video user is parsed out and offered as yet another substitution variable in file naming.
Commits on Jan 22, 2013
  1. @isync

    Create video_user.t

    isync authored
    Added tests for parsing out of YT user(names), untested, meant as a starting point.
Commits on Feb 7, 2013
  1. @isync

    Update t/private/video_user.t

    isync authored
    Was a mock up, now works.
    Also: we should ask users if it is okay to connect to the Internet for testing!
This page is out of date. Refresh to see the latest.
Showing with 49 additions and 1 deletion.
  1. +14 −1 lib/WWW/YouTube/Download.pm
  2. +35 −0 t/private/video_user.t
View
15 lib/WWW/YouTube/Download.pm
@@ -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';
@@ -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',
@@ -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 = [];
@@ -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,
@@ -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) = @_;
@@ -335,6 +345,7 @@ C<< filename >> supported format:
{video_id}
{title}
+ {user}
{fmt}
{suffix}
{resolution}
@@ -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)>
View
35 t/private/video_user.t
@@ -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
Something went wrong with that request. Please try again.