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();
40 my $p = HTML::Parser->new(api_version => 3);
48 $metadata->{'URL'} = $url;
49 $metadata->{'ID'} = $2;
50 $metadata->{'TYPE'} = 'video';
51 $metadata->{'SOURCE'} = $self->{'NAME'};
52 $metadata->{'TITLE'} = undef;
53 $metadata->{'DLURL'} = undef;
55 unless(defined($content = LWP::Simple::get(sprintf('http://youtube.com/watch?v=%s', $2)))) {
56 $self->error('Could not download %s', $url);
60 if ($content =~ /This video has been removed due to terms of use violation/) {
61 $self->error('Video has been removed');
65 $p->handler(start => \@accum, "tagname, attr");
66 $p->handler(text => \@text, "text");
67 $p->report_tags(qw(meta script));
71 # Look for the title in the meta tags
73 if ('meta' eq $e->[0]) {
74 if ('title' eq $e->[1]->{'name'}) {
75 $metadata->{'TITLE'} = $e->[1]->{'content'};
80 # Look for the download URL
82 if ($e->[0] =~ m|/watch_fullscreen\?(.*)\&fs|) {
83 $metadata->{'DLURL'} = 'http://www.youtube.com/get_video.php?' . $1;
87 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
88 $self->error('Could not determine download URL');