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

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 139 CC 9 SCAR 32.8
CC
9
Cov
0%
track
72.0
BUILDARGS
6.0
uri
2.0
CRAP 26.7
1 package Parcel::Track::KR::Dongbu;
2 # ABSTRACT: Parcel::Track driver for the KG Dongbu Express (KG 동부택배)
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 File::Which;
14 use HTML::Selector::XPath;
15 use HTML::TreeBuilder::XPath;
16 use HTTP::Tiny;
17 use IPC::Open3;
18  
19 #
20 # to support HTTPS
21 #
22 use IO::Socket::SSL;
23 use Mozilla::CA;
24 use Net::SSLeay;
25  
26 our $URI =
27 'https://www.dongbups.com/newHtml/delivery/dvsearch.jsp?mode=SEARCH&search_type=1&sellNum=Y&search_item_no=%s';
28 our $AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
29  
30 sub BUILDARGS {
31 my ( $class, @args ) = @_;
32  
33 my %params;
34 if ( ref $args[0] eq 'HASH' ) {
35 %params = %{ $args[0] };
36 }
37 else {
38 %params = @args;
39 }
40 $params{id} =~ s/\D//g;
41  
42 return \%params;
43 }
44  
45 sub uri { sprintf( $URI, $_[0]->id ) }
46  
47 sub track {
48 my $self = shift;
49  
50 my %result = (
51 from => q{},
52 to => q{},
53 result => q{},
54 htmls => [],
55 descs => [],
56 );
57  
58 my $content;
59 if ( exists &Net::SSLeay::CTX_v2_new ) {
60 my $http = HTTP::Tiny->new(
61 agent => $AGENT,
62 SSL_options => { SSL_version => 'SSLv2', }
63 );
64  
65 my $res = $http->get( $self->uri );
66 print $res->{content};
67 unless ( $res->{success} ) {
68 $result{result} = 'failed to get parcel tracking info from the site';
69 return \%result;
70 }
71  
72 $content = Encode::encode( 'utf-8', Encode::decode( 'cp949', $res->{content} ) );
73 }
74 elsif ( my $wget = File::Which::which('wget') ) {
75 my ( $stdin, $stdout, $stderr );
76 my $pid = open3( $stdin, $stdout, $stderr, $wget, qw( -O - ), $self->uri )
77 or die "cannot run $wget process: $!\n";
78  
79 waitpid( $pid, 0 );
80  
81 my $fetched = do { local $/; <$stdout> };
82 $content = Encode::encode( 'utf-8', Encode::decode( 'cp949', $fetched ) );
83 }
84 else {
85 $result{result} =
86 'This version of OpenSSL has been compiled without SSLv2 support and there is no wget';
87 return \%result;
88 }
89  
90 unless ($content) {
91 $result{result} = 'failed to tracking parcel info';
92 return \%result;
93 }
94  
95 #
96 # http://stackoverflow.com/questions/19703341/disabling-html-entities-expanding-in-htmltreebuilder-perl-module
97 #
98 my $tree = HTML::TreeBuilder::XPath->new;
99 $tree->ignore_unknown(0);
100 $tree->no_expand_entities(1);
101 $tree->attr_encoded(1);
102 $tree->parse($content);
103 $tree->eof;
104  
105 my $prefix = '/html/body/div[2]/div[2]/div[2]/div[2]/';
106  
107 my $html1 = ( $tree->findnodes("$prefix/table") )[0];
108 unless ($html1) {
109 $result{result} = 'cannot find such parcel info';
110 return \%result;
111 }
112  
113 my $found = ( $tree->findnodes("$prefix/table/tbody/tr/td[1]") )[0];
114 my $found_text = $found ? $found->as_text : q{};
115 my $not_found = Encode::encode_utf8('해당운송장번호는 없습니다');
116 if ( $found_text =~ m/$not_found/ ) {
117 $result{result} = 'cannot find such parcel info';
118 return \%result;
119 }
120  
121 $result{from} = $tree->findvalue("$prefix/table/tbody/tr/td[2]");
122 $result{to} = $tree->findvalue("$prefix/table/tbody/tr/td[4]");
123 $result{result} = $self->_filter_text( $tree->findvalue("$prefix/table/tbody/tr/td[6]") );
124 $result{htmls} = [ $html1->as_HTML ];
125 $result{descs} = [ $result{result} ];
126  
127 return \%result;
128 }
129  
130 sub _filter_text {
131 my ( $self, $text ) = @_;
132  
133 $text =~ s/&nbsp;/ /gms;
134 $text =~ s/(^\s+|\s+$)//gms;
135 $text =~ s/ +/ /gms;
136  
137 return $text;
138 }
139  
140 1;
141  
142 #
143 # This file is part of Parcel-Track-KR-Dongbu
144 #
145 # This software is copyright (c) 2015 by Keedi Kim.
146 #
147 # This is free software; you can redistribute it and/or modify it under
148 # the same terms as the Perl 5 programming language system itself.
149 #
150  
151 __END__