Youtube: match .../watch?&v=... URLs
[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
20     $self->{'NAME'} = 'metacafe';
21     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*metacafe.com/watch/(\d+)(?:\S+)?)'];
22
23     bless($self, $class);
24
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 = HTML::Parser->new(api_version => 3);
37     my @accum;
38     my @text;
39     my $e;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'video';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'TITLE'} = undef;
49     $metadata->{'DLURL'} = undef;
50
51     unless(defined($content = $self->simple_get(sprintf('http://www.metacafe.com/watch/%s', $2)))) {
52         $self->error('Could not download %s', $url);
53         return undef;
54     }
55
56     $p->handler(start => \@accum, "tagname, attr");
57     $p->handler(text => \@text, "text");
58     $p->report_tags(qw(meta script));
59     $p->utf8_mode(1);
60     $p->parse($content);
61
62     # Look for the title in the meta tags
63     foreach $e (@accum) {
64         if ('meta' eq $e->[0]) {
65             if ('title' eq $e->[1]->{'name'}) {
66                 $metadata->{'TITLE'} = $e->[1]->{'content'};
67                 $metadata->{'TITLE'} =~ s/^Metacafe\s*-\s*//;
68             }
69         }
70     }
71
72     # Look for the download URL
73     foreach $e (@text) {
74         if ($e->[0] =~ m|\.addParam\("flashvars", ".*\&mediaURL=([^\&]+)\&.*"|) {
75             $metadata->{'DLURL'} = $1;
76         }
77     }
78
79     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
80         $self->error('Could not determine download URL');
81         return undef;
82     }
83
84     return $metadata;
85 }
86
87 1;