1# $Id: /mirror/gungho/lib/Gungho/Component/Scraper.pm 4037 2007-10-25T14:20:48.994833Z lestrrat  $
2#
3# Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4# All rights reserved.
5
6package Gungho::Component::Scraper;
7use strict;
8use warnings;
9use base qw(Gungho::Component);
10use Web::Scraper::Config;
11
12__PACKAGE__->mk_classdata(_scrapers => {});
13
14sub scrape
15{
16    my ($c, $response, $arg) = @_;
17    my $scraper = $c->_load_scraper($arg);
18    $scraper->scrape($response->content);
19}
20
21sub _load_scraper
22{
23    my ($c, $config) = @_;
24
25    my $name;
26    if (! ref $config) {
27        $name = $config;
28    } else {
29        $name = do {
30            require Data::Dumper;
31            require Digest::MD5;
32            local $Data::Dumper::Indent  = 1;
33            local $Data::Dumper::Sorkeys = 1;
34            local $Data::Dumper::Terse   = 1;
35            Digest::MD5::md5_hex( Data::Dumper::Dumper( $config ) );
36        };
37        die if $@;
38    }
39
40    $c->_scrapers->{ $name } ||= Web::Scraper::Config->new($config);
41}
42
431;
44
45__END__
46
47=head1 NAME
48
49Gungho::Component::Scraper - Web::Scraper From Within Gungho
50
51=head1 SYNOPSIS
52
53  # Either setup $name in config, or call
54  # $c->register_scraper_config($name, $config);
55  $c->scrape($response, $name);
56
57  $c->scrape($response, $config);
58
59=head1 DESCRIPTION
60
61This component allows you to use Web::Scraper (via Web::Scraper::Config) from
62within Gungho.
63
64=head1 METHODS
65
66=head2 scrape ($response, $config)
67
68=cut