blib/lib/Parcel/Track/KR/KGB.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 117 CC 4 SCAR 14.8
CC
4
Cov
0%
track
12.0
BUILDARGS
6.0
uri
2.0
CRAP 4.4
1 package Parcel::Track::KR::KGB;
2 # ABSTRACT: Parcel::Track driver for the KGB LOGIS
3  
4 use utf8;
5  
6 use Moo;
7  
8 our $VERSION = '0.001';
9  
10 with 'Parcel::Track::Role::Base';
11  
12 use Encode;
13 use HTML::Selector::XPath;
14 use HTML::TreeBuilder::XPath;
15 use HTTP::Tiny;
16 use List::MoreUtils;
17  
18 #
19 # to support HTTPS
20 #
21 use IO::Socket::SSL;
22 use Mozilla::CA;
23 use Net::SSLeay;
24  
25 our $URI = 'https://www.kgbls.co.kr/sub5/trace.asp?f_slipno=%s';
26 our $AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
27  
28 sub BUILDARGS {
29 my ( $class, @args ) = @_;
30  
31 my %params;
32 if ( ref $args[0] eq 'HASH' ) {
33 %params = %{ $args[0] };
34 }
35 else {
36 %params = @args;
37 }
38 $params{id} =~ s/\D//g;
39  
40 return \%params;
41 }
42  
43 sub uri { sprintf( $URI, $_[0]->id ) }
44  
45 sub track {
46 my $self = shift;
47  
48 my %result = (
49 from => q{},
50 to => q{},
51 result => q{},
52 htmls => [],
53 descs => [],
54 );
55  
56 my $http = HTTP::Tiny->new(
57 agent => $AGENT,
58 verify_SSL => 1,
59 );
60  
61 my $res = $http->get( $self->uri );
62 unless ( $res->{success} ) {
63 $result{result} = 'failed to get parcel tracking info from the site';
64 return \%result;
65 }
66  
67 my $content = Encode::encode( 'utf-8', Encode::decode( 'cp949', $res->{content} ) );
68  
69 #
70 # http://stackoverflow.com/questions/19703341/disabling-html-entities-expanding-in-htmltreebuilder-perl-module
71 #
72 my $tree = HTML::TreeBuilder::XPath->new;
73 $tree->ignore_unknown(0);
74 $tree->no_expand_entities(1);
75 $tree->attr_encoded(1);
76 $tree->parse($content);
77 $tree->eof;
78  
79 my $prefix =
80 '/html/body/table[3]/tr/td[4]/table[2]/tr/td[2]/table[3]/tr/td[2]/table[1]/tr[2]/td/table/tr/td[2]';
81  
82 my @tables = ( $tree->findnodes("$prefix/table") );
83  
84 #
85 # remove some need-less inner tables
86 #
87 shift @tables;
88 shift @tables;
89 shift @tables;
90 pop @tables;
91  
92 #
93 # extract each rows
94 #
95 my @real_tables;
96 my @table_indexes;
97 my $it = List::MoreUtils::natatime( 2, @tables );
98 while ( my ( $table, $border ) = $it->() ) {
99 push @real_tables, $table;
100 }
101  
102 unless (@real_tables) {
103 $result{result} = 'cannot find such parcel info';
104 return \%result;
105 }
106  
107 #$result{from} = 'N/A'; # KGB doesn't provide from information
108 #$result{to} = 'N/A'; # KGB doesn't provide to information
109 $result{htmls} = [ join( "\n", map $_->as_HTML, @real_tables ) ];
110  
111 my $index = 0;
112 my $xpath_index = 0;
113 for my $table (@real_tables) {
114 $xpath_index = $index++ * 2 + 4;
115  
116 my $place = $tree->findvalue("$prefix/table[$xpath_index]/tr/td[1]");
117 my $time = $tree->findvalue("$prefix/table[$xpath_index]/tr/td[2]");
118 my $detail = $tree->findvalue("$prefix/table[$xpath_index]/tr/td[3]");
119  
120 push(
121 @{ $result{descs} },
122 map {
123 my $desc = $_;
124  
125 my $regex = Encode::encode_utf8('다\\.');
126 $desc =~ s/($regex)(\S)/$1 $2/gms;
127  
128 $desc =~ s/(^\s+|\s+$)//gms;
129 $desc =~ s/\r+//gms;
130 $desc =~ s/\n+/ /gms;
131 $desc =~ s/ +/ /gms;
132  
133 $desc;
134 } join( q{ }, $time, $place, $detail ),
135 );
136  
137 $result{result} = $result{descs}->[-1];
138 $result{result} =~ s/\..*//;
139 }
140  
141 return \%result;
142 }
143  
144 1;
145  
146 #
147 # This file is part of Parcel-Track-KR-KGB
148 #
149 # This software is copyright (c) 2015 by Keedi Kim.
150 #
151 # This is free software; you can redistribute it and/or modify it under
152 # the same terms as the Perl 5 programming language system itself.
153 #
154  
155 __END__