- fix vimeo grabber
[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     if (exists($t->{'xml'}->{'video'}->{'is_hd'}) and (0 != $t->{'xml'}->{'video'}->{'is_hd'})) {
66         $self->debug('Selecting HD video');
67         $dlurl = $t->{'xml'}->{'video'}->{'hd_file'};
68     } else {
69         $self->debug('Selecting SD video');
70         $dlurl = $t->{'xml'}->{'video'}->{'file'};
71     }
72     $timestamp = $t->{'xml'}->{'timestamp'};
73
74     unless(defined($dlurl)) {
75         $self->error('No dlurl found in XML');
76         return undef;
77     }
78
79     # Vimeo appends a hash to the download URL, in order to thwart people like me.
80     # Unfortunately the algorithm isn't that complicated :)
81     if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
82         $dlpath = $1;
83         $timestamp += 1800;
84         $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
85     } else {
86         $self->error('Unknown dlurl scheme: %s', $dlurl);
87         return undef;
88     }
89
90     $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
91     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
92
93     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
94         $self->error('Could not extract download URL and title');
95         return undef;
96     }
97
98     return $metadata;
99 }
100
101 1;