line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks.pm 9010 2007-11-13T02:08:07.210715Z daisuke $ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> |
4
|
|
|
|
|
|
|
# All rights reserved. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package GunghoX::FollowLinks; |
7
|
2
|
|
|
2
|
|
1963
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
47
|
|
8
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
46
|
|
9
|
2
|
|
|
2
|
|
5
|
use base qw(Gungho::Component); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
418
|
|
10
|
2
|
|
|
2
|
|
897
|
use Gungho::Util; |
|
2
|
|
|
|
|
5933
|
|
|
2
|
|
|
|
|
16
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.00004'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata($_) for qw(follow_links_parsers); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub setup |
16
|
|
|
|
|
|
|
{ |
17
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
18
|
0
|
|
|
|
|
|
$c->next::method(); |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $config = $c->config->{follow_links}; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
$c->follow_links_parsers( {} ); |
23
|
0
|
|
|
|
|
|
foreach my $parser_config (@{ $config->{parsers} }) { |
|
0
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
my $module = $parser_config->{module}; |
25
|
0
|
|
|
|
|
|
my $pkg = Gungho::Util::load_module($module, 'GunghoX::FollowLinks::Parser'); |
26
|
0
|
|
|
|
|
|
my $obj = $pkg->new( %{ $parser_config->{config} } ); |
|
0
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
$obj->register( $c ); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
$c; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub follow_links |
35
|
|
|
|
|
|
|
{ |
36
|
0
|
|
|
0
|
1
|
|
my ($c, $response) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $content_type = $response->content_type; |
39
|
|
|
|
|
|
|
my $parser = |
40
|
|
|
|
|
|
|
$c->follow_links_parsers->{ $content_type } || |
41
|
0
|
|
0
|
|
|
|
$c->follow_links_parsers->{ 'DEFAULT' } |
42
|
|
|
|
|
|
|
; |
43
|
0
|
0
|
|
|
|
|
return () unless $parser; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
$c->log->debug( "Parsing links for " . $response->request->uri ); |
46
|
0
|
|
|
|
|
|
$parser->parse( $c, $response ); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
1; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
__END__ |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 NAME |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
GunghoX::FollowLinks - Automatically Follow Links Within Responses |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 SYNOPSIS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
follow_links: |
60
|
|
|
|
|
|
|
parsers: |
61
|
|
|
|
|
|
|
- module: HTML |
62
|
|
|
|
|
|
|
config: |
63
|
|
|
|
|
|
|
rules: |
64
|
|
|
|
|
|
|
- module: HTML::SelectedTags |
65
|
|
|
|
|
|
|
config: |
66
|
|
|
|
|
|
|
tags: |
67
|
|
|
|
|
|
|
- a |
68
|
|
|
|
|
|
|
- module: MIME |
69
|
|
|
|
|
|
|
config: |
70
|
|
|
|
|
|
|
types: |
71
|
|
|
|
|
|
|
- text/html |
72
|
|
|
|
|
|
|
- module: Text |
73
|
|
|
|
|
|
|
config: |
74
|
|
|
|
|
|
|
rules: |
75
|
|
|
|
|
|
|
- module: URI |
76
|
|
|
|
|
|
|
config: |
77
|
|
|
|
|
|
|
match: |
78
|
|
|
|
|
|
|
- host: ^example\.com |
79
|
|
|
|
|
|
|
action: FOLLOW_ALLOW |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
package MyHandler; |
82
|
|
|
|
|
|
|
sub handle_response |
83
|
|
|
|
|
|
|
{ |
84
|
|
|
|
|
|
|
my ($self, $c, $req, $res) = @_; |
85
|
|
|
|
|
|
|
$c->follow_links($res); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
The most common action that a crawler takes is to follow links on a page. |
91
|
|
|
|
|
|
|
This module helps you with that task. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 METHODS |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 setup |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 follow_links |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Parses the given HTTP::Response/Gungho::Response object and dispatches the |
100
|
|
|
|
|
|
|
appropriate parser from its content-type. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
For each URL found, Automatically dispatches the rules given to the parser, |
103
|
|
|
|
|
|
|
and if the rules match, the URL is sent to Gungho-E<gt>send_request. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Returns the number of matches found. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 AUTHOR |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Copyright (c) 2007 Daisuke Maki E<lt>daisuke@endeworks.jpE<gt> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 LICENSE |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
114
|
|
|
|
|
|
|
under the same terms as Perl itself. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
See http://www.perl.com/perl/misc/Artistic.html |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |