- Add grabber for DailyMotion
[videosite.git] / videosite / DailyMotionGrabber.pm
1 #
2 # download strategy revised using
3 # http://www.kde-apps.org/content/show.php?content=41456
4
5 package DailyMotionGrabber;
6
7 use GrabberBase;
8 @ISA = qw(GrabberBase);
9
10 use LWP::Simple qw(!get);
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'} = 'youtube';
21     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*.dailymotion.com/(?:[^/]+/)*video/([-a-zA-Z0-9_]+)'];
22
23     bless($self, $class);
24
25     return $self;
26 }
27
28 sub _parse {
29     my $self = shift;
30     my $url = shift;
31     my $pattern = shift;
32     my $content;
33     my $metadata = {};
34     my $p = HTML::Parser->new(api_version => 3);
35     my @accum;
36     my @text;
37     my $e;
38
39     $url =~ m|$pattern|;
40     $url = $1;
41
42     $metadata->{'URL'} = $url;
43     $metadata->{'ID'} = $2;
44     $metadata->{'TYPE'} = 'video';
45     $metadata->{'SOURCE'} = 'dailymotion';
46     $metadata->{'TITLE'} = undef;
47     $metadata->{'DLURL'} = undef;
48
49     unless(defined($content = LWP::Simple::get(sprintf('http://www.dailymotion.com/video/%s', $2)))) {
50         $self->error('Could not download %s', $url);
51         return undef;
52     }
53
54     $p->handler(start => \@accum, "tagname, attr");
55     $p->handler(text => \@text, "text");
56     $p->report_tags(qw(meta script));
57     $p->utf8_mode(1);
58     $p->parse($content);
59
60     # Look for the title in the meta tags
61     foreach $e (@accum) {
62         if ('meta' eq $e->[0]) {
63             if ('title' eq $e->[1]->{'name'}) {
64                 $metadata->{'TITLE'} = $e->[1]->{'content'};
65             }
66         }
67     }
68
69     # Look for the download URL
70     foreach $e (@text) {
71         if ($e->[0] =~ m|\.add_variable("url", "([^\"]+)")|) {
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;