Snotr: Fix video title acquisition
[videosite.git] / videosite / VeohGrabber.pm
1 # (c) 2009 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for veoh.com
5
6 package videosite::VeohGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use XML::Simple;
12 use Data::Dumper;
13
14 use strict;
15
16 sub new {
17     my $class = shift;
18     my $self = $class->SUPER::new();
19
20     $self->{'NAME'} = 'veoh';
21     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*veoh.com/browse/videos/category/[^/]+/watch/([^/]+))'];
22
23     bless($self, $class);
24     $self->_prepare_parameters();
25
26     return $self;
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $url = shift;
32     my $pattern = shift;
33     my $content;
34     my $metadata = {};
35     my $p = XML::Simple->new();
36     my $ua = $self->ua();
37     my $t;
38     my $dlurl;
39     my $r;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'video';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'TITLE'} = undef;
49     $metadata->{'DLURL'} = undef;
50
51     # Get the XML file containing the video metadata
52     unless(defined($content = $self->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), $ua))) {
53         $self->error('Could not download XML metadata');
54         return undef;
55     }
56
57     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
58         $self->error('Could not parse XML metadata');
59         return undef;
60     }
61
62     if (exists($t->{'rsp'}->{'videoList'}->{'video'}->{'fullPreviewHashPath'})) {
63         $dlurl = $t->{'rsp'}->{'videoList'}->{'video'}->{'fullPreviewHashPath'};
64     } else {
65         $dlurl = $t->{'rsp'}->{'videoList'}->{'video'}->{'fullPreviewHashLowPath'}
66     }
67
68     unless(defined($dlurl)) {
69         $self->error('No dlurl found in XML');
70         return undef;
71     }
72
73     # We now have to fetch the dlurl to get the redirect target after it,
74     # because the dlurl itself must be called with the right referer set
75
76     $ua->max_redirect(0);
77     $r = $ua->get($dlurl, 'referer' => 'http://www.veoh.com');
78
79     unless ($r->is_redirect) {
80         $self->error('Expected redirect, got %s', $r->code);
81         return undef;
82     }
83
84     $metadata->{'DLURL'} = $r->header('Location');
85     $metadata->{'TITLE'} = $t->{'rsp'}->{'videoList'}->{'video'}->{'title'};
86
87     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
88         $self->error('Could not extract download URL and title');
89         return undef;
90     }
91
92     return $metadata;
93 }
94
95 1;