| 1 | # $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks.pm 40585 2008-01-29T15:58:05.363572Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package GunghoX::FollowLinks; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Component); | ||
| 10 | use Class::Null; | ||
| 11 | use Gungho::Util; | ||
| 12 | our $VERSION = '0.00006'; | ||
| 13 | |||
| 14 | __PACKAGE__->mk_classdata($_) for qw(follow_links_parsers follow_link_log); | ||
| 15 | |||
| 16 | sub setup | ||
| 17 | { | ||
| 18 | my $c = shift; | ||
| 19 | $c->next::method(); | ||
| 20 | |||
| 21 | my $config = $c->config->{follow_links}; | ||
| 22 | |||
| 23 | $c->follow_links_parsers( {} ); | ||
| 24 | foreach my $parser_config (@{ $config->{parsers} }) { | ||
| 25 | my $module = $parser_config->{module}; | ||
| 26 | my $pkg = Gungho::Util::load_module($module, 'GunghoX::FollowLinks::Parser'); | ||
| 27 | my $obj = $pkg->new( %{ $parser_config->{config} } ); | ||
| 28 | |||
| 29 | $obj->register( $c ); | ||
| 30 | } | ||
| 31 | |||
| 32 | $c; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub follow_links | ||
| 36 | { | ||
| 37 | my ($c, $response) = @_; | ||
| 38 | |||
| 39 | eval { | ||
| 40 | my $content_type = $response->content_type; | ||
| 41 | my $parser = | ||
| 42 | $c->follow_links_parsers->{ $content_type } || | ||
| 43 | $c->follow_links_parsers->{ 'DEFAULT' } | ||
| 44 | ; | ||
| 45 | if ($parser) { | ||
| 46 | $c->log->debug( "Parsing links for " . $response->request->uri ); | ||
| 47 | $parser->parse( $c, $response ); | ||
| 48 | } | ||
| 49 | }; | ||
| 50 | warn if $@; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |