fix quoting in AsyncWgetFileGetter again
[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
27     return bless($self, $class);
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;
37     my $e;
38     my $quality = $self->_getval('QUALITY');
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'ID'} = $2;
45     $metadata->{'TYPE'} = 'video';
46     $metadata->{'SOURCE'} = $self->{'NAME'};
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     unless(defined($content = $self->simple_get(sprintf('http://video.google.com/videohosted?docid=%s', $2)))) {
51         $self->error('Could not download %s', $url);
52         return undef;
53     }
54
55     $p = HTML::TokeParser->new(\$content);
56
57     # Look for the title
58     if ($p->get_tag('title')) {
59         $metadata->{'TITLE'} = $p->get_text();
60         $metadata->{'TITLE'} =~ s/\s?- Google Video$//s;
61     }
62
63     if ($quality eq 'h264') {
64         while ($e = $p->get_tag('a')) {
65             if ((exists($e->[1]{'id'})) and ('ipoddownloadlink' eq $e->[1]{'id'})) {
66                 $metadata->{'DLURL'} = $e->[1]{'href'};
67                 last;
68             }
69         }
70     } else {
71         while ($e = $p->get_tag('script')) {
72             if ($p->get_text() =~ m|googleplayer\.swf\?\\46videoUrl\\75(.+?)\\46|s) {
73                 my $u = $1;
74                 $u =~ s/%(..)/chr(hex($1))/ge;
75                 $metadata->{'DLURL'} = $u;
76                 last;
77             }
78         }
79     }
80
81     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
82         $self->error('Could not determine download URL');
83         return undef;
84     }
85
86     return $metadata;
87 }
88
89 1;