- Add copyright information to all files
[videosite.git] / videosite / DailyMotionGrabber.pm
1 # Grabber for dailymotion.com
2 #
3 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
4 # licensed under GNU GPL v2
5
6 package DailyMotionGrabber;
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'} = 'dailymotion';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*dailymotion.com/(?:[^/]+/)*video/([-a-zA-Z0-9_]+))'];
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'} = 'dailymotion';
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     unless(defined($content = LWP::Simple::get(sprintf('http://www.dailymotion.com/video/%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/^Dailymotion\s*:\s*//;
67             }
68         }
69     }
70
71     # Look for the download URL
72     foreach $e (@text) {
73         if ($e->[0] =~ m|\.addVariable\("url", "([^\"]+)"|) {
74             $metadata->{'DLURL'} = $1;
75             $metadata->{'DLURL'} =~ s/%(..)/chr(hex($1))/ge;
76
77         }
78     }
79
80     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
81         $self->error('Could not determine download URL');
82         return undef;
83     }
84
85     return $metadata;
86 }
87
88 1;