1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 # Grabber for youtube.com/de/...
6 # download strategy revised using
7 # http://www.kde-apps.org/content/show.php?content=41456
9 package YouTubeGrabber;
12 @ISA = qw(GrabberBase);
23 my $self = $class->SUPER::new();
25 $self->{'NAME'} = 'youtube';
26 $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/watch\?(?:.+=.+&)*v=([-a-zA-Z0-9_]+))',
27 '(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))'];
28 $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'], 'USERNAME' => ['', 'Username to use for YouTube login'], 'PASSWORD' => ['', 'Password to use for YouTube login']};
31 $self->_prepare_parameters();
45 my $jar = HTTP::Cookies->new;
46 my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
49 my $quality = $self->_getval('QUALITY');
55 $metadata->{'URL'} = $url;
56 $metadata->{'ID'} = $2;
57 $metadata->{'TYPE'} = 'video';
58 $metadata->{'SOURCE'} = $self->{'NAME'};
59 $metadata->{'TITLE'} = undef;
60 $metadata->{'DLURL'} = undef;
62 if ($quality eq 'high') {
64 } elsif ($quality eq 'h264') {
68 $videourl = sprintf('http://www.youtube.com/watch?v=%s%s', $2, $append);
70 unless(defined($r = $ua->get($videourl))) {
71 $self->error('Could not download %s', $url);
75 if ($r->base->as_string() =~ m,/verify_age,) {
76 $self->debug('Video requires age verification');
77 $ua->cookie_jar($jar);
78 $r = $self->__login($videourl, $ua);
80 $self->error('Could not log into YouTube');
84 $content = $r->content();
86 $p = HTML::TokeParser->new(\$content);
88 while ($tag = $p->get_tag('div', 'meta', 'script')) {
89 if ('meta' eq $tag->[0]) {
90 if ('title' eq $tag->[1]->{'name'}) {
91 $metadata->{'TITLE'} = $tag->[1]->{'content'};
92 $self->debug('Title found: %s', $metadata->{'TITLE'});
94 } elsif ('script' eq $tag->[0]) {
96 if ($e =~ m|/watch_fullscreen\?(.+)\x27|) {
97 my %args = map { split(/=/, $_, 2); } split(/&/, $1);
98 $metadata->{'DLURL'} = sprintf('http://www.youtube.com/get_video.php?video_id=%s&t=%s%s',
99 $metadata->{'ID'}, $args{'t'}, $append);
100 $self->debug('URL found: %s', $metadata->{'DLURL'});
102 } elsif ('div' eq $tag->[0]) {
103 if (exists($tag->[1]->{'class'}) and ('errorBox' eq $tag->[1]->{'class'})) {
104 $self->error("Could not get video data for youtube %s: %s",
105 $metadata->{'ID'}, $p->get_trimmed_text());
111 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
112 $self->error('Could not determine download URL');
121 my $videourl = shift;
123 my $user = $self->_getval('USERNAME');
124 my $pass = $self->_getval('PASSWORD');
136 $jar->scan(sub { $found = 1 if ( $key eq $_[1]) });
141 if (($user eq '') or ($pass eq '')) {
142 $self->error('No username or password defined for YouTube');
146 $self->debug('Logging in');
147 $r = $ua->post('http://www.youtube.com/signup', { 'next' => '/', 'current_form' => 'login', 'action_login' => '1', 'username' => $user, 'password' => $pass });
149 unless(check_cookie($ua->cookie_jar, 'LOGIN_INFO')) {
150 $self->error('Could not log into YouTube');
154 $r = $ua->get($videourl);
155 if ($r->base->as_string() =~ m,/verify_age,) {
156 $self->debug("Looking for session token...");
157 $c = $r->decoded_content();
158 $p = HTML::TokeParser->new(\$c);
159 while (my $tag = $p->get_tag('script')) {
160 if ($p->get_text() =~ /gXSRF_token = '(.+)'/) {
166 unless(defined($token)) {
167 $self->error("Could not find session token");
171 $self->debug('Authenticating session...');
172 $r = $ua->post($r->base->as_string, { 'next_url' => $r->base->path, 'action_confirm' => 'Confirm Birth Date', 'session_token' => $token });
175 unless(check_cookie($ua->cookie_jar, 'is_adult')) {
176 $self->error('Could not authenticate session');
180 return $ua->get($videourl);