4521f374b3337d7645ef8895d9f65021ca70fc33
[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 HTML::TokeParser;
12 use Data::Dumper;
13
14 use strict;
15
16 sub new {
17     my $class = shift;
18     my $self = $class->SUPER::new(
19         NAME => 'liveleak',
20         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*liveleak.com/view\?i=([^\&]+))'],
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;
34     my $ua = $self->ua();
35
36     $url =~ m|$pattern|;
37     $url = $1;
38
39     $metadata->{'URL'} = $url;
40     $metadata->{'ID'} = $2;
41     $metadata->{'TYPE'} = 'video';
42     $metadata->{'SOURCE'} = $self->{'NAME'};
43     $metadata->{'TITLE'} = undef;
44     $metadata->{'DLURL'} = undef;
45
46     # Get the site to extract the title
47     unless(defined($content = $self->simple_get($url, $ua))) {
48         $self->error('Could not download page');
49         return undef;
50     }
51
52     $p = HTML::TokeParser->new(\$content);
53
54     # Look for the title
55     if ($p->get_tag('title')) {
56         $metadata->{'TITLE'} = $p->get_text();
57         $metadata->{'TITLE'} =~ s/^LiveLeak\.com\s+-\s+(.+)$/$1/im;
58     }
59
60     # Get the file containing the video metadata
61     unless(defined($content = $self->simple_get(sprintf('http://www.liveleak.com/mi?token=%s', $2), $ua))) {
62         $self->error('Could not download metadata');
63         return undef;
64     }
65
66     unless ($content =~ m/file_location=([^\&]+)\&/) {
67         $self->error('Could not find download URL');
68         return undef;
69     }
70     $metadata->{'DLURL'} = $1;
71     $metadata->{'DLURL'} =~ s/%(..)/chr(hex($1))/ge;
72
73     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
74         $self->error('Could not extract download URL and title');
75         return undef;
76     }
77
78     return $metadata;
79 }
80
81 1;