- Vimeo fixed their hash algorithm. Wohooo!
[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 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 $hd;
41     my $dlpath;
42     my $timestamp;
43     my $hash;
44
45     $url =~ m|$pattern|;
46     $url = $1;
47
48     $metadata->{'URL'} = $url;
49     $metadata->{'ID'} = $2;
50     $metadata->{'TYPE'} = 'video';
51     $metadata->{'SOURCE'} = $self->{'NAME'};
52     $metadata->{'TITLE'} = undef;
53     $metadata->{'DLURL'} = undef;
54
55     # Get the XML file containing the video metadata
56     unless(defined($content = LWP::Simple::get(sprintf('http://www.vimeo.com/moogaloop/load/clip:%s/local?context=default&context_id=undefined', $2)))) {
57         $self->error('Could not download XML metadata');
58         return undef;
59     }
60
61     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
62         $self->error('Could not parse XML metadata');
63         return undef;
64     }
65
66     if (exists($t->{'xml'}->{'video'}->{'isHD'}) and (0 != $t->{'xml'}->{'video'}->{'isHD'})) {
67         $self->debug('Selecting HD video');
68         $hd = '/?q=hd';
69     } else {
70         $self->debug('Selecting SD video');
71         $hd = '';
72     }
73     $timestamp = $t->{'xml'}->{'request_signature_expires'};
74     $hash = $t->{'xml'}->{'request_signature'};
75     $dlurl = sprintf('http://vimeo.com/moogaloop/play/clip:%s/%s/%d%s', $metadata->{'ID'}, $hash, $timestamp, $hd);
76
77     unless(defined($dlurl)) {
78         $self->error('No dlurl found in XML');
79         return undef;
80     }
81
82     # # Vimeo appends a hash to the download URL, in order to thwart people like me.
83     # # Unfortunately the algorithm isn't that complicated :)
84     # if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
85     #     $dlpath = $1;
86     #     $timestamp += 1800;
87     #     $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
88     # } else {
89     #     $self->error('Unknown dlurl scheme: %s', $dlurl);
90     #     return undef;
91     # }
92
93     # $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
94     $metadata->{'DLURL'} = $dlurl;
95     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
96
97     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
98         $self->error('Could not extract download URL and title');
99         return undef;
100     }
101
102     return $metadata;
103 }
104
105 1;