Use API for output
[videosite.git] / videosite / CollegeHumorGrabber.pm
1 # Grabber for collegehumor.com
2 #
3 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
5
6 package videosite::CollegeHumorGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use videosite::HTMLHelper;
12 use XML::Simple;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new(
20         NAME => 'collegehumor',
21         _SELFTESTURL => 'http://www.collegehumor.com/video/5635400/pixar-intro-parody',
22         _SELFTESTTITLE => 'Pixar Intro Parody',
23         PATTERNS => ['(http://www.collegehumor.com/video:(\d+))',
24                      '(http://www.collegehumor.com/video/(\d+))'],
25     );
26
27     return bless($self, $class);
28 }
29
30 sub _parse {
31     my $self = shift;
32     my $url = shift;
33     my $pattern = shift;
34     my $content;
35     my $metadata = {};
36     my $p = XML::Simple->new();
37     my @accum;
38     my $t;
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'ID'} = $2;
45     $metadata->{'TYPE'} = 'video';
46     $metadata->{'SOURCE'} = $self->{'NAME'};
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     # Get the XML file containing the video metadata
51     unless(defined($content = $self->simple_get(sprintf('http://www.collegehumor.com/moogaloop/video/%s', $2)))) {
52         $self->error('Could not download XML metadata');
53         return undef;
54     }
55
56     unless(defined($t = $p->XMLin($content))) {
57         $self->error('Could not parse XML metadata');
58         return undef;
59     }
60
61     $metadata->{'DLURL'} = $t->{'video'}->{'file'};
62     $metadata->{'TITLE'} = $t->{'video'}->{'caption'};
63
64     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
65         $self->error('Could not extract download URL and title');
66         return undef;
67     }
68
69     return $metadata;
70 }
71
72 1;