line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::KuronekoYamato; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
29785
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
91
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
1234
|
use version; our $VERSION = qv('0.0.5'); |
|
1
|
|
|
|
|
4470
|
|
|
1
|
|
|
|
|
8
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Other recommended modules (uncomment to use): |
10
|
|
|
|
|
|
|
# use IO::Prompt; |
11
|
|
|
|
|
|
|
# use Perl6::Export; |
12
|
|
|
|
|
|
|
# use Perl6::Slurp; |
13
|
|
|
|
|
|
|
# use Perl6::Say; |
14
|
1
|
|
|
1
|
|
1084
|
use Encode; |
|
1
|
|
|
|
|
20578
|
|
|
1
|
|
|
|
|
157
|
|
15
|
1
|
|
|
1
|
|
644
|
use WWW::Mechanize; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use Web::Scraper; |
17
|
|
|
|
|
|
|
use YAML; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Module implementation here |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# コンストラクタ |
23
|
|
|
|
|
|
|
sub new { |
24
|
|
|
|
|
|
|
my $class = shift; |
25
|
|
|
|
|
|
|
my $self; |
26
|
|
|
|
|
|
|
my $mech = WWW::Mechanize->new(); |
27
|
|
|
|
|
|
|
$mech->agent_alias( 'Windows IE 6' ); |
28
|
|
|
|
|
|
|
$mech->get('http://toi.kuronekoyamato.co.jp/cgi-bin/tneko?init'); |
29
|
|
|
|
|
|
|
$self->{mech} = $mech; |
30
|
|
|
|
|
|
|
$self->{user_agent} = __PACKAGE__; |
31
|
|
|
|
|
|
|
return bless $self, $class; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# ヤマト運輸に問い合わせ |
35
|
|
|
|
|
|
|
sub check { |
36
|
|
|
|
|
|
|
my $self = shift; |
37
|
|
|
|
|
|
|
my $numbers = shift; # 荷物問い合わせ番号のリストのリファレンス |
38
|
|
|
|
|
|
|
# フォームの問い合わせは10件ごとなので10件ごとのリストのリストにする |
39
|
|
|
|
|
|
|
my $list; # 10件ごとに分割されたリストのリストが入る |
40
|
|
|
|
|
|
|
my $j = -1; #添え字調整 |
41
|
|
|
|
|
|
|
foreach ( my $i = 0; $i < $#$numbers + 1; $i++ ) { |
42
|
|
|
|
|
|
|
$j++ unless $i % 10; |
43
|
|
|
|
|
|
|
push @{$list->[$j]}, $numbers->[$i]; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
# _requestを呼んで実際にWebアクセスする |
46
|
|
|
|
|
|
|
my $result = []; |
47
|
|
|
|
|
|
|
foreach my $item( @$list ) { |
48
|
|
|
|
|
|
|
sleep 5 if $#$result != -1; # 2回目のアクセスの前に5秒ウェイト |
49
|
|
|
|
|
|
|
my $res = _request($self, $item); |
50
|
|
|
|
|
|
|
push @$result, @$res; # 返答は最大10件なので、$resultにためていく |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
return $result; # 集まったリストを返す |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# 実際にリストからアクセスする |
56
|
|
|
|
|
|
|
sub _request { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
my $list = shift; |
59
|
|
|
|
|
|
|
$self->{mech}->form_number(1); |
60
|
|
|
|
|
|
|
for ( my $i = 0; $i < $#$list + 1; $i++) { |
61
|
|
|
|
|
|
|
my $field = sprintf "number%02d", $i+1; |
62
|
|
|
|
|
|
|
$self->{mech}->set_fields( $field => $list->[$i]); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
$self->{mech}->submit; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Web::Scraper による解析 |
67
|
|
|
|
|
|
|
my $s = scraper { |
68
|
|
|
|
|
|
|
process '//tr/td[2]/input/../../td[3][contains(. , "-")]/..', |
69
|
|
|
|
|
|
|
'results[]' => scraper { |
70
|
|
|
|
|
|
|
process '//td[3]', |
71
|
|
|
|
|
|
|
number => 'TEXT', |
72
|
|
|
|
|
|
|
process '//td[4]', |
73
|
|
|
|
|
|
|
date => 'TEXT', |
74
|
|
|
|
|
|
|
process '//td[5]', |
75
|
|
|
|
|
|
|
status => 'TEXT', |
76
|
|
|
|
|
|
|
}, |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
my $res = $s->scrape( |
79
|
|
|
|
|
|
|
$self->{mech}->content() |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
# 得られた結果をリストで返す |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $res2 = []; |
84
|
|
|
|
|
|
|
foreach my $item ( @{$res->{results}} ) { |
85
|
|
|
|
|
|
|
my $item2 = {}; |
86
|
|
|
|
|
|
|
foreach my $key ( keys %$item ) { |
87
|
|
|
|
|
|
|
$item2->{$key} = encode('utf8', $item->{$key}); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
delete $item2->{date} if $item2->{date} eq q(); |
90
|
|
|
|
|
|
|
$item2->{user_agent} = $self->{user_agent}; |
91
|
|
|
|
|
|
|
push @$res2, $item2; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
$res->{results} = $res2; |
94
|
|
|
|
|
|
|
return $res->{results}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub dump { |
98
|
|
|
|
|
|
|
my $self = shift; |
99
|
|
|
|
|
|
|
print Dump($self); |
100
|
|
|
|
|
|
|
return; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
104
|
|
|
|
|
|
|
__END__ |