File Coverage

blib/lib/WebService/SagawaKyubin.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package WebService::SagawaKyubin;
2              
3 1     1   23744 use warnings;
  1         2  
  1         33  
4 1     1   5 use strict;
  1         1  
  1         35  
5 1     1   5 use Carp;
  1         5  
  1         91  
6              
7 1     1   1100 use version; our $VERSION = qv('0.0.5');
  1         3459  
  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   1683 use Encode;
  1         14661  
  1         85  
15 1     1   1127 use WWW::Mechanize;
  1         184288  
  1         46  
16 1     1   519 use Web::Scraper;
  0            
  0            
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             $self->{start_url} = 'http://k2k.sagawa-exp.co.jp/m/top.do';
28             $mech->agent_alias( 'Windows IE 6' );
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}->get( $self->{start_url} );
60             $self->{mech}->form_name('main');
61             for ( my $i = 0; $i < $#$list + 1; $i++) {
62             my $field = sprintf "main:no%d", $i+1;
63             $self->{mech}->set_fields( $field => $list->[$i]);
64             }
65             $self->{mech}->click('main:toiStart');
66              
67             # Web::Scraper による解析
68             my $s = scraper {
69             #process '//table/tr[2]/td/table/tr/td/form[2]/div/table/tbody/tr',
70             process '//form[2]/div/table/tbody/tr',
71             'results[]' => scraper {
72             process '//td[1]',
73             number => 'TEXT',
74             process '//td[2]',
75             date => 'TEXT',
76             process '//td[3]',
77             status => 'TEXT',
78             },
79             };
80             my $res = $s->scrape(
81             $self->{mech}->content()
82             );
83             # 得られた結果をリストで返す
84            
85             my $res2 = [];
86             foreach my $item ( @{$res->{results}} ) {
87             my $item2 = {};
88             foreach my $key ( keys %$item ) {
89             $item2->{$key} = encode_utf8( $item->{$key} );
90             }
91             $item2->{user_agent} = $self->{user_agent};
92             push @$res2, $item2;
93             }
94             $res->{results} = $res2;
95             return $res->{results};
96             }
97            
98             sub dump {
99             my $self = shift;
100             print Dump($self);
101             return;
102             }
103            
104             1; # Magic true value required at end of module
105             __END__