--- /dev/null
+# (c) 2009 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+#
+# Grabber for veoh.com
+
+package videosite::VeohGrabber;
+
+use videosite::GrabberBase;
+@ISA = qw(videosite::GrabberBase);
+
+use LWP::Simple qw(!get);
+use LWP::UserAgent;
+use XML::Simple;
+use Data::Dumper;
+
+use strict;
+
+sub new {
+ my $class = shift;
+ my $self = $class->SUPER::new();
+
+ $self->{'NAME'} = 'veoh';
+ $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*veoh.com/browse/videos/category/[^/]+/watch/([^/]+))'];
+
+ 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 $ua = LWP::UserAgent->new(max_redirect => 0);
+ my $t;
+ my $dlurl;
+ my $r;
+
+ $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.veoh.com/rest/v2/execute.xml?apiKey=5697781E-1C60-663B-FFD8-9B49D2B56D36&method=veoh.search.search&type=video&maxResults=1&permalink=%s&contentRatingId=1&', $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;
+ }
+
+ if (exists($t->{'rsp'}->{'videoList'}->{'video'}->{'fullPreviewHashPath'})) {
+ $dlurl = $t->{'rsp'}->{'videoList'}->{'video'}->{'fullPreviewHashPath'};
+ } else {
+ $dlurl = $t->{'rsp'}->{'videoList'}->{'video'}->{'fullPreviewHashLowPath'}
+ }
+
+ unless(defined($dlurl)) {
+ $self->error('No dlurl found in XML');
+ return undef;
+ }
+
+ # We now have to fetch the dlurl to get the redirect target after it,
+ # because the dlurl itself must be called with the right referer set
+
+ $r = $ua->get($dlurl, 'referer' => 'http://www.veoh.com');
+
+ unless ($r->is_redirect) {
+ $self->error('Expected redirect, got %s', $r->code);
+ return undef;
+ }
+
+ $metadata->{'DLURL'} = $r->header('Location');
+ $metadata->{'TITLE'} = $t->{'rsp'}->{'videoList'}->{'video'}->{'title'};
+
+ unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
+ $self->error('Could not extract download URL and title');
+ return undef;
+ }
+
+ return $metadata;
+}
+
+1;