# (c) 2007 by Ralf Ertzinger # 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; } # There is no XML header in the data, which makes XML::Simple unhappy $content = '' . $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; } # 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;