# download strategy revised using
# http://www.kde-apps.org/content/show.php?content=41456
-package YouTubeGrabber;
+package videosite::YouTubeGrabber;
-use GrabberBase;
-@ISA = qw(GrabberBase);
+use videosite::GrabberBase;
+@ISA = qw(videosite::GrabberBase);
-use LWP::Simple qw(!get);
-use HTML::Parser;
+use LWP::UserAgent;
+use HTTP::Cookies;
+use HTML::TokeParser;
use Data::Dumper;
use strict;
$self->{'NAME'} = 'youtube';
$self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/watch\?(?:.+=.+&)*v=([-a-zA-Z0-9_]+))',
'(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))'];
+ $self->{'_PARAMS'} = {
+ 'QUALITY' => ['normal', 'Quality of the video to download.', {
+ 'normal' => 'standard resolution flash video',
+ 'high' => 'higher resolution flash video',
+ 'h264' => 'high resolution MPEG4 video',
+ 'hd' => 'HD720 resolution'}],
+ 'USERNAME' => ['', 'Username to use for YouTube login'],
+ 'PASSWORD' => ['', 'Password to use for YouTube login']};
bless($self, $class);
$self->_prepare_parameters();
my $pattern = shift;
my $content;
my $metadata = {};
- my $p = HTML::Parser->new(api_version => 3);
- my @accum;
- my @text;
+ my $p;
my $e;
+ my $tag;
+ my $jar = HTTP::Cookies->new;
+ my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
+ my $r;
+ my $videourl;
+ my $quality = $self->_getval('QUALITY');
+ my %preflist = (
+ 'hd' => [22, 35, 18, 34, 6, 5],
+ 'h264' => [18, 34, 22, 35, 6, 5],
+ 'high' => [34, 35, 18, 22, 6, 5],
+ 'normal' => [6, 5, 34, 35, 18, 22]);
+ my $preflist;
$url =~ m|$pattern|;
$url = $1;
$metadata->{'TITLE'} = undef;
$metadata->{'DLURL'} = undef;
- unless(defined($content = LWP::Simple::get(sprintf('http://youtube.com/watch?v=%s', $2)))) {
+ $preflist = $preflist{$quality};
+ $self->debug("Quality: %s, preflist: [%s]", $quality, join(", ", @{$preflist}));
+
+ $videourl = sprintf('http://www.youtube.com/watch?v=%s', $2);
+
+ unless(defined($r = $ua->get($videourl))) {
$self->error('Could not download %s', $url);
return undef;
}
- if ($content =~ /This video has been removed due to terms of use violation/) {
- $self->error('Video has been removed');
- return undef;
+ if ($r->base->as_string() =~ m,/verify_age,) {
+ $self->debug('Video requires age verification');
+ $ua->cookie_jar($jar);
+ $r = $self->__login($videourl, $ua);
+ unless(defined($r)) {
+ $self->error('Could not log into YouTube');
+ return undef;
+ }
}
+ $content = $r->content();
+
+ $p = HTML::TokeParser->new(\$content);
- $p->handler(start => \@accum, "tagname, attr");
- $p->handler(text => \@text, "text");
- $p->report_tags(qw(meta script));
- $p->utf8_mode(1);
- $p->parse($content);
-
- # Look for the title in the meta tags
- foreach $e (@accum) {
- if ('meta' eq $e->[0]) {
- if ('title' eq $e->[1]->{'name'}) {
- $metadata->{'TITLE'} = $e->[1]->{'content'};
+ while ($tag = $p->get_tag('div', 'meta', 'script')) {
+ if ('meta' eq $tag->[0]) {
+ if ('title' eq $tag->[1]->{'name'}) {
+ $metadata->{'TITLE'} = $tag->[1]->{'content'};
$self->debug('Title found: %s', $metadata->{'TITLE'});
}
- }
- }
+ } elsif ('script' eq $tag->[0]) {
+ $e = $p->get_text();
+ $self->debug("Found script: %s", $e);
+ if ($e =~ m|\x22fmt_url_map\x22\s*:\s*\x22([^\x22]+)\x22|) {
+ $self->debug("Found fmt_url_map: %s", $1);
+ my $urls = $1;
+ my %urls;
+
+ $urls =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
+ %urls = split(/[\|,]/, $urls);
+ $self->debug("Found quality levels [%s]", join(", ", keys(%urls)));
+
+ foreach (keys(%urls)) {
+ if ($_ == 35) {
+ $self->debug('Found flv,h264,large: %s', $urls{$_});
+ } elsif ($_ == 34) {
+ $self->debug('Found flv,h264: %s', $urls{$_});
+ } elsif ($_ == 22) {
+ $self->debug('Found mp4,h264,large: %s', $urls{$_});
+ } elsif ($_ == 18) {
+ $self->debug('Found mp4,h264: %s', $urls{$_});
+ } elsif ($_ == 5) {
+ $self->debug('Found flv,flv: %s', $urls{$_});
+ } else {
+ $self->error('Unknown tag %s: %s', $_, $urls{$_});
+ }
+ }
+
+ foreach (@{$preflist}) {
+ if (exists($urls{$_})) {
+ $self->debug("Selected URL with quality level %s", $_);
+ $metadata->{'DLURL'} = $urls{$_};
+ last;
+ }
+ }
- # Look for the download URL
- foreach $e (@text) {
- if ($e->[0] =~ m|/watch_fullscreen\?(.+)\x27|) {
- my %args = map { my @a = split(/=/); ($a[0], $a[1]) } split(/&/, $1);
- $metadata->{'DLURL'} = sprintf('http://www.youtube.com/get_video.php?video_id=%s&t=%s',
- $metadata->{'ID'}, $args{'t'});
$self->debug('URL found: %s', $metadata->{'DLURL'});
+ }
+ } elsif ('div' eq $tag->[0]) {
+ if (exists($tag->[1]->{'class'}) and ('errorBox' eq $tag->[1]->{'class'})) {
+ $self->error("Could not get video data for youtube %s: %s",
+ $metadata->{'ID'}, $p->get_trimmed_text());
+ return undef;
+ }
}
}
return $metadata;
}
+sub __login {
+ my $self = shift;
+ my $videourl = shift;
+ my $ua = shift;
+ my $user = $self->_getval('USERNAME');
+ my $pass = $self->_getval('PASSWORD');
+ my $r;
+ my $p;
+ my $c;
+ my $token;
+
+ sub check_cookie {
+
+ my $jar = shift;
+ my $key = shift;
+ my $found = undef;
+
+ $jar->scan(sub { $found = 1 if ( $key eq $_[1]) });
+
+ return $found;
+ }
+
+ if (($user eq '') or ($pass eq '')) {
+ $self->error('No username or password defined for YouTube');
+ return undef;
+ }
+
+ $self->debug('Logging in');
+ $r = $ua->post('http://www.youtube.com/signup', { 'next' => '/', 'current_form' => 'login', 'action_login' => '1', 'username' => $user, 'password' => $pass });
+
+ unless(check_cookie($ua->cookie_jar, 'LOGIN_INFO')) {
+ $self->error('Could not log into YouTube');
+ return undef;
+ }
+
+ $r = $ua->get($videourl);
+ if ($r->base->as_string() =~ m,/verify_age,) {
+ $self->debug("Looking for session token...");
+ $c = $r->decoded_content();
+ $p = HTML::TokeParser->new(\$c);
+ while (my $tag = $p->get_tag('script')) {
+ if ($p->get_text() =~ /gXSRF_token = '(.+)'/) {
+ $token = $1;
+ last;
+ }
+ }
+
+ unless(defined($token)) {
+ $self->error("Could not find session token");
+ return undef;
+ }
+
+ $self->debug('Authenticating session...');
+ $r = $ua->post($r->base->as_string, { 'next_url' => $r->base->path, 'action_confirm' => 'Confirm Birth Date', 'session_token' => $token });
+ }
+
+ unless(check_cookie($ua->cookie_jar, 'is_adult')) {
+ $self->error('Could not authenticate session');
+ return undef;
+ }
+
+ return $ua->get($videourl);
+}
+
1;