| 1 | # $Id: /mirror/perl/Web-Scraper-Config/trunk/lib/Web/Scraper/Config.pm 7145 2007-05-09T16:36:57.901467Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | |||
| 5 | package Web::Scraper::Config; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Config::Any; | ||
| 9 | use Data::Visitor::Callback; | ||
| 10 | use Web::Scraper; | ||
| 11 | use URI; | ||
| 12 | our $VERSION = '0.01'; | ||
| 13 | |||
| 14 | sub new | ||
| 15 | { | ||
| 16 | my $class = shift; | ||
| 17 | my $self = bless {}, $class; | ||
| 18 | my $config = shift; | ||
| 19 | my $opts = shift; | ||
| 20 | $config = $self->_load_config($config); | ||
| 21 | |||
| 22 | { # BROKEN YAML::Syck | ||
| 23 | my $v = Data::Visitor::Callback->new( | ||
| 24 | plain_value => sub { $_[1] =~ s/(\w:) /$1/g; $_[1] } | ||
| 25 | ); | ||
| 26 | $config = $v->visit($config); | ||
| 27 | } | ||
| 28 | |||
| 29 | $self->{config} = $config; | ||
| 30 | $self->{callbacks} = $opts->{callbacks} if $opts && $opts->{callbacks}; | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _load_config | ||
| 35 | { | ||
| 36 | my $self = shift; | ||
| 37 | my $file = shift; | ||
| 38 | |||
| 39 | if (ref $file eq 'HASH') { | ||
| 40 | return $file; | ||
| 41 | } else { | ||
| 42 | # This is a bit hackish, but we're only loading one file, so | ||
| 43 | # we should be okay | ||
| 44 | my $list = Config::Any->load_files({ files => [ $file ]}); | ||
| 45 | if (! @$list ) { | ||
| 46 | require Carp; | ||
| 47 | Carp::croak("Could not load config file $file: $@"); | ||
| 48 | } | ||
| 49 | return (values %{$list->[0]})[0]; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | sub scrape | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | my $config = $self->{config}; | ||
| 57 | my $scraper = $self->_recurse($config)->(); | ||
| 58 | return $scraper->scrape(@_); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _recurse | ||
| 62 | { | ||
| 63 | my ($self, $rules) = @_; | ||
| 64 | |||
| 65 | my $ref = ref($rules); | ||
| 66 | my $ret; | ||
| 67 | if (! $ref) { | ||
| 68 | if ($rules =~ /^__callback\(([^\)]+)\)__$/) { | ||
| 69 | $rules = $self->{callbacks}{$1}; | ||
| 70 | } | ||
| 71 | $ret = $rules; | ||
| 72 | } elsif ($ref eq 'ARRAY') { | ||
| 73 | my @elements; | ||
| 74 | foreach my $rule (@$rules) { | ||
| 75 | if ($rule =~ /^__callback\(([^\)]+)\)__$/) { | ||
| 76 | $rule = $self->{callbacks}{$1}; | ||
| 77 | push @elements, sub { sub { $rule->(@_) } }; | ||
| 78 | } else { | ||
| 79 | push @elements, ref $rule ? $self->_recurse($rule) : $rule; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | if (! grep { my $ref = ref($_); $ref ? $ref ne 'CODE' : 1 } @elements) { | ||
| 84 | $ret = sub { | ||
| 85 | foreach my $code (@elements) { | ||
| 86 | $code->() | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | } else { | ||
| 90 | $ret = \@elements; | ||
| 91 | } | ||
| 92 | } elsif ($ref eq 'HASH'){ | ||
| 93 | my($op) = keys %$rules; | ||
| 94 | my $h = $self->_recurse($rules->{$op}); | ||
| 95 | my $is_func = ($op =~ /^(?:scraper|process(?:_first)?|result)$/); | ||
| 96 | |||
| 97 | if ($is_func) { | ||
| 98 | my @args = (ref $h eq 'ARRAY') ? @$h : ($h); | ||
| 99 | if ($op eq 'scraper') { | ||
| 100 | $ret = sub { | ||
| 101 | scraper(sub { for (@args) { $_->() } }) | ||
| 102 | }; | ||
| 103 | } else { | ||
| 104 | $ret = sub { | ||
| 105 | my $code = sub { | ||
| 106 | @_ = map { (ref $_ eq 'CODE') ? $_->() : $_ }@args; | ||
| 107 | goto &$op; | ||
| 108 | }; | ||
| 109 | $code->() | ||
| 110 | }; | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | $ret = { $op => $h }; | ||
| 114 | } | ||
| 115 | } else { | ||
| 116 | require Data::Dumper; | ||
| 117 | die "Web::Scraper::Config does not know how to parse: " . Data::Dumper::Dumper($rules); | ||
| 118 | } | ||
| 119 | |||
| 120 | return $ret; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ |