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