Change new() function in plugins to avoid having to call _prepare_parameters() in...
[videosite.git] / videosite / VimeoGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for vimeo.com
5
6 package videosite::VimeoGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use HTML::TokeParser;
12 use videosite::JSArrayParser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new(
20         NAME => 'vimeo',
21         _SELFTESTURL => 'http://vimeo.com/35055590',
22         _SELFTESTTITLE => 'Hello',
23         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(?:m/)?(\d+))'],
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 $dlurl;
38     my $hd;
39     my $dlpath;
40     my $timestamp;
41     my $hash;
42
43     $url =~ m|$pattern|;
44     $url = $1;
45
46     $metadata->{'URL'} = $url;
47     $metadata->{'ID'} = $2;
48     $metadata->{'TYPE'} = 'video';
49     $metadata->{'SOURCE'} = $self->{'NAME'};
50     $metadata->{'TITLE'} = undef;
51     $metadata->{'DLURL'} = undef;
52
53     # Get the XML file containing the video metadata
54     unless(defined($content = $self->simple_get(sprintf('http://vimeo.com/%s', $2)))) {
55         $self->error('Could not download site');
56         return undef;
57     }
58
59     $p = HTML::TokeParser->new(\$content);
60
61     while ($e = $p->get_tag('script')) {
62         if ($e->[0] eq 'script') {
63             my $t = $p->get_text();
64
65             if ($t =~ m|clip\d+_\d+ = (.*\});Player|s) {
66                 my $jsp = videosite::JSArrayParser->new();
67                 my $r;
68
69                 $self->debug("Found raw config: %s", $1);
70                 $r = $jsp->parse($1);
71
72                 unless(defined($r)) {
73                     $self->error("Found information hash, but could not parse");
74                     return undef;
75                 }
76
77                 $self->debug("Found parsed config: %s", Dumper($r));
78
79                 unless(exists($r->{'config'}->{'request'})) {
80                     $self->error("Required information not found in hash");
81                     return undef;
82                 }
83
84                 $metadata->{'TITLE'} = $r->{'config'}->{'video'}->{'title'};
85                 $hd = grep { $_ eq 'hd' } @{$r->{'config'}->{'video'}->{'files'}->{'h264'}};
86                 $self->debug("HD: %d", $hd);
87                 $r = $r->{'config'}->{'request'};
88
89                 $metadata->{'DLURL'} = sprintf("http://%s/play_redirect?clip_id=%d&sig=%s&time=%d&quality=%s&codecs=H264,VP8,VP6",
90                         $r->{'player_url'},
91                         $metadata->{'ID'},
92                         $r->{'signature'},
93                         $r->{'timestamp'},
94                         $hd?'hd':'sd',
95                         );
96             }
97         }
98     }
99
100     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
101         $self->error('Could not extract download URL and title');
102         return undef;
103     }
104
105     return $metadata;
106 }
107
108 1;