+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);
+}
+
+# Take an encoded url_encoded_fmt_stream_map and return a hash
+# matching video IDs to download URLs
+sub _decode_url_encoded_fmt_stream_map {
+ my $self = shift;
+ my $data = shift;
+ my $dataencoded = shift;
+ my @data;
+ my $h = {};
+
+ $data = $self->decode_hexurl($data) if $dataencoded;
+ # This will
+ # - Split the decoded string into segments (along ,)
+ # - Interpret each segment as a concatenated key-value list (key and value separated by =, pairs separated by &
+ # - URL-decode each key and value _again_
+ #
+ # @data will be an array of hash references
+
+ @data = map { { map { $self->decode_hexurl($_) } split /[&=]/ } } split /,/, $data;
+ $self->debug("_decode_url_encoded_fmt_stream_map() decoded %s", Dumper(\@data));
+
+ # From each array entry, pick the itag and the url values and return that
+ # as a hash reference
+
+
+ foreach (@data) {
+ if (exists($_->{'sig'})) {
+ $h->{$_->{'itag'}} = sprintf('%s&signature=%s', $_->{'url'}, $_->{'sig'});
+ } else {
+ $h->{$_->{'itag'}} = $_->{'url'};
+ }
+ }
+
+ return $h;
+}
+
+
+
+sub __pick_url {
+ my $self = shift;
+ my $urls = shift;
+ my $preflist = shift;
+ my $audiopreflist = shift;
+ my $metadata = shift;
+ my $picked_format;
+
+ 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->{$_};
+ $picked_format = $_;
+ if (exists($videoformats{$_})) {
+ $metadata->{'FORMAT'} = $videoformats{$_};
+ } else {
+ $metadata->{'FORMAT'} = 'unknown';
+ }
+ last;
+ }
+ }
+
+ if (defined($picked_format)) {
+ if ($metadata->{'FORMAT'} =~ m/Adaptive/) {
+ if (defined($audiopreflist)) {
+ # Pick an audio format as well
+ foreach (@{$audiopreflist}) {
+ if (exists($urls->{$_})) {
+ $self->debug("Selected audio URL with quality level %s", $_);
+ $metadata->{'DLURL_AUDIO'} = $urls->{$_};
+ if (exists($videoformats{$_})) {
+ $metadata->{'FORMAT_AUDIO'} = $videoformats{$_};
+ } else {
+ $metadata->{'FORMAT_AUDIO'} = 'unknown';
+ }
+ last;
+ }
+ }
+ } else {
+ $self->error('Need an audio preflist, but none provided');
+ }
+ }
+ }
+
+ $self->debug('URL found: %s', $metadata->{'DLURL'});
+}
+