Youtube: Add video format 43 (WebM)
[videosite.git] / videosite / YouTubeGrabber.pm
index dd1b2af..eca896b 100644 (file)
@@ -1,25 +1,62 @@
+# (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+#
+# Grabber for youtube.com/de/...
 #
 # 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 HTML::TokeParser;
+use HTML::Entities qw(decode_entities);
+use Encode;
 use Data::Dumper;
+use videosite::JSArrayParser;
 
 use strict;
 
+my %preflist = (
+    'insane' => [38, 37, 22, 35, 18, 34, 6, 5, 43],
+    'hd' => [37, 22, 35, 18, 34, 6, 5, 38, 43],
+    'h264' => [18, 34, 37, 22, 35, 6, 5, 38, 43],
+    'high' => [34, 35, 18, 37, 22, 6, 5, 38, 43],
+    'normal' => [6, 5, 34, 35, 18, 22, 37, 38, 43]);
+my %videoformats = (
+    43 => 'webm',
+    38 => 'mp4,h264,4k',
+    37 => 'mp4,h264,1080p',
+    35 => 'flv,h264,large',
+    34 => 'flv,h264',
+    22 => 'mp4,h264,720p',
+    18 => 'mp4,h264',
+    5 => 'flv,flv',
+    );
+
 sub new {
     my $class = shift;
     my $self = $class->SUPER::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->{'PATTERNS'} = ['(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/watch(?:_popup)?\?(?:.+=.+&)*v=([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/watch\#\!v=([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/user/[[:alnum:]]+\?v=([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/(?:user/)?[[:alnum:]]+#p/(?:\w+/)+\d+/([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtu\.be/watch\?v=([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtu\.be/([-a-zA-Z0-9_]+))',
+                           '(https?://(?:[-a-zA-Z0-9_.]+\.)*youtube\.(?:com|de|co.uk)/user/\w+\?.*/([-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();
@@ -31,46 +68,268 @@ sub _parse {
     my $self = shift;
     my $url = shift;
     my $pattern = shift;
+    my $id;
+    my $res;
+
+    my $url =~ m|$pattern|;
+    my $url = $1;
+    my $id = $2;
+
+    $self->debug("Matched id %s from pattern %s", $id, $pattern);
+
+    $res = $self->_parse_by_video_info($url, $id);
+    if (defined($res) && ref($res)) {
+        return $res;
+    } else {
+        $res = $self->_parse_by_scrape($url, $id);
+    }
+
+    return $res;
+}
+
+sub _parse_by_video_info {
+    my $self = shift;
+    my $url = shift;
+    my $id = shift;
+    my $quality = $self->_getval('QUALITY');
+    my $metadata;
+    my $videourl;
+    my $ua = $self->ua();
+    my $preflist;
+    my $r;
+    my $content;
+    my $urls;
+
+    $metadata->{'URL'} = $url;
+    $metadata->{'ID'} = $id;
+    $metadata->{'TYPE'} = 'video';
+    $metadata->{'SOURCE'} = $self->{'NAME'};
+    $metadata->{'TITLE'} = undef;
+    $metadata->{'DLURL'} = undef;
+    $metadata->{'COOKIE'} = undef;
+
+    $preflist = $preflist{$quality};
+    $self->debug("Quality: %s, preflist: [%s]", $quality, join(", ", @{$preflist}));
+
+    $videourl = sprintf('https://www.youtube.com/get_video_info?video_id=%s&eurl=%s',
+            $id, 'http%3A%2F%2Fwww%2Eyoutube%2Ecom%2F');
+    $self->debug("Video info URL: %s", $videourl);
+
+    $r = $ua->get($videourl);
+    unless($r->is_success()) {
+        $self->debug('Could not download %s: %s', $videourl, $r->code());
+        return undef;
+    }
+
+    $content = $r->content();
+    $self->debug('Content from get_video_info: %s', $content);
+
+    # Decode content
+    $content = { split /[&=]/, $content };
+
+    if ($content->{'status'} ne 'ok') {
+        $self->debug("Non OK status code found: %s", $content->{'status'});
+        return undef;
+    }
+
+    unless(exists($content->{'fmt_url_map'}) and exists($content->{'title'})) {
+        $self->debug("No fmt_url_map or no title found");
+        return undef;
+    }
+
+    # Decode fmt_url_map
+    $urls = $content->{'fmt_url_map'};
+    $urls =~ s/%(..)/chr(hex($1))/ge;
+    $urls = { split /[\|,]/, $urls };
+
+    $self->__pick_url($urls, $preflist, $metadata);
+
+    $metadata->{'TITLE'} = $content->{'title'};
+    $metadata->{'TITLE'} =~ s/\+/ /g;
+    $metadata->{'TITLE'} =~ s/%(..)/chr(hex($1))/ge;
+    $metadata->{'TITLE'} = decode("utf8", $metadata->{'TITLE'});
+
+    $self->debug('Title found: %s', $metadata->{'TITLE'});
+
+    unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
+        $self->error('Could not determine download URL');
+        return undef;
+    }
+
+    return $metadata;
+}
+
+sub _parse_by_scrape {
+    my $self = shift;
+    my $url = shift;
+    my $id = shift;
     my $content;
     my $metadata = {};
-    my $p = HTML::Parser->new(api_version => 3);
-    my @accum;
-    my @text;
+    my $p;
     my $e;
-
-    $url =~ m|$pattern|;
-    $url = $1;
+    my $tag;
+    my $ua = $self->ua();
+    my $r;
+    my $videourl;
+    my $quality = $self->_getval('QUALITY');
+    my $preflist;
+    my $jsp;
 
     $metadata->{'URL'} = $url;
-    $metadata->{'ID'} = $2;
-    $metadata->{'TYPE'} = 'youtube';
+    $metadata->{'ID'} = $id;
+    $metadata->{'TYPE'} = 'video';
+    $metadata->{'SOURCE'} = $self->{'NAME'};
     $metadata->{'TITLE'} = undef;
     $metadata->{'DLURL'} = undef;
+    $metadata->{'COOKIE'} = 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('https://www.youtube.com/watch?v=%s', $id);
+
+    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');
+        my @logindata = $self->__login($videourl, $ua);
+        $r = $logindata[0];
+        $metadata->{'COOKIE'} = $logindata[1];
+        unless(defined($r)) {
+            $self->error('Could not log into YouTube');
+            return undef;
         }
     }
+    $content = $r->content();
+
+    $p = HTML::TokeParser->new(\$content);
+
+    SWF_ARGS: while ($tag = $p->get_tag('div', 'meta', 'script')) {
+        if ('meta' eq $tag->[0]) {
+            if (exists($tag->[1]->{'name'}) and ('title' eq $tag->[1]->{'name'})) {
+                $metadata->{'TITLE'} = $tag->[1]->{'content'};
+                # Convert HTML entities in the title. This is a bit convoluted.
+                $metadata->{'TITLE'} = decode_entities(
+                                           decode("utf8", $metadata->{'TITLE'}));
+                    
+                $self->debug('Title found: %s', $metadata->{'TITLE'});
+            }
+        } elsif ('script' eq $tag->[0]) {
+            my %urls;
+
+            $e = $p->get_text();
+            $self->debug("Found script: %s", $e);
+
+            if ($e =~ m|\x27SWF_ARGS\x27:\s+(.+),|) {
+                my $args = $1;
+
+                $self->debug("Found SWF_ARGS: %s", $args);
+                $jsp = videosite::JSArrayParser->new();
+                $self->debug("Using %s to parse", ref($jsp));
+                $r = $jsp->parse($args);
+
+                unless(defined($r)) {
+                    $self->error("Found information hash, but could not parse");
+                    return undef;
+                }
+
+                if (exists($r->{'fmt_url_map'}) and ($r->{'fmt_url_map'} ne '')) {
+                    my $urls =  $r->{'fmt_url_map'};
+
+                    $self->debug("Video has fmt_url_map: %s", $urls);
+
+                    $urls =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
+                    %urls = split(/[\|,]/, $urls);
+                    $self->debug("Pagetype: old (SWF_ARGS), fmt_url_map");
+
+                } elsif (exists($r->{'t'}) and ($r->{'t'} ne '')) {
+                    my $thash = $r->{'t'};
 
-    # 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;
+                    if (exists($r->{'fmt_map'}) && ($r->{'fmt_map'} ne '')) {
+                        my $fmt = $r->{'fmt_map'};
+                        my @fmt;
+
+                        $self->debug('Video has fmt_map');
+                        $fmt =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
+                        @fmt = split(/,/, $fmt);
+                        foreach (@fmt) {
+                            @_=split(/\//);
+                            $urls{$_[0]} =  sprintf('http://www.youtube.com/get_video?video_id=%s&fmt=%d&t=%s', 
+                                $metadata->{'ID'},
+                                $_[0],
+                                $thash);
+                        }
+                        $self->debug("Pagetype: 2009 (SWF_ARGS), t with fmt_map");
+
+                    } else {
+                        $urls{5} = sprintf('http://www.youtube.com/get_video?video_id=%s&t=%s',
+                            $metadata->{'ID'},
+                            $thash);
+                        $self->debug("Pagetype: 2009 (SWF_ARGS), t without fmt_map");
+                    }
+                } else {
+                    $self->error('Neither fmt_url_map nor t found in video information hash');
+                    return undef;
+                }
+            } elsif ($e =~ m|var swfHTML = .*fmt_url_map=([^\&]+)\&|) {
+                my $urls = $1;
+                $self->debug("Video has fmt_url_map: %s", $urls);
+
+                $urls =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
+                %urls = split(/[\|,]/, $urls);
+                $self->debug("Pagetype: 2010 (swfHTML), fmt_url_map");
+            } elsif ($e =~ m|\x27PLAYER_CONFIG\x27:\s+(.+)\}\);|) {
+                my $args = $1;
+                $self->debug("Found PLAYER_CONFIG: %s", $args);
+
+                $jsp = videosite::JSArrayParser->new();
+                $self->debug("Using %s to parse", ref($jsp));
+                $r = $jsp->parse($args);
+
+                unless(defined($r)) {
+                    $self->error("Found information hash, but could not parse");
+                    return undef;
+                }
+
+                if (exists($r->{'args'}) and exists($r->{'args'}->{'ps'}) and ($r->{'args'}->{'ps'} eq 'live')) {
+                    $self->error("Video URL seems to point to a live stream, cannot save this");
+                    return undef;
+                }
+
+                if (exists($r->{'args'}) and exists($r->{'args'}->{'fmt_url_map'}) and ($r->{'args'}->{'fmt_url_map'} ne '')) {
+                    my $urls = $r->{'args'}->{'fmt_url_map'};
+
+                    $self->debug("Video has fmt_url_map: %s", $urls);
+
+                    %urls = split(/[\|,]/, $urls);
+                    foreach (keys(%urls)) {
+                        my $u = $urls{$_};
+                        $u =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
+                        $urls{$_} = $u;
+                    }
+                    $self->debug("Pagetype: 2011 (PLAYER_CONFIG), fmt_url_map");
+                } else {
+                    $self->error('fmt_url_map not found in PLAYER_CONFIG');
+                    return undef;
+                }
+            }
+
+            if (%urls) {
+                $self->__pick_url(\%urls, $preflist, $metadata);
+                last SWF_ARGS;
+            }
+        } elsif ('div' eq $tag->[0]) {
+            if (exists($tag->[1]->{'id'}) and ('watch-player-unavailable-message-container' eq $tag->[1]->{'id'})) {
+                # Search forward to the next <div>
+                $tag = $p->get_tag('div');
+                $self->error("Could not get video data for youtube %s: %s",
+                        $metadata->{'ID'}, $p->get_trimmed_text());
+                return undef;
+            }
         }
     }
 
@@ -82,4 +341,137 @@ 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;
+    }
+
+    sub get_all_cookies {
+
+        my $jar = shift;
+        my $key = shift;
+        my $val = "";
+        $jar->scan(sub { $val .= "; " if !( $val eq "" ); $val .= "$_[1]=$_[2]" });
+
+        return $val;
+    }
+
+    if (($user eq '') or ($pass eq '')) {
+        $self->error('No username or password defined for YouTube');
+        return undef;
+    }
+
+    $self->debug('Logging in');
+    $r = $ua->get('https://www.google.com/accounts/ServiceLoginAuth?service=youtube');
+    unless($r->is_success()) {
+        $self->debug("Could not get login page (make sure your LWP supports HTTPS!)");
+        return undef;
+    }
+    $c = $r->decoded_content();
+    $p = HTML::TokeParser->new(\$c);
+    while (my $tag = $p->get_tag('input')) {
+        $self->debug("%s", Dumper($tag));
+        if ($tag->[1]{name} eq 'GALX') {
+            $token = $tag->[1]{value};
+            last;
+        }
+    }
+    $self->debug("GALX = %s", $token);
+    $r = $ua->post('https://www.google.com/accounts/ServiceLoginAuth?service=youtube', { 'service' => 'youtube', 'Email' => $user, 'Passwd' => $pass, 'GALX' => $token });
+    unless($r->is_success()) {
+        $self->debug("Could not get login page (make sure your LWP supports HTTPS!)");
+        return undef;
+    }
+    $c = $r -> decoded_content();
+    $p = HTML::TokeParser->new(\$c);
+    while (my $tag = $p->get_tag('script')) {
+        if($p->get_text() =~ /location\.replace\("(.+)"\)/) {
+            $token = $1;
+            $token =~ s/\\x([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
+            last;
+        }
+    }
+    $r = $ua->get($token);
+    unless(check_cookie($ua->cookie_jar, 'LOGIN_INFO')) {
+        $self->error('Could not log into YouTube');
+        return undef;
+    }
+
+    $self->debug("Got a cookie");
+
+    $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() =~ /'XSRF_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 });
+    }
+
+# Apparently there is no longer a specific "is_adult" cookie
+# or, by the looks of it, anything similar
+#
+#    unless(check_cookie($ua->cookie_jar, 'is_adult')) {
+#        $self->error('Could not authenticate session');
+#        return undef;
+#    }
+
+    my $cookie = get_all_cookies($ua->cookie_jar);
+    return ($ua->get($videourl), $cookie);
+}
+
+sub __pick_url {
+    my $self = shift;
+    my $urls = shift;
+    my $preflist = shift;
+    my $metadata = shift;
+
+    foreach (keys(%{$urls})) {
+        if (exists($videoformats{$_})) {
+            $self->debug('Found URL for format %s (%s): %s', $_, $videoformats{$_}, $urls->{$_});
+        } else {
+            $self->error('Unknown format %s: %s', $_, $urls->{$_});
+        }
+    }
+
+    foreach (@{$preflist}) {
+        if (exists($urls->{$_})) {
+            $self->debug("Selected URL with quality level %s", $_);
+            $metadata->{'DLURL'} = $urls->{$_};
+            last;
+        }
+    }
+
+    $self->debug('URL found: %s', $metadata->{'DLURL'});
+}
+
 1;