Youtube: Simplify match pattern
[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->{_SELFTESTURL} = 'http://vimeo.com/35055590';
23     $self->{_SELFTESTTITLE} = 'Hello';
24     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(\d+))'];
25
26     bless($self, $class);
27     $self->_prepare_parameters();
28
29     return $self;
30 }
31
32 sub _parse {
33     my $self = shift;
34     my $url = shift;
35     my $pattern = shift;
36     my $content;
37     my $metadata = {};
38     my $p = XML::Simple->new();
39     my $t;
40     my $dlurl;
41     my $hd;
42     my $dlpath;
43     my $timestamp;
44     my $hash;
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     unless(defined($content = $self->simple_get(sprintf('http://www.vimeo.com/moogaloop/load/clip:%s', $2)))) {
58         $self->error('Could not download XML metadata');
59         return undef;
60     }
61
62     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
63         $self->error('Could not parse XML metadata');
64         return undef;
65     }
66
67     if (exists($t->{'xml'}->{'video'}->{'isHD'}) and (0 != $t->{'xml'}->{'video'}->{'isHD'})) {
68         $self->debug('Selecting HD video');
69         $hd = '/?q=hd';
70     } else {
71         $self->debug('Selecting SD video');
72         $hd = '';
73     }
74     $timestamp = $t->{'xml'}->{'request_signature_expires'};
75     $hash = $t->{'xml'}->{'request_signature'};
76     $dlurl = sprintf('http://vimeo.com/moogaloop/play/clip:%s/%s/%d%s', $metadata->{'ID'}, $hash, $timestamp, $hd);
77
78     unless(defined($dlurl)) {
79         $self->error('No dlurl found in XML');
80         return undef;
81     }
82
83     # # Vimeo appends a hash to the download URL, in order to thwart people like me.
84     # # Unfortunately the algorithm isn't that complicated :)
85     # if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
86     #     $dlpath = $1;
87     #     $timestamp += 1800;
88     #     $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
89     # } else {
90     #     $self->error('Unknown dlurl scheme: %s', $dlurl);
91     #     return undef;
92     # }
93
94     # $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
95     $metadata->{'DLURL'} = $dlurl;
96     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
97
98     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
99         $self->error('Could not extract download URL and title');
100         return undef;
101     }
102
103     return $metadata;
104 }
105
106 1;