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 videosite::YouTubeGrabber;
11 use videosite::GrabberBase;
12 @ISA = qw(videosite::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'} = {
29 'QUALITY' => ['normal', 'Quality of the video to download.', {
30 'normal' => 'standard resolution flash video',
31 'high' => 'higher resolution flash video',
32 'h264' => 'high resolution MPEG4 video',
33 'hd' => 'HD720 resolution'}],
34 'USERNAME' => ['', 'Username to use for YouTube login'],
35 'PASSWORD' => ['', 'Password to use for YouTube login']};
38 $self->_prepare_parameters();
52 my $jar = HTTP::Cookies->new;
53 my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
56 my $quality = $self->_getval('QUALITY');
62 $metadata->{'URL'} = $url;
63 $metadata->{'ID'} = $2;
64 $metadata->{'TYPE'} = 'video';
65 $metadata->{'SOURCE'} = $self->{'NAME'};
66 $metadata->{'TITLE'} = undef;
67 $metadata->{'DLURL'} = undef;
69 if ($quality eq 'high') {
71 } elsif ($quality eq 'h264') {
73 } elsif ($quality eq 'hd') {
77 $videourl = sprintf('http://www.youtube.com/watch?v=%s%s', $2, $append);
79 unless(defined($r = $ua->get($videourl))) {
80 $self->error('Could not download %s', $url);
84 if ($r->base->as_string() =~ m,/verify_age,) {
85 $self->debug('Video requires age verification');
86 $ua->cookie_jar($jar);
87 $r = $self->__login($videourl, $ua);
89 $self->error('Could not log into YouTube');
93 $content = $r->content();
95 $p = HTML::TokeParser->new(\$content);
97 while ($tag = $p->get_tag('div', 'meta', 'script')) {
98 if ('meta' eq $tag->[0]) {
99 if ('title' eq $tag->[1]->{'name'}) {
100 $metadata->{'TITLE'} = $tag->[1]->{'content'};
101 $self->debug('Title found: %s', $metadata->{'TITLE'});
103 } elsif ('script' eq $tag->[0]) {
105 if ($e =~ m|/watch_fullscreen\?(.+)\x27|) {
106 my %args = map { split(/=/, $_, 2); } split(/&(?!amp;)/, $1);
107 $metadata->{'DLURL'} = sprintf('http://www.youtube.com/get_video.php?video_id=%s&t=%s%s',
108 $metadata->{'ID'}, $args{'t'}, $append);
109 $self->debug('URL found: %s', $metadata->{'DLURL'});
111 } elsif ('div' eq $tag->[0]) {
112 if (exists($tag->[1]->{'class'}) and ('errorBox' eq $tag->[1]->{'class'})) {
113 $self->error("Could not get video data for youtube %s: %s",
114 $metadata->{'ID'}, $p->get_trimmed_text());
120 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
121 $self->error('Could not determine download URL');
130 my $videourl = shift;
132 my $user = $self->_getval('USERNAME');
133 my $pass = $self->_getval('PASSWORD');
145 $jar->scan(sub { $found = 1 if ( $key eq $_[1]) });
150 if (($user eq '') or ($pass eq '')) {
151 $self->error('No username or password defined for YouTube');
155 $self->debug('Logging in');
156 $r = $ua->post('http://www.youtube.com/signup', { 'next' => '/', 'current_form' => 'login', 'action_login' => '1', 'username' => $user, 'password' => $pass });
158 unless(check_cookie($ua->cookie_jar, 'LOGIN_INFO')) {
159 $self->error('Could not log into YouTube');
163 $r = $ua->get($videourl);
164 if ($r->base->as_string() =~ m,/verify_age,) {
165 $self->debug("Looking for session token...");
166 $c = $r->decoded_content();
167 $p = HTML::TokeParser->new(\$c);
168 while (my $tag = $p->get_tag('script')) {
169 if ($p->get_text() =~ /gXSRF_token = '(.+)'/) {
175 unless(defined($token)) {
176 $self->error("Could not find session token");
180 $self->debug('Authenticating session...');
181 $r = $ua->post($r->base->as_string, { 'next_url' => $r->base->path, 'action_confirm' => 'Confirm Birth Date', 'session_token' => $token });
184 unless(check_cookie($ua->cookie_jar, 'is_adult')) {
185 $self->error('Could not authenticate session');
189 return $ua->get($videourl);