- Manually add an XML header to the metadata
[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     # There is no XML header in the data, which makes XML::Simple unhappy
61     $content = '<?xml version="1.0" encoding="UTF-8"?>' . $content;
62
63     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
64         $self->error('Could not parse XML metadata');
65         return undef;
66     }
67
68     $dlurl = $t->{'xml'}->{'video'}->{'hd_file'} || $t->{'xml'}->{'video'}->{'file'};
69     $timestamp = $t->{'xml'}->{'timestamp'};
70
71     unless(defined($dlurl)) {
72         return undef;
73     }
74
75     # Vimeo appends a hash to the download URL, in order to thwart people like me.
76     # Unfortunately the algorithm isn't that complicated :)
77     if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
78         $dlpath = $1;
79         $timestamp += 1800;
80         $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
81     } else {
82         return undef;
83     }
84
85     $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
86     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
87
88     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
89         $self->error('Could not extract download URL and title');
90         return undef;
91     }
92
93     return $metadata;
94 }
95
96 1;