Merge branch 'master' of http://10.200.0.3/GIT/videosite
[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 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'} = 'myvideo';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*myvideo.de/watch/(\d+))'];
23
24     bless($self, $class);
25     $self->_prepare_parameters();
26
27     return $self;
28 }
29
30 sub _parse {
31     my $self = shift;
32     my $url = shift;
33     my $pattern = shift;
34     my $content;
35     my $metadata = {};
36     my $p = HTML::Parser->new(api_version => 3);
37     my @accum;
38     my @text;
39     my $e;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'video';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'TITLE'} = undef;
49     $metadata->{'DLURL'} = undef;
50
51     unless(defined($content = LWP::Simple::get(sprintf('http://www.myvideo.de/watch/%s', $2)))) {
52         $self->error('Could not download %s', $url);
53         return undef;
54     }
55
56     $p->handler(start => \@accum, "tagname, attr");
57     $p->report_tags(qw(meta link));
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/\s+Video\s+-\s+\S+\s+-\s+MyVideo$//;
67                 $self->debug("Found title: %s", $metadata->{'TITLE'});
68                 last;
69             }
70         }
71     }
72
73     # Look for the download URL
74     foreach $e (@accum) {
75         if ('link' eq $e->[0]) {
76             if (exists($e->[1]->{'rel'}) and ('image_src' eq $e->[1]->{'rel'})) {
77                 $metadata->{'DLURL'} = $e->[1]->{'href'};
78                 $metadata->{'DLURL'} =~ s,thumbs/[^/]*$,,;
79                 $metadata->{'DLURL'} .= $metadata->{'ID'} . ".flv";
80                 $self->debug("Found URL: %s", $metadata->{'DLURL'});
81                 last;
82             }
83         }
84     }
85
86     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
87         $self->error('Could not determine download URL');
88         return undef;
89     }
90
91     return $metadata;
92 }
93
94 1;