7be3806a3254bacad619d8c5568834511212c6ae
[videosite.git] / videosite / MyVideoGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for myvideo.de
5
6 package videosite::MyVideoGrabber;
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 => 'myvideo',
20         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*myvideo.de/watch/(\d+))'],
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.myvideo.de/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->report_tags(qw(meta link));
55     $p->utf8_mode(1);
56     $p->parse($content);
57
58     # Look for the title in the meta tags
59     foreach $e (@accum) {
60         if ('meta' eq $e->[0]) {
61             if ('title' eq $e->[1]->{'name'}) {
62                 $metadata->{'TITLE'} = $e->[1]->{'content'};
63                 $metadata->{'TITLE'} =~ s/\s+Video\s+-\s+\S+\s+-\s+MyVideo$//;
64                 $self->debug("Found title: %s", $metadata->{'TITLE'});
65                 last;
66             }
67         }
68     }
69
70     # Look for the download URL
71     foreach $e (@accum) {
72         if ('link' eq $e->[0]) {
73             if (exists($e->[1]->{'rel'}) and ('image_src' eq $e->[1]->{'rel'})) {
74                 $metadata->{'DLURL'} = $e->[1]->{'href'};
75                 $metadata->{'DLURL'} =~ s,thumbs/[^/]*$,,;
76                 $metadata->{'DLURL'} .= $metadata->{'ID'} . ".flv";
77                 $self->debug("Found URL: %s", $metadata->{'DLURL'});
78                 last;
79             }
80         }
81     }
82
83     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
84         $self->error('Could not determine download URL');
85         return undef;
86     }
87
88     return $metadata;
89 }
90
91 1;