fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite / HTMLHelper.pm
1 #
2 # A helper class for getting values out of a HTML document
3 #
4
5 package videosite::HTMLHelper;
6
7 use HTML::TreeBuilder;
8 @ISA = qw(HTML::TreeBuilder);
9
10 use strict;
11 use Data::Dumper;
12 use LWP::Simple qw(!get);
13
14 sub new {
15     my $class = shift;
16     my $self = $class->SUPER::new(@_);
17
18     return bless($self, $class);
19 }
20
21 sub load {
22     my $self = shift;
23     my $URL = shift;
24     my $c;
25
26     unless(defined($c = LWP::Simple::get($URL))) {
27         # Error loading URL
28         return undef;
29     }
30
31     $self->parse($c);
32
33     return 1;
34 }
35
36 sub findnodes {
37     my $self = shift;
38     my $path = shift;
39     my ($tagname, $classifier);
40     my %matchtag;
41
42     #
43     # Try to make sense of the path specifier.
44     # For the moment, we just allow paths of the following two forms:
45     #
46     # a) <tagname>
47     # b) <tagname>[@<attribute>=<value> (, @<attribute>=<value> ...)]
48     #
49
50     unless (($tagname, $classifier) = $path =~
51             m|(\w+)(?:\[((?:\@[\w-]+=\x22[^\x22]+?\x22)(?:\s*,\s*(?:\@\w+=\x22[^\x22]+?\x22))*)\])?|) {
52         # bad path name
53         return wantarray?():undef;
54     }
55
56     $matchtag{'_tag'} = $tagname;
57     if (defined($classifier)) {
58         foreach (split(/\s*,\s*/, $classifier)) {
59             my ($n, $v) = split(/=/, $_, 2);
60             $n =~ s/^\@//;
61             $v =~ s/"//g;
62             $matchtag{$n} = $v;
63         }
64     }
65
66     return $self->look_down(%matchtag);
67 }
68
69 1;