- Add video quality parameter for GoogleVideo
[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::TokeParser;
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     $self->{'_PARAMS'} = {'QUALITY' => ['normal', 'Quality of the video to download. normal = standard resolution flash video, h264 = high resolution MPEG4 video']};
24
25     bless($self, $class);
26     $self->_prepare_parameters();
27
28     return $self;
29 }
30
31 sub _parse {
32     my $self = shift;
33     my $url = shift;
34     my $pattern = shift;
35     my $content;
36     my $metadata = {};
37     my $p;
38     my $e;
39     my $qualty = $self->_getval('QUALITY');
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/videohosted?docid=%s', $2)))) {
52         $self->error('Could not download %s', $url);
53         return undef;
54     }
55
56     $p = HTML::TokeParser->new(\$content);
57
58     # Look for the title
59     if ($p->get_tag('title')) {
60         $metadata->{'TITLE'} = $p->get_text();
61         $metadata->{'TITLE'} =~ s/\s?- Google Video$//s;
62     }
63
64     if ($quality eq 'h264') {
65         while ($e = $p->get_tag('a')) {
66             if ((exists($e->[1]{'id'})) and ('ipoddownloadlink' eq $e->[1]{'id'})) {
67                 $metadata->{'DLURL'} = $e->[1]{'href'};
68                 last;
69             }
70         }
71     } else {
72         while ($e = $p->get_tag('script')) {
73             if ($p->get_text() =~ m|googleplayer\.swf\?\\46videoUrl\\75(.+?)\\46|s) {
74                 my $u = $1;
75                 $u =~ s/%(..)/chr(hex($1))/ge;
76                 $metadata->{'DLURL'} = $u;
77                 last;
78             }
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;