Use API for output
[videosite.git] / videosite / MetaCafeGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for metacafe.com
5
6 package videosite::MetaCafeGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use HTML::Parser;
12 use Data::Dumper;
13
14 use strict;
15
16 sub new {
17     my $class = shift;
18     my $self = $class->SUPER::new(
19         NAME => 'metacafe',
20         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*metacafe.com/watch/(\d+)(?:\S+)?)'],
21     );
22
23     return bless($self, $class);
24 }
25
26 sub _parse {
27     my $self = shift;
28     my $url = shift;
29     my $pattern = shift;
30     my $content;
31     my $metadata = {};
32     my $p = HTML::Parser->new(api_version => 3);
33     my @accum;
34     my @text;
35     my $e;
36
37     $url =~ m|$pattern|;
38     $url = $1;
39
40     $metadata->{'URL'} = $url;
41     $metadata->{'ID'} = $2;
42     $metadata->{'TYPE'} = 'video';
43     $metadata->{'SOURCE'} = $self->{'NAME'};
44     $metadata->{'TITLE'} = undef;
45     $metadata->{'DLURL'} = undef;
46
47     unless(defined($content = $self->simple_get(sprintf('http://www.metacafe.com/watch/%s', $2)))) {
48         $self->error('Could not download %s', $url);
49         return undef;
50     }
51
52     $p->handler(start => \@accum, "tagname, attr");
53     $p->handler(text => \@text, "text");
54     $p->report_tags(qw(meta script));
55     $p->utf8_mode(1);
56     $p->parse($content);
57
58     # Look for the title in the meta tags
59     foreach $e (@accum) {
60         if ('meta' eq $e->[0]) {
61             if ('title' eq $e->[1]->{'name'}) {
62                 $metadata->{'TITLE'} = $e->[1]->{'content'};
63                 $metadata->{'TITLE'} =~ s/^Metacafe\s*-\s*//;
64             }
65         }
66     }
67
68     # Look for the download URL
69     foreach $e (@text) {
70         if ($e->[0] =~ m|\.addParam\("flashvars", ".*\&mediaURL=([^\&]+)\&.*"|) {
71             $metadata->{'DLURL'} = $1;
72         }
73     }
74
75     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
76         $self->error('Could not determine download URL');
77         return undef;
78     }
79
80     return $metadata;
81 }
82
83 1;