Change new() function in plugins to avoid having to call _prepare_parameters() in...
[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     return bless($self, $class);
24 }
25
26 sub _parse {
27     my $self = shift;
28     my $url = shift;
29     my $pattern = shift;
30     my $content;
31     my $metadata = {};
32     my $p = HTML::Parser->new(api_version => 3);
33     my @accum;
34     my @text;
35     my $e;
36
37     $url =~ m|$pattern|;
38     $url = $1;
39
40     $metadata->{'URL'} = $url;
41     $metadata->{'ID'} = $2;
42     $metadata->{'TYPE'} = 'video';
43     $metadata->{'SOURCE'} = $self->{'NAME'};
44     $metadata->{'TITLE'} = undef;
45     $metadata->{'DLURL'} = undef;
46
47     unless(defined($content = $self->simple_get(sprintf('http://www.myvideo.de/watch/%s', $2)))) {
48         $self->error('Could not download %s', $url);
49         return undef;
50     }
51
52     $p->handler(start => \@accum, "tagname, attr");
53     $p->report_tags(qw(meta link));
54     $p->utf8_mode(1);
55     $p->parse($content);
56
57     # Look for the title in the meta tags
58     foreach $e (@accum) {
59         if ('meta' eq $e->[0]) {
60             if ('title' eq $e->[1]->{'name'}) {
61                 $metadata->{'TITLE'} = $e->[1]->{'content'};
62                 $metadata->{'TITLE'} =~ s/\s+Video\s+-\s+\S+\s+-\s+MyVideo$//;
63                 $self->debug("Found title: %s", $metadata->{'TITLE'});
64                 last;
65             }
66         }
67     }
68
69     # Look for the download URL
70     foreach $e (@accum) {
71         if ('link' eq $e->[0]) {
72             if (exists($e->[1]->{'rel'}) and ('image_src' eq $e->[1]->{'rel'})) {
73                 $metadata->{'DLURL'} = $e->[1]->{'href'};
74                 $metadata->{'DLURL'} =~ s,thumbs/[^/]*$,,;
75                 $metadata->{'DLURL'} .= $metadata->{'ID'} . ".flv";
76                 $self->debug("Found URL: %s", $metadata->{'DLURL'});
77                 last;
78             }
79         }
80     }
81
82     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
83         $self->error('Could not determine download URL');
84         return undef;
85     }
86
87     return $metadata;
88 }
89
90 1;