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);
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]);
68 $metadata->{'URL'} = $url;
69 $metadata->{'ID'} = $2;
70 $metadata->{'TYPE'} = 'video';
71 $metadata->{'SOURCE'} = $self->{'NAME'};
72 $metadata->{'TITLE'} = undef;
73 $metadata->{'DLURL'} = undef;
75 $preflist = $preflist{$quality};
76 $self->debug("Quality: %s, preflist: [%s]", $quality, join(", ", @{$preflist}));
78 $videourl = sprintf('http://www.youtube.com/watch?v=%s', $2);
80 unless(defined($r = $ua->get($videourl))) {
81 $self->error('Could not download %s', $url);
85 if ($r->base->as_string() =~ m,/verify_age,) {
86 $self->debug('Video requires age verification');
87 $ua->cookie_jar($jar);
88 $r = $self->__login($videourl, $ua);
90 $self->error('Could not log into YouTube');
94 $content = $r->content();
96 $p = HTML::TokeParser->new(\$content);
98 while ($tag = $p->get_tag('div', 'meta', 'script')) {
99 if ('meta' eq $tag->[0]) {
100 if ('title' eq $tag->[1]->{'name'}) {
101 $metadata->{'TITLE'} = $tag->[1]->{'content'};
102 $self->debug('Title found: %s', $metadata->{'TITLE'});
104 } elsif ('script' eq $tag->[0]) {
106 $self->debug("Found script: %s", $e);
107 if ($e =~ m|\x22fmt_url_map\x22\s*:\s*\x22([^\x22]+)\x22|) {
108 $self->debug("Found fmt_url_map: %s", $1);
113 $urls =~ s/%(..)/chr(hex($1))/ge;
114 @urls = split(/,/, $urls);
116 my ($tag, $url) = split(/\|/);
118 $self->debug('Found flv,h264,large: %s', $url);
119 } elsif ($tag == 34) {
120 $self->debug('Found flv,h264: %s', $url);
121 } elsif ($tag == 22) {
122 $self->debug('Found mp4,h264,large: %s', $url);
123 } elsif ($tag == 18) {
124 $self->debug('Found mp4,h264: %s', $url);
125 } elsif ($tag == 5) {
126 $self->debug('Found flv,flv: %s', $url);
128 $self->error('Unknown tag %s: %s', $tag, $url);
134 foreach (@{$preflist}) {
135 if (exists($urls{$_})) {
136 $self->debug("Selected URL with quality level %s", $_);
137 $metadata->{'DLURL'} = $urls{$_};
142 $self->debug('URL found: %s', $metadata->{'DLURL'});
144 } elsif ('div' eq $tag->[0]) {
145 if (exists($tag->[1]->{'class'}) and ('errorBox' eq $tag->[1]->{'class'})) {
146 $self->error("Could not get video data for youtube %s: %s",
147 $metadata->{'ID'}, $p->get_trimmed_text());
153 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
154 $self->error('Could not determine download URL');
163 my $videourl = shift;
165 my $user = $self->_getval('USERNAME');
166 my $pass = $self->_getval('PASSWORD');
178 $jar->scan(sub { $found = 1 if ( $key eq $_[1]) });
183 if (($user eq '') or ($pass eq '')) {
184 $self->error('No username or password defined for YouTube');
188 $self->debug('Logging in');
189 $r = $ua->post('http://www.youtube.com/signup', { 'next' => '/', 'current_form' => 'login', 'action_login' => '1', 'username' => $user, 'password' => $pass });
191 unless(check_cookie($ua->cookie_jar, 'LOGIN_INFO')) {
192 $self->error('Could not log into YouTube');
196 $r = $ua->get($videourl);
197 if ($r->base->as_string() =~ m,/verify_age,) {
198 $self->debug("Looking for session token...");
199 $c = $r->decoded_content();
200 $p = HTML::TokeParser->new(\$c);
201 while (my $tag = $p->get_tag('script')) {
202 if ($p->get_text() =~ /gXSRF_token = '(.+)'/) {
208 unless(defined($token)) {
209 $self->error("Could not find session token");
213 $self->debug('Authenticating session...');
214 $r = $ua->post($r->base->as_string, { 'next_url' => $r->base->path, 'action_confirm' => 'Confirm Birth Date', 'session_token' => $token });
217 unless(check_cookie($ua->cookie_jar, 'is_adult')) {
218 $self->error('Could not authenticate session');
222 return $ua->get($videourl);