Add _selftest() function and test script to verify Grabber functionality
[videosite.git] / videosite / GrabberBase.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3
4 package videosite::GrabberBase;
5 use videosite::Base;
6 @ISA = qw(videosite::Base);
7
8 use strict;
9
10 sub new {
11     my $class = shift;
12     my $self = $class->SUPER::new();
13
14     $self = {%{$self},
15         NAME => 'FlashGrab',
16         TYPE => 'grabber',
17         PATTERNS => [],
18     };
19     return bless($self, $class);
20 }
21
22 sub get($$) {
23     my $self = shift;
24     my $url = shift;
25     my $pattern;
26     my $res;
27
28     return undef unless $self->_getval('enabled');
29
30     foreach $pattern (@{$self->{'PATTERNS'}}) {
31         $self->debug("Matching %s against %s", $pattern, $url);
32         if ($url =~ m|$pattern|) {
33             $self->debug("Match");
34             foreach ($self->connectors()) {
35                 $self->debug("Using connector %s", $_->{'name'});
36                 $self->selectconn($_);
37                 $res = $self->_parse($url, $pattern);
38                 if (defined($res)) {
39                     $res->{'CONNECTOR'} = $_;
40                     last;
41                 }
42             }
43             return wantarray?($res, $pattern):$res;
44         }
45     }
46
47     return undef;
48 }
49
50 sub enable {
51     my $self = shift;
52
53     $self->debug('Enabling %s grabber', $self->{'NAME'});
54     $self->setval('enabled', '1');
55 }
56
57 sub disable {
58     my $self = shift;
59
60     $self->debug('Disabling %s grabber', $self->{'NAME'});
61     $self->setval('enabled', '0');
62 }
63
64 sub _parse {
65     my $self = shift;
66     my $url = shift;
67
68     return undef;
69 }
70
71 sub _selftest {
72     my $self = shift;
73     my $info;
74
75     unless(exists($self->{_SELFTESTURL}) and exists($self->{_SELFTESTTITLE})) {
76         return 0;
77     }
78
79     unless(defined($info = $self->get($self->{_SELFTESTURL}))) {
80         $self->error("Could not get information from %s", $self->{_SELFTESTURL});
81         return undef;
82     }
83
84     unless($info->{TITLE} eq $self->{_SELFTESTTITLE}) {
85         $self->error("Title from info does not equal expected result (%s != %s)", $info->{TITLE}, $self->{_SELFTESTTITLE});
86         return undef;
87     }
88
89     return 1;
90 }
91
92 1;