Fix sevenload grabber
[videosite.git] / videosite / SevenloadGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for sevenload.com/de
5
6 package videosite::SevenloadGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use XML::Simple;
12 use HTML::TokeParser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new(
20         NAME => 'sevenload',
21         _SELFTESTURL => 'http://www.sevenload.com/videos/twins-one-guitar-5122ed655a1cb35c41003c64',
22         _SELFTESTTITLE => 'Twins one guitar',
23         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*sevenload\.com/videos/.+-([[:alnum:]]+))'],
24         @_,
25     );
26
27     return bless($self, $class);
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;
37     my $t;
38
39     $url =~ m|$pattern|;
40     $url = $1;
41
42     $metadata->{'URL'} = $url;
43     $metadata->{'ID'} = $2;
44     $metadata->{'TYPE'} = 'video';
45     $metadata->{'SOURCE'} = $self->{'NAME'};
46     $metadata->{'TITLE'} = undef;
47     $metadata->{'DLURL'} = undef;
48
49     # Get the HTML page for the title
50     unless(defined($content = $self->simple_get($url))) {
51         $self->error('Could not download HTM');
52         return undef;
53     }
54
55     $p = HTML::TokeParser->new(\$content);
56     while ($t = $p->get_tag('meta')) {
57         if ('meta' eq $t->[0]) {
58             if (exists($t->[1]->{'property'}) and ($t->[1]->{'property'} eq 'og:title')) {
59                 $metadata->{'TITLE'} = $t->[1]->{'content'};
60             }
61         }
62     }
63
64     # Get the XML file containing the video metadata
65     unless(defined($content = $self->simple_get(sprintf('http://player-services.sevenload.com/p/1/sp/1/playManifest/entryId/%s', $2)))) {
66         $self->error('Could not download XML metadata');
67         return undef;
68     }
69
70     $p = XML::Simple->new();
71     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
72         $self->error('Could not parse XML metadata');
73         return undef;
74     }
75
76     # Loop through the video streams
77     $metadata->{'DLURL'} = $t->{'manifest'}->{'media'}->{'url'};
78
79     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
80         $self->error('Could not extract download URL and title');
81         return undef;
82     }
83
84     return $metadata;
85 }
86
87 1;