- Add GoogleGrabber
[videosite.git] / videosite / GoogleGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for video.google.com
5
6 package GoogleGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use HTML::Parser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new();
20
21     $self->{'NAME'} = 'google';
22     $self->{'PATTERNS'} = ['(http://video\.google\.com/videoplay\?docid=([-\d]+))'];
23
24     bless($self, $class);
25     $self->_prepare_parameters();
26
27     return $self;
28 }
29
30 sub _parse {
31     my $self = shift;
32     my $url = shift;
33     my $pattern = shift;
34     my $content;
35     my $metadata = {};
36     my $p = HTML::Parser->new(api_version => 3);
37     my @accum;
38     my @text;
39     my $e;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'video';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'TITLE'} = undef;
49     $metadata->{'DLURL'} = undef;
50
51     unless(defined($content = LWP::Simple::get(sprintf('http://video.google.com/videoplay?docid=%s', $2)))) {
52         $self->error('Could not download %s', $url);
53         return undef;
54     }
55
56     $p->handler(start => \@accum, "tagname, attr");
57     $p->handler(text => \@text, "text");
58     $p->report_tags(qw(embed div));
59     $p->utf8_mode(1);
60     $p->parse($content);
61
62     # Look for the title in the div tags
63     foreach $e (@accum) {
64         if ('div' eq $e->[0]) {
65             if ((exists($e->[1]->{'class'})) and ('title' eq $e->[1]->{'class'})) {
66                 $metadata->{'TITLE'} = $e->[1]->{'title'};
67             }
68         }
69
70         if ('embed' eq $e->[0]) {
71             if ((exists($e->[1]->{'src'})) and ($e->[1]->{'src'} =~ m|^/googleplayer.swf\?\&videoUrl=([^&]+)\&|)) {
72                 $metadata->{'DLURL'} = $1;
73                 $metadata->{'DLURL'} =~ s/%(..)/chr(hex($1))/ge;
74             }
75         }
76     }
77
78     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
79         $self->error('Could not determine download URL');
80         return undef;
81     }
82
83     return $metadata;
84 }
85
86 1;