- Add grabber for vimeo.com (which is, finally, a bit more challenging than other...
[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 VimeoGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use XML::Simple;
13 use Digest::MD5 qw(md5_hex);
14 use Data::Dumper;
15
16 use strict;
17
18 sub new {
19     my $class = shift;
20     my $self = $class->SUPER::new();
21
22     $self->{'NAME'} = 'vimeo';
23     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(\d+))'];
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 = XML::Simple->new();
38     my $t;
39     my $dlurl;
40     my $dlpath;
41     my $timestamp;
42     my $hash;
43
44     $url =~ m|$pattern|;
45     $url = $1;
46
47     $metadata->{'URL'} = $url;
48     $metadata->{'ID'} = $2;
49     $metadata->{'TYPE'} = 'video';
50     $metadata->{'SOURCE'} = $self->{'NAME'};
51     $metadata->{'TITLE'} = undef;
52     $metadata->{'DLURL'} = undef;
53
54     # Get the XML file containing the video metadata
55     unless(defined($content = LWP::Simple::get(sprintf('http://www.vimeo.com/moogaloop/load/clip:%s/local?context=default&context_id=undefined', $2)))) {
56         $self->error('Could not download XML metadata');
57         return undef;
58     }
59
60     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
61         $self->error('Could not parse XML metadata');
62         return undef;
63     }
64
65     $dlurl = $t->{'xml'}->{'video'}->{'hd_file'} || $t->{'xml'}->{'video'}->{'file'};
66     $timestamp = $t->{'xml'}->{'timestamp'};
67
68     unless(defined($dlurl)) {
69         return undef;
70     }
71
72     # Vimeo appends a hash to the download URL, in order to thwart people like me.
73     # Unfortunately the algorithm isn't that complicated :)
74     if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
75         $dlpath = $1;
76         $timestamp += 1800;
77         $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
78     } else {
79         return undef;
80     }
81
82     $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
83     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
84
85     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
86         $self->error('Could not extract download URL and title');
87         return undef;
88     }
89
90     return $metadata;
91 }
92
93 1;