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);
18 use videosite::JSArrayParser;
24 my $self = $class->SUPER::new();
26 $self->{'NAME'} = 'youtube';
27 $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/watch\?(?:.+=.+&)*v=([-a-zA-Z0-9_]+))',
28 '(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))'];
29 $self->{'_PARAMS'} = {
30 'QUALITY' => ['normal', 'Quality of the video to download.', {
31 'normal' => 'standard resolution flash video',
32 'high' => 'higher resolution flash video',
33 'h264' => 'high resolution MPEG4 video',
34 'hd' => 'HD720 resolution'}],
35 'USERNAME' => ['', 'Username to use for YouTube login'],
36 'PASSWORD' => ['', 'Password to use for YouTube login']};
39 $self->_prepare_parameters();
53 my $jar = HTTP::Cookies->new;
54 my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
57 my $quality = $self->_getval('QUALITY');
59 'hd' => [22, 35, 18, 34, 6, 5],
60 'h264' => [18, 34, 22, 35, 6, 5],
61 'high' => [34, 35, 18, 22, 6, 5],
62 'normal' => [6, 5, 34, 35, 18, 22]);
69 $metadata->{'URL'} = $url;
70 $metadata->{'ID'} = $2;
71 $metadata->{'TYPE'} = 'video';
72 $metadata->{'SOURCE'} = $self->{'NAME'};
73 $metadata->{'TITLE'} = undef;
74 $metadata->{'DLURL'} = undef;
76 $preflist = $preflist{$quality};
77 $self->debug("Quality: %s, preflist: [%s]", $quality, join(", ", @{$preflist}));
79 $videourl = sprintf('http://www.youtube.com/watch?v=%s', $2);
81 unless(defined($r = $ua->get($videourl))) {
82 $self->error('Could not download %s', $url);
86 if ($r->base->as_string() =~ m,/verify_age,) {
87 $self->debug('Video requires age verification');
88 $ua->cookie_jar($jar);
89 $r = $self->__login($videourl, $ua);
91 $self->error('Could not log into YouTube');
95 $content = $r->content();
97 $p = HTML::TokeParser->new(\$content);
99 SWF_ARGS: while ($tag = $p->get_tag('div', 'meta', 'script')) {
100 if ('meta' eq $tag->[0]) {
101 if ('title' eq $tag->[1]->{'name'}) {
102 $metadata->{'TITLE'} = $tag->[1]->{'content'};
103 $self->debug('Title found: %s', $metadata->{'TITLE'});
105 } elsif ('script' eq $tag->[0]) {
107 $self->debug("Found script: %s", $e);
108 if ($e =~ m|\x27SWF_ARGS\x27:\s+(.+),|) {
112 $self->debug("Found SWF_ARGS: %s", $args);
113 $jsp = videosite::JSArrayParser->new();
114 $r = $jsp->parse($args);
116 unless(defined($r)) {
117 $self->error("Found information hash, but could not parse");
121 if (exists($r->{'fmt_url_map'}) and ($r->{'fmt_url_map'} ne '')) {
122 my $urls = $r->{'fmt_url_map'};
124 $self->debug("Video has fmt_url_map: %s", $urls);
126 $urls =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
127 %urls = split(/[\|,]/, $urls);
128 } elsif (exists($r->{'t'}) and ($r->{'t'} ne '')) {
129 my $thash = $r->{'t'};
131 if (exists($r->{'fmt_map'}) && ($r->{'fmt_map'} ne '')) {
132 my $fmt = $r->{'fmt_map'};
135 $self->debug('Video has fmt_map');
136 $fmt =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
137 @fmt = split(/,/, $fmt);
140 $urls{$_[0]} = sprintf('http://www.youtube.com/get_video?video_id=%s&fmt=%d&t=%s',
146 $urls{5} = sprintf('http://www.youtube.com/get_video?video_id=%s&t=%s',
151 $self->error('Neither fmt_url_map nor t found in video information hash');
154 $self->debug("Found quality levels [%s]", join(", ", keys(%urls)));
156 foreach (keys(%urls)) {
158 $self->debug('Found flv,h264,large: %s', $urls{$_});
160 $self->debug('Found flv,h264: %s', $urls{$_});
162 $self->debug('Found mp4,h264,large: %s', $urls{$_});
164 $self->debug('Found mp4,h264: %s', $urls{$_});
166 $self->debug('Found flv,flv: %s', $urls{$_});
168 $self->error('Unknown tag %s: %s', $_, $urls{$_});
172 foreach (@{$preflist}) {
173 if (exists($urls{$_})) {
174 $self->debug("Selected URL with quality level %s", $_);
175 $metadata->{'DLURL'} = $urls{$_};
180 $self->debug('URL found: %s', $metadata->{'DLURL'});
183 } elsif ('div' eq $tag->[0]) {
184 if (exists($tag->[1]->{'class'}) and ('errorBox' eq $tag->[1]->{'class'})) {
185 $self->error("Could not get video data for youtube %s: %s",
186 $metadata->{'ID'}, $p->get_trimmed_text());
192 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
193 $self->error('Could not determine download URL');
202 my $videourl = shift;
204 my $user = $self->_getval('USERNAME');
205 my $pass = $self->_getval('PASSWORD');
217 $jar->scan(sub { $found = 1 if ( $key eq $_[1]) });
222 if (($user eq '') or ($pass eq '')) {
223 $self->error('No username or password defined for YouTube');
227 $self->debug('Logging in');
228 $r = $ua->post('http://www.youtube.com/signup', { 'next' => '/', 'current_form' => 'login', 'action_login' => '1', 'username' => $user, 'password' => $pass });
230 unless(check_cookie($ua->cookie_jar, 'LOGIN_INFO')) {
231 $self->error('Could not log into YouTube');
235 $r = $ua->get($videourl);
236 if ($r->base->as_string() =~ m,/verify_age,) {
237 $self->debug("Looking for session token...");
238 $c = $r->decoded_content();
239 $p = HTML::TokeParser->new(\$c);
240 while (my $tag = $p->get_tag('script')) {
241 if ($p->get_text() =~ /gXSRF_token = '(.+)'/) {
247 unless(defined($token)) {
248 $self->error("Could not find session token");
252 $self->debug('Authenticating session...');
253 $r = $ua->post($r->base->as_string, { 'next_url' => $r->base->path, 'action_confirm' => 'Confirm Birth Date', 'session_token' => $token });
256 unless(check_cookie($ua->cookie_jar, 'is_adult')) {
257 $self->error('Could not authenticate session');
261 return $ua->get($videourl);