- Add grabber for vimeo.com (which is, finally, a bit more challenging than other...
authorRalf Ertzinger <sun@ryoko.camperquake.de>
Fri, 29 Feb 2008 10:11:42 +0000 (11:11 +0100)
committerRalf Ertzinger <sun@ryoko.camperquake.de>
Fri, 29 Feb 2008 10:11:42 +0000 (11:11 +0100)
videosite/VimeoGrabber.pm [new file with mode: 0644]

diff --git a/videosite/VimeoGrabber.pm b/videosite/VimeoGrabber.pm
new file mode 100644 (file)
index 0000000..3e8ce6a
--- /dev/null
@@ -0,0 +1,93 @@
+# (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+#
+# Grabber for vimeo.com
+
+package VimeoGrabber;
+
+use GrabberBase;
+@ISA = qw(GrabberBase);
+
+use LWP::Simple qw(!get);
+use XML::Simple;
+use Digest::MD5 qw(md5_hex);
+use Data::Dumper;
+
+use strict;
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new();
+
+    $self->{'NAME'} = 'vimeo';
+    $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*vimeo.com/(\d+))'];
+
+    bless($self, $class);
+    $self->_prepare_parameters();
+
+    return $self;
+}
+
+sub _parse {
+    my $self = shift;
+    my $url = shift;
+    my $pattern = shift;
+    my $content;
+    my $metadata = {};
+    my $p = XML::Simple->new();
+    my $t;
+    my $dlurl;
+    my $dlpath;
+    my $timestamp;
+    my $hash;
+
+    $url =~ m|$pattern|;
+    $url = $1;
+
+    $metadata->{'URL'} = $url;
+    $metadata->{'ID'} = $2;
+    $metadata->{'TYPE'} = 'video';
+    $metadata->{'SOURCE'} = $self->{'NAME'};
+    $metadata->{'TITLE'} = undef;
+    $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)))) {
+        $self->error('Could not download XML metadata');
+        return undef;
+    }
+
+    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;
+    }
+
+    # 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 {
+        return undef;
+    }
+
+    $metadata->{'DLURL'} = sprintf('%s?e=%d&h=%s', $dlurl, $timestamp, $hash);
+    $metadata->{'TITLE'} = $t->{'xml'}->{'video'}->{'caption'};
+
+    unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
+        $self->error('Could not extract download URL and title');
+        return undef;
+    }
+
+    return $metadata;
+}
+
+1;