| 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 | |||
| 6 | package Gungho::Component::Scraper; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Component); | ||
| 10 | use Web::Scraper::Config; | ||
| 11 | |||
| 12 | __PACKAGE__->mk_classdata(_scrapers => {}); | ||
| 13 | |||
| 14 | sub scrape | ||
| 15 | { | ||
| 16 | my ($c, $response, $arg) = @_; | ||
| 17 | my $scraper = $c->_load_scraper($arg); | ||
| 18 | $scraper->scrape($response->content); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub _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 | |||
| 43 | 1; | ||
| 44 | |||
| 45 | __END__ |