Youtube: match .../watch?&v=... URLs
[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
21     $self->{'NAME'} = 'collegehumor';
22     $self->{_SELFTESTURL} = 'http://www.collegehumor.com/video/5635400/pixar-intro-parody';
23     $self->{_SELFTESTTITLE} = 'Pixar Intro Parody';
24     $self->{'PATTERNS'} = ['(http://www.collegehumor.com/video:(\d+))',
25                            '(http://www.collegehumor.com/video/(\d+))'];
26
27     bless($self, $class);
28     $self->_prepare_parameters();
29
30     return $self;
31 }
32
33 sub _parse {
34     my $self = shift;
35     my $url = shift;
36     my $pattern = shift;
37     my $content;
38     my $metadata = {};
39     my $p = XML::Simple->new();
40     my @accum;
41     my $t;
42
43     $url =~ m|$pattern|;
44     $url = $1;
45
46     $metadata->{'URL'} = $url;
47     $metadata->{'ID'} = $2;
48     $metadata->{'TYPE'} = 'video';
49     $metadata->{'SOURCE'} = $self->{'NAME'};
50     $metadata->{'TITLE'} = undef;
51     $metadata->{'DLURL'} = undef;
52
53     # Get the XML file containing the video metadata
54     unless(defined($content = $self->simple_get(sprintf('http://www.collegehumor.com/moogaloop/video/%s', $2)))) {
55         $self->error('Could not download XML metadata');
56         return undef;
57     }
58
59     unless(defined($t = $p->XMLin($content))) {
60         $self->error('Could not parse XML metadata');
61         return undef;
62     }
63
64     $metadata->{'DLURL'} = $t->{'video'}->{'file'};
65     $metadata->{'TITLE'} = $t->{'video'}->{'caption'};
66
67     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
68         $self->error('Could not extract download URL and title');
69         return undef;
70     }
71
72     return $metadata;
73 }
74
75 1;