Merge branch 'master' of http://10.200.0.3/GIT/videosite
[videosite.git] / videosite / LiveLeakGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for liveleak.com
5
6 package videosite::LiveLeakGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use LWP::Simple qw(!get);
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
21     $self->{'NAME'} = 'liveleak';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*liveleak.com/view\?i=([^\&]+))'];
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;
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     # Get the site to extract the title
49     unless(defined($content = LWP::Simple::get($url))) {
50         $self->error('Could not download page');
51         return undef;
52     }
53
54     $p = HTML::TokeParser->new(\$content);
55
56     # Look for the title
57     if ($p->get_tag('title')) {
58         $metadata->{'TITLE'} = $p->get_text();
59         $metadata->{'TITLE'} =~ s/^LiveLeak\.com\s+-\s+(.+)$/$1/im;
60     }
61
62     # Get the file containing the video metadata
63     unless(defined($content = LWP::Simple::get(sprintf('http://www.liveleak.com/mi?token=%s', $2)))) {
64         $self->error('Could not download metadata');
65         return undef;
66     }
67
68     unless ($content =~ m/file_location=([^\&]+)\&/) {
69         $self->error('Could not find download URL');
70         return undef;
71     }
72     $metadata->{'DLURL'} = $1;
73     $metadata->{'DLURL'} =~ s/%(..)/chr(hex($1))/ge;
74
75     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
76         $self->error('Could not extract download URL and title');
77         return undef;
78     }
79
80     return $metadata;
81 }
82
83 1;