line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
2
|
|
|
2
|
|
2860
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
78
|
|
8
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
84
|
|
9
|
2
|
|
|
2
|
|
10
|
use base qw(Gungho::Component); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
699
|
|
10
|
2
|
|
|
2
|
|
995
|
use Class::Null; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
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__ |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 NAME |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
GunghoX::FollowLinks - Automatically Follow Links Within Responses |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
follow_links: |
64
|
|
|
|
|
|
|
parsers: |
65
|
|
|
|
|
|
|
- module: HTML |
66
|
|
|
|
|
|
|
config: |
67
|
|
|
|
|
|
|
rules: |
68
|
|
|
|
|
|
|
- module: HTML::SelectedTags |
69
|
|
|
|
|
|
|
config: |
70
|
|
|
|
|
|
|
tags: |
71
|
|
|
|
|
|
|
- a |
72
|
|
|
|
|
|
|
- module: MIME |
73
|
|
|
|
|
|
|
config: |
74
|
|
|
|
|
|
|
types: |
75
|
|
|
|
|
|
|
- text/html |
76
|
|
|
|
|
|
|
- module: Text |
77
|
|
|
|
|
|
|
config: |
78
|
|
|
|
|
|
|
rules: |
79
|
|
|
|
|
|
|
- module: URI |
80
|
|
|
|
|
|
|
config: |
81
|
|
|
|
|
|
|
match: |
82
|
|
|
|
|
|
|
- host: ^example\.com |
83
|
|
|
|
|
|
|
action: FOLLOW_ALLOW |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
package MyHandler; |
86
|
|
|
|
|
|
|
sub handle_response |
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
my ($self, $c, $req, $res) = @_; |
89
|
|
|
|
|
|
|
$c->follow_links($res); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 DESCRIPTION |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
The most common action that a crawler takes is to follow links on a page. |
95
|
|
|
|
|
|
|
This module helps you with that task. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 METHODS |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 setup |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 follow_links |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Parses the given HTTP::Response/Gungho::Response object and dispatches the |
104
|
|
|
|
|
|
|
appropriate parser from its content-type. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
For each URL found, Automatically dispatches the rules given to the parser, |
107
|
|
|
|
|
|
|
and if the rules match, the URL is sent to Gungho-E<gt>send_request. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Returns the number of matches found. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 AUTHOR |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Copyright (c) 2007 Daisuke Maki E<lt>daisuke@endeworks.jpE<gt> |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 LICENSE |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
118
|
|
|
|
|
|
|
under the same terms as Perl itself. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
See http://www.perl.com/perl/misc/Artistic.html |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |