Youtube: Simplify regex for scraping
[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
20     $self->{'NAME'} = 'liveleak';
21     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*liveleak.com/view\?i=([^\&]+))'];
22
23     bless($self, $class);
24     $self->_prepare_parameters();
25
26     return $self;
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $url = shift;
32     my $pattern = shift;
33     my $content;
34     my $metadata = {};
35     my $p;
36     my $ua = $self->ua();
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 = $self->simple_get($url, $ua))) {
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 = $self->simple_get(sprintf('http://www.liveleak.com/mi?token=%s', $2), $ua))) {
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;