error on removed video
[videosite.git] / videosite / YouTubeGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for youtube.com/de/...
5 #
6 # download strategy revised using
7 # http://www.kde-apps.org/content/show.php?content=41456
8
9 package YouTubeGrabber;
10
11 use GrabberBase;
12 @ISA = qw(GrabberBase);
13
14 use LWP::Simple qw(!get);
15 use HTML::Parser;
16 use Data::Dumper;
17
18 use strict;
19
20 sub new {
21     my $class = shift;
22     my $self = $class->SUPER::new();
23
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_]+))'];
27
28     bless($self, $class);
29     $self->_prepare_parameters();
30
31     return $self;
32 }
33
34 sub _parse {
35     my $self = shift;
36     my $url = shift;
37     my $pattern = shift;
38     my $content;
39     my $metadata = {};
40     my $p = HTML::Parser->new(api_version => 3);
41     my @accum;
42     my @text;
43     my $e;
44
45     $url =~ m|$pattern|;
46     $url = $1;
47
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;
54
55     unless(defined($content = LWP::Simple::get(sprintf('http://youtube.com/watch?v=%s', $2)))) {
56         $self->error('Could not download %s', $url);
57         return undef;
58     }
59
60     if ($content =~ /This video has been removed due to terms of use violation/) {
61         $self->error('Video has been removed');
62         return undef;
63     }
64
65     $p->handler(start => \@accum, "tagname, attr");
66     $p->handler(text => \@text, "text");
67     $p->report_tags(qw(meta script));
68     $p->utf8_mode(1);
69     $p->parse($content);
70
71     # Look for the title in the meta tags
72     foreach $e (@accum) {
73         if ('meta' eq $e->[0]) {
74             if ('title' eq $e->[1]->{'name'}) {
75                 $metadata->{'TITLE'} = $e->[1]->{'content'};
76             }
77         }
78     }
79
80     # Look for the download URL
81     foreach $e (@text) {
82         if ($e->[0] =~ m|/watch_fullscreen\?(.*)\&fs|) {
83             $metadata->{'DLURL'} = 'http://www.youtube.com/get_video.php?' . $1;
84         }
85     }
86
87     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
88         $self->error('Could not determine download URL');
89         return undef;
90     }
91
92     return $metadata;
93 }
94
95 1;