2 # download strategy revised using
3 # http://www.kde-apps.org/content/show.php?content=41456
5 package YouTubeGrabber;
8 @ISA = qw(GrabberBase);
10 use LWP::Simple qw(!get);
18 my $self = $class->SUPER::new();
20 $self->{'NAME'} = 'youtube';
21 $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/watch\?(?:.+=.+&)*v=([-a-zA-Z0-9_]+))',
22 '(http://(?:[-a-zA-Z0-9_.]+\.)*youtube.(?:com|de|co.uk)/v/([-a-zA-Z0-9_]+))'];
25 $self->_prepare_parameters();
36 my $p = HTML::Parser->new(api_version => 3);
44 $metadata->{'URL'} = $url;
45 $metadata->{'ID'} = $2;
46 $metadata->{'TYPE'} = 'video';
47 $metadata->{'SOURCE'} = 'youtube';
48 $metadata->{'TITLE'} = undef;
49 $metadata->{'DLURL'} = undef;
51 unless(defined($content = LWP::Simple::get(sprintf('http://youtube.com/watch?v=%s', $2)))) {
52 $self->error('Could not download %s', $url);
56 $p->handler(start => \@accum, "tagname, attr");
57 $p->handler(text => \@text, "text");
58 $p->report_tags(qw(meta script));
62 # Look for the title in the meta tags
64 if ('meta' eq $e->[0]) {
65 if ('title' eq $e->[1]->{'name'}) {
66 $metadata->{'TITLE'} = $e->[1]->{'content'};
71 # Look for the download URL
73 if ($e->[0] =~ m|/watch_fullscreen\?(.*)\&fs|) {
74 $metadata->{'DLURL'} = 'http://www.youtube.com/get_video.php?' . $1;
78 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
79 $self->error('Could not determine download URL');