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);
14 use LWP::Simple qw(!get);
22 my $self = $class->SUPER::new();
24 $self->{'NAME'} = 'youtube';
25 $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/watch\?(?:.+=.+&)*v=([-a-zA-Z0-9_]+))',
26 '(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))'];
29 $self->_prepare_parameters();
47 $metadata->{'URL'} = $url;
48 $metadata->{'ID'} = $2;
49 $metadata->{'TYPE'} = 'video';
50 $metadata->{'SOURCE'} = $self->{'NAME'};
51 $metadata->{'TITLE'} = undef;
52 $metadata->{'DLURL'} = undef;
54 unless(defined($content = LWP::Simple::get(sprintf('http://youtube.com/watch?v=%s', $2)))) {
55 $self->error('Could not download %s', $url);
59 $p = HTML::TokeParser->new(\$content);
61 while ($tag = $p->get_tag('div', 'meta', 'script')) {
62 if ('meta' eq $tag->[0]) {
63 if ('title' eq $tag->[1]->{'name'}) {
64 $metadata->{'TITLE'} = $tag->[1]->{'content'};
65 $self->debug('Title found: %s', $metadata->{'TITLE'});
67 } elsif ('script' eq $tag->[0]) {
69 if ($e =~ m|/watch_fullscreen\?(.+)\x27|) {
70 my %args = map { split(/=/, $_, 2); } split(/&/, $1);
71 $metadata->{'DLURL'} = sprintf('http://www.youtube.com/get_video.php?video_id=%s&t=%s',
72 $metadata->{'ID'}, $args{'t'});
73 $self->debug('URL found: %s', $metadata->{'DLURL'});
75 } elsif ('div' eq $tag->[0]) {
76 if ('errorBox' eq $tag->[1]->{'class'}) {
77 $self->error("Could not get video data for youtube %s: %s",
78 $metadata->{'ID'}, $p->get_trimmed_text());
84 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
85 $self->error('Could not determine download URL');