- Add copyright information to all files
[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 MetaCafeGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use HTML::Parser;
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'} = 'metacafe';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*metacafe.com/watch/(\d+)(?:\S+)?)'];
23
24     bless($self, $class);
25
26     return $self;
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $url = shift;
32     my $pattern = shift;
33     my $content;
34     my $metadata = {};
35     my $p = HTML::Parser->new(api_version => 3);
36     my @accum;
37     my @text;
38     my $e;
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'ID'} = $2;
45     $metadata->{'TYPE'} = 'video';
46     $metadata->{'SOURCE'} = $self->{'NAME'};
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     unless(defined($content = LWP::Simple::get(sprintf('http://www.metacafe.com/watch/%s', $2)))) {
51         $self->error('Could not download %s', $url);
52         return undef;
53     }
54
55     $p->handler(start => \@accum, "tagname, attr");
56     $p->handler(text => \@text, "text");
57     $p->report_tags(qw(meta script));
58     $p->utf8_mode(1);
59     $p->parse($content);
60
61     # Look for the title in the meta tags
62     foreach $e (@accum) {
63         if ('meta' eq $e->[0]) {
64             if ('title' eq $e->[1]->{'name'}) {
65                 $metadata->{'TITLE'} = $e->[1]->{'content'};
66                 $metadata->{'TITLE'} =~ s/^Metacafe\s*-\s*//;
67             }
68         }
69     }
70
71     # Look for the download URL
72     foreach $e (@text) {
73         if ($e->[0] =~ m|\.addParam\("flashvars", ".*\&mediaURL=([^\&]+)\&.*"|) {
74             $metadata->{'DLURL'} = $1;
75         }
76     }
77
78     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
79         $self->error('Could not determine download URL');
80         return undef;
81     }
82
83     return $metadata;
84 }
85
86 1;