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