Youtube: remove COOKIE 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 videosite::VimeoGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use XML::Simple;
12 use Digest::MD5 qw(md5_hex);
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new();
20
21     $self->{'NAME'} = 'vimeo';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(\d+))'];
23
24     bless($self, $class);
25     $self->_prepare_parameters();
26
27     return $self;
28 }
29
30 sub _parse {
31     my $self = shift;
32     my $url = shift;
33     my $pattern = shift;
34     my $content;
35     my $metadata = {};
36     my $p = XML::Simple->new();
37     my $t;
38     my $dlurl;
39     my $hd;
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 = $self->simple_get(sprintf('http://www.vimeo.com/moogaloop/load/clip:%s', $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'}->{'isHD'}) and (0 != $t->{'xml'}->{'video'}->{'isHD'})) {
66         $self->debug('Selecting HD video');
67         $hd = '/?q=hd';
68     } else {
69         $self->debug('Selecting SD video');
70         $hd = '';
71     }
72     $timestamp = $t->{'xml'}->{'request_signature_expires'};
73     $hash = $t->{'xml'}->{'request_signature'};
74     $dlurl = sprintf('http://vimeo.com/moogaloop/play/clip:%s/%s/%d%s', $metadata->{'ID'}, $hash, $timestamp, $hd);
75
76     unless(defined($dlurl)) {
77         $self->error('No dlurl found in XML');
78         return undef;
79     }
80
81     # # Vimeo appends a hash to the download URL, in order to thwart people like me.
82     # # Unfortunately the algorithm isn't that complicated :)
83     # if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
84     #     $dlpath = $1;
85     #     $timestamp += 1800;
86     #     $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
87     # } else {
88     #     $self->error('Unknown dlurl scheme: %s', $dlurl);
89     #     return undef;
90     # }
91
92     # $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
93     $metadata->{'DLURL'} = $dlurl;
94     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
95
96     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
97         $self->error('Could not extract download URL and title');
98         return undef;
99     }
100
101     return $metadata;
102 }
103
104 1;