- Add copyright information to all files
[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'} = 'youtube';
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     $p->handler(start => \@accum, "tagname, attr");
61     $p->handler(text => \@text, "text");
62     $p->report_tags(qw(meta script));
63     $p->utf8_mode(1);
64     $p->parse($content);
65
66     # Look for the title in the meta tags
67     foreach $e (@accum) {
68         if ('meta' eq $e->[0]) {
69             if ('title' eq $e->[1]->{'name'}) {
70                 $metadata->{'TITLE'} = $e->[1]->{'content'};
71             }
72         }
73     }
74
75     # Look for the download URL
76     foreach $e (@text) {
77         if ($e->[0] =~ m|/watch_fullscreen\?(.*)\&fs|) {
78             $metadata->{'DLURL'} = 'http://www.youtube.com/get_video.php?' . $1;
79         }
80     }
81
82     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
83         $self->error('Could not determine download URL');
84         return undef;
85     }
86
87     return $metadata;
88 }
89
90 1;