Change new() function in plugins to avoid having to call _prepare_parameters() in...
[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     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;
33     my $ua = $self->ua();
34
35     $url =~ m|$pattern|;
36     $url = $1;
37
38     $metadata->{'URL'} = $url;
39     $metadata->{'ID'} = $2;
40     $metadata->{'TYPE'} = 'video';
41     $metadata->{'SOURCE'} = $self->{'NAME'};
42     $metadata->{'TITLE'} = undef;
43     $metadata->{'DLURL'} = undef;
44
45     # Get the site to extract the title
46     unless(defined($content = $self->simple_get($url, $ua))) {
47         $self->error('Could not download page');
48         return undef;
49     }
50
51     $p = HTML::TokeParser->new(\$content);
52
53     # Look for the title
54     if ($p->get_tag('title')) {
55         $metadata->{'TITLE'} = $p->get_text();
56         $metadata->{'TITLE'} =~ s/^LiveLeak\.com\s+-\s+(.+)$/$1/im;
57     }
58
59     # Get the file containing the video metadata
60     unless(defined($content = $self->simple_get(sprintf('http://www.liveleak.com/mi?token=%s', $2), $ua))) {
61         $self->error('Could not download metadata');
62         return undef;
63     }
64
65     unless ($content =~ m/file_location=([^\&]+)\&/) {
66         $self->error('Could not find download URL');
67         return undef;
68     }
69     $metadata->{'DLURL'} = $1;
70     $metadata->{'DLURL'} =~ s/%(..)/chr(hex($1))/ge;
71
72     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
73         $self->error('Could not extract download URL and title');
74         return undef;
75     }
76
77     return $metadata;
78 }
79
80 1;