- Add YouTube authentication for videos that require it
[videosite.git] / videosite / YouTubeGrabber.pm
index c5ee0f3..617b65d 100644 (file)
@@ -11,8 +11,9 @@ package YouTubeGrabber;
 use GrabberBase;
 @ISA = qw(GrabberBase);
 
-use LWP::Simple qw(!get);
-use HTML::Parser;
+use LWP::UserAgent;
+use HTTP::Cookies;
+use HTML::TokeParser;
 use Data::Dumper;
 
 use strict;
@@ -24,6 +25,7 @@ sub new {
     $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'} = {'USERNAME' => ['', 'Username to use for YouTube login'], 'PASSWORD' => ['', 'Password to use for YouTube login']};
 
     bless($self, $class);
     $self->_prepare_parameters();
@@ -37,10 +39,13 @@ sub _parse {
     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;
 
     $url =~ m|$pattern|;
     $url = $1;
@@ -52,30 +57,46 @@ sub _parse {
     $metadata->{'TITLE'} = undef;
     $metadata->{'DLURL'} = undef;
 
-    unless(defined($content = LWP::Simple::get(sprintf('http://youtube.com/watch?v=%s', $2)))) {
+    $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;
     }
 
-    $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'};
-            }
+    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->decoded_content();
 
-    # Look for the download URL
-    foreach $e (@text) {
-        if ($e->[0] =~ m|/watch_fullscreen\?(.*)\&fs|) {
-            $metadata->{'DLURL'} = 'http://www.youtube.com/get_video.php?' . $1;
+    $p = HTML::TokeParser->new(\$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();
+            if ($e =~ m|/watch_fullscreen\?(.+)\x27|) {
+                my %args = map { split(/=/, $_, 2); } 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 ('errorBox' eq $tag->[1]->{'class'}) {
+                $self->error("Could not get video data for youtube %s: %s",
+                        $metadata->{'ID'}, $p->get_trimmed_text());
+                return undef;
+            }
         }
     }
 
@@ -87,4 +108,68 @@ sub _parse {
     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;