- Add grabber for snotr.com
[videosite.git] / videosite / SnotrGrabber.pm
1 # (c) 2008 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for snotr.com
5
6 package SnotrGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use HTML::Parser;
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'} = 'snotr';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*snotr\.com/video/(\d+))'];
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     my $t;
38     my @accum;
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'ID'} = $2;
45     $metadata->{'TYPE'} = 'video';
46     $metadata->{'SOURCE'} = $self->{'NAME'};
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     # Get the HTML file containing the title
51     unless(defined($content = LWP::Simple::get(sprintf('http://www.snotr.com/video/%s', $2)))) {
52         $self->error('Could not download HTML');
53         return undef;
54     }
55
56     $p = HTML::Parser->new(api_version => 3);
57
58     $p->handler(start => \@accum, "tagname, attr");
59     $p->report_tags(qw(meta));
60     $p->utf8_mode(1);
61     $p->parse($content);
62
63     # Look for the title in the meta tags
64     foreach $t (@accum) {
65         if ('meta' eq $t->[0]) {
66             if (exists($t->[1]->{'name'}) and ('title' eq $t->[1]->{'name'})) {
67                 $metadata->{'TITLE'} = $t->[1]->{'content'};
68                 last;
69             }
70         }
71     }
72
73     # The actual video URL is insultingly simple.
74     $metadata->{'DLURL'} = sprintf('http://videos.snotr.com/%s.flv', $metadata->{'ID'});
75
76     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
77         $self->error('Could not extract download URL and title');
78         return undef;
79     }
80
81     return $metadata;
82 }
83
84 1;