- Study message before throwing it to the grabbers
[videosite.git] / videosite / CollegeHumorGrabber.pm
index 2d9b37f..3bda5dd 100644 (file)
@@ -10,6 +10,7 @@ use GrabberBase;
 
 use LWP::Simple qw(!get);
 use XML::Simple;
+use HTML::Parser;
 use Data::Dumper;
 
 use strict;
@@ -34,6 +35,7 @@ sub _parse {
     my $content;
     my $metadata = {};
     my $p = XML::Simple->new();
+    my @accum;
     my $t;
 
     $url =~ m|$pattern|;
@@ -42,7 +44,7 @@ sub _parse {
     $metadata->{'URL'} = $url;
     $metadata->{'ID'} = $2;
     $metadata->{'TYPE'} = 'video';
-    $metadata->{'SOURCE'} = 'collegehumor';
+    $metadata->{'SOURCE'} = $self->{'NAME'};
     $metadata->{'TITLE'} = undef;
     $metadata->{'DLURL'} = undef;
 
@@ -58,7 +60,29 @@ sub _parse {
     }
 
     $metadata->{'DLURL'} = $t->{'video'}->{'file'};
-    $metadata->{'TITLE'} = $t->{'video'}->{'caption'};
+
+    # The XML does not contain the full title of the video, for
+    # reasons possibly known to some jerk at CollegeHumor.
+    # So we'll have to parse the actual HTML, too.
+    unless(defined($content = LWP::Simple::get(sprintf('http://www.collegehumor.com/video:%s', $2)))) {
+        $self->error('Could not download HTML');
+        return undef;
+    }
+    $p = HTML::Parser->new(api_version => 3);
+
+    $p->handler(start => \@accum, "tagname, attr");
+    $p->report_tags(qw(meta));
+    $p->utf8_mode(1);
+    $p->parse($content);
+
+    # Look for the title in the meta tags
+    foreach $t (@accum) {
+        if ('meta' eq $t->[0]) {
+            if ('title' eq $t->[1]->{'name'}) {
+                $metadata->{'TITLE'} = $t->[1]->{'content'};
+            }
+        }
+    }
 
     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
         $self->error('Could not extract download URL and title');