Base, GrabberBase: change hash keys for connectors
[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 LWP::Simple qw(!get);
13 use XML::Simple;
14 use Data::Dumper;
15
16 use strict;
17
18 sub new {
19     my $class = shift;
20     my $self = $class->SUPER::new();
21
22     $self->{'NAME'} = 'collegehumor';
23     $self->{'PATTERNS'} = ['(http://www.collegehumor.com/video:(\d+))',
24                            '(http://www.collegehumor.com/video/(\d+))'];
25
26     bless($self, $class);
27     $self->_prepare_parameters();
28
29     return $self;
30 }
31
32 sub _parse {
33     my $self = shift;
34     my $url = shift;
35     my $pattern = shift;
36     my $content;
37     my $metadata = {};
38     my $p = XML::Simple->new();
39     my @accum;
40     my $t;
41
42     $url =~ m|$pattern|;
43     $url = $1;
44
45     $metadata->{'URL'} = $url;
46     $metadata->{'ID'} = $2;
47     $metadata->{'TYPE'} = 'video';
48     $metadata->{'SOURCE'} = $self->{'NAME'};
49     $metadata->{'TITLE'} = undef;
50     $metadata->{'DLURL'} = undef;
51
52     # Get the XML file containing the video metadata
53     unless(defined($content = LWP::Simple::get(sprintf('http://www.collegehumor.com/moogaloop/video/%s', $2)))) {
54         $self->error('Could not download XML metadata');
55         return undef;
56     }
57
58     unless(defined($t = $p->XMLin($content))) {
59         $self->error('Could not parse XML metadata');
60         return undef;
61     }
62
63     $metadata->{'DLURL'} = $t->{'video'}->{'file'};
64     $metadata->{'TITLE'} = $t->{'video'}->{'caption'};
65
66     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
67         $self->error('Could not extract download URL and title');
68         return undef;
69     }
70
71     return $metadata;
72 }
73
74 1;