- Add copyright information to all files
[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 CollegeHumorGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::Simple qw(!get);
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
21     $self->{'NAME'} = 'collegehumor';
22     $self->{'PATTERNS'} = ['(http://www.collegehumor.com/video:(\d+))'];
23
24     bless($self, $class);
25     $self->_prepare_parameters();
26
27     return $self;
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 $t;
38
39     $url =~ m|$pattern|;
40     $url = $1;
41
42     $metadata->{'URL'} = $url;
43     $metadata->{'ID'} = $2;
44     $metadata->{'TYPE'} = 'video';
45     $metadata->{'SOURCE'} = 'collegehumor';
46     $metadata->{'TITLE'} = undef;
47     $metadata->{'DLURL'} = undef;
48
49     # Get the XML file containing the video metadata
50     unless(defined($content = LWP::Simple::get(sprintf('http://www.collegehumor.com/moogaloop/video:%s', $2)))) {
51         $self->error('Could not download XML metadata');
52         return undef;
53     }
54
55     unless(defined($t = $p->XMLin($content))) {
56         $self->error('Could not parse XML metadata');
57         return undef;
58     }
59
60     $metadata->{'DLURL'} = $t->{'video'}->{'file'};
61     $metadata->{'TITLE'} = $t->{'video'}->{'caption'};
62
63     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
64         $self->error('Could not extract download URL and title');
65         return undef;
66     }
67
68     return $metadata;
69 }
70
71 1;