Change new() function in plugins to avoid having to call _prepare_parameters() in...
[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 videosite::GoogleGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use HTML::TokeParser;
12 use Data::Dumper;
13
14 use strict;
15
16 sub new {
17     my $class = shift;
18     my $self = $class->SUPER::new(
19         NAME => 'google',
20         PATTERNS => ['(http://video\.google\.com/videoplay\?docid=([-\d]+))'],
21         _PARAMS => {
22             QUALITY => ['normal', 'Quality of the video to download. normal = standard resolution flash video, h264 = high resolution MPEG4 video']
23         },
24     );
25
26     return bless($self, $class);
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $url = shift;
32     my $pattern = shift;
33     my $content;
34     my $metadata = {};
35     my $p;
36     my $e;
37     my $quality = $self->_getval('QUALITY');
38
39     $url =~ m|$pattern|;
40     $url = $1;
41
42     $metadata->{'URL'} = $url;
43     $metadata->{'ID'} = $2;
44     $metadata->{'TYPE'} = 'video';
45     $metadata->{'SOURCE'} = $self->{'NAME'};
46     $metadata->{'TITLE'} = undef;
47     $metadata->{'DLURL'} = undef;
48
49     unless(defined($content = $self->simple_get(sprintf('http://video.google.com/videohosted?docid=%s', $2)))) {
50         $self->error('Could not download %s', $url);
51         return undef;
52     }
53
54     $p = HTML::TokeParser->new(\$content);
55
56     # Look for the title
57     if ($p->get_tag('title')) {
58         $metadata->{'TITLE'} = $p->get_text();
59         $metadata->{'TITLE'} =~ s/\s?- Google Video$//s;
60     }
61
62     if ($quality eq 'h264') {
63         while ($e = $p->get_tag('a')) {
64             if ((exists($e->[1]{'id'})) and ('ipoddownloadlink' eq $e->[1]{'id'})) {
65                 $metadata->{'DLURL'} = $e->[1]{'href'};
66                 last;
67             }
68         }
69     } else {
70         while ($e = $p->get_tag('script')) {
71             if ($p->get_text() =~ m|googleplayer\.swf\?\\46videoUrl\\75(.+?)\\46|s) {
72                 my $u = $1;
73                 $u =~ s/%(..)/chr(hex($1))/ge;
74                 $metadata->{'DLURL'} = $u;
75                 last;
76             }
77         }
78     }
79
80     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
81         $self->error('Could not determine download URL');
82         return undef;
83     }
84
85     return $metadata;
86 }
87
88 1;