Youtube: Add video format 43 (WebM)
[videosite.git] / videosite / VimeoGrabber.pm
index 3e8ce6a..ba76cff 100644 (file)
@@ -3,12 +3,12 @@
 #
 # Grabber for vimeo.com
 
-package VimeoGrabber;
+package videosite::VimeoGrabber;
 
-use GrabberBase;
-@ISA = qw(GrabberBase);
+use videosite::GrabberBase;
+@ISA = qw(videosite::GrabberBase);
 
-use LWP::Simple qw(!get);
+use LWP::UserAgent;
 use XML::Simple;
 use Digest::MD5 qw(md5_hex);
 use Data::Dumper;
@@ -37,9 +37,11 @@ sub _parse {
     my $p = XML::Simple->new();
     my $t;
     my $dlurl;
+    my $hd;
     my $dlpath;
     my $timestamp;
     my $hash;
+    my $ua = LWP::UserAgent->new(agent => 'Mozilla');
 
     $url =~ m|$pattern|;
     $url = $1;
@@ -52,34 +54,48 @@ sub _parse {
     $metadata->{'DLURL'} = undef;
 
     # Get the XML file containing the video metadata
-    unless(defined($content = LWP::Simple::get(sprintf('http://www.vimeo.com/moogaloop/load/clip:%s/local?context=default&context_id=undefined', $2)))) {
+    $content = $ua->get(sprintf('http://www.vimeo.com/moogaloop/load/clip:%s', $2));
+    unless ($content->is_success()) {
         $self->error('Could not download XML metadata');
         return undef;
     }
 
+    $content = $content->decoded_content();
+
     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
         $self->error('Could not parse XML metadata');
         return undef;
     }
 
-    $dlurl = $t->{'xml'}->{'video'}->{'hd_file'} || $t->{'xml'}->{'video'}->{'file'};
-    $timestamp = $t->{'xml'}->{'timestamp'};
-
-    unless(defined($dlurl)) {
-        return undef;
+    if (exists($t->{'xml'}->{'video'}->{'isHD'}) and (0 != $t->{'xml'}->{'video'}->{'isHD'})) {
+        $self->debug('Selecting HD video');
+        $hd = '/?q=hd';
+    } else {
+        $self->debug('Selecting SD video');
+        $hd = '';
     }
+    $timestamp = $t->{'xml'}->{'request_signature_expires'};
+    $hash = $t->{'xml'}->{'request_signature'};
+    $dlurl = sprintf('http://vimeo.com/moogaloop/play/clip:%s/%s/%d%s', $metadata->{'ID'}, $hash, $timestamp, $hd);
 
-    # Vimeo appends a hash to the download URL, in order to thwart people like me.
-    # Unfortunately the algorithm isn't that complicated :)
-    if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
-        $dlpath = $1;
-        $timestamp += 1800;
-        $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
-    } else {
+    unless(defined($dlurl)) {
+        $self->error('No dlurl found in XML');
         return undef;
     }
 
-    $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
+    # # Vimeo appends a hash to the download URL, in order to thwart people like me.
+    # # Unfortunately the algorithm isn't that complicated :)
+    # if ($dlurl =~ m|http://bitcast.vimeo.com(.+)|) {
+    #     $dlpath = $1;
+    #     $timestamp += 1800;
+    #     $hash = md5_hex(sprintf('redFiretruck%s?e=%d', $dlpath, $timestamp));
+    # } else {
+    #     $self->error('Unknown dlurl scheme: %s', $dlurl);
+    #     return undef;
+    # }
+
+    # $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
+    $metadata->{'DLURL'} = $dlurl;
     $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
 
     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {