| 1 | # ABSTRACT: get/post url, return unicode content, auto detect CJK charset | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Novel::Robot::Browser; | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = 0.22; | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | #use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Encode::Detect::CJK qw/detect/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use File::Slurp qw/slurp/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use HTTP::CookieJar; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use IO::Uncompress::Gunzip qw(gunzip); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | #use Parallel::ForkManager; | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Term::ProgressBar; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | our $DEFAULT_URL_CONTENT = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | our %DEFAULT_HEADER = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 'Accept-Charset' => 'gb2312,utf-8;q=0.7,*;q=0.7', | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 'Accept-Encoding' => "gzip", | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 'Accept-Language' => 'zh,zh-cn;q=0.8,en-us;q=0.5,en;q=0.3', | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 'Connection' => 'keep-alive', | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | #'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0', | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 'User-Agent' => 'User-Agent: MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1', | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 'DNT' => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my ( $self, %opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | $opt{retry} ||= 5; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"retry"} ||= 5
MC/DC0%0/1
$opt{"retry"} ||= 5
$opt{"retry"}
Truth table50%1/2
A
A= $opt{"retry"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $opt{max_process_num} ||= 5; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"max_process_num"} ||= 5
MC/DC0%0/1
$opt{"max_process_num"} ||= 5
$opt{"max_process_num"}
Truth table50%1/2
A
A= $opt{"max_process_num"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $opt{browser} ||= _init_browser( $opt{browser_headers} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$opt{"browser"} ||= _init_browser($opt{"browser_headers"})
MC/DC0%0/2
$opt{"browser"} ||= _init_browser($opt{"browser_headers"})
$opt{"browser"}
_init_browser($opt{"browser_headers"})
Truth table33%1/3
A ||= B
A= $opt{"browser"}B= _init_browser($opt{"browser_headers"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | $opt{use_chrome} ||= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"use_chrome"} ||= 0
MC/DC0%0/1
$opt{"use_chrome"} ||= 0
$opt{"use_chrome"}
Truth table50%1/2
A
A= $opt{"use_chrome"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | bless {%opt}, __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | sub _init_browser { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my ( $headers ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init_browser: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | $headers ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$headers ||= {}
MC/DC0%0/1
$headers ||= {}
$headers
Truth table50%1/2
A
A= $headers
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my %h = ( %DEFAULT_HEADER, %$headers ); | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | #my $cookie_jar = Novel::Robot::Browser::CookieJar->new(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | my $cookie_jar = HTTP::CookieJar->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | my $http = HTTP::Tiny->new( | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | default_headers => \%h, | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | cookie_jar => $cookie_jar, | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | return $http; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | sub request_url_whole { | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my ( $self, $url, %o ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_url_whole: not called CC=44 SCAR=75.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $html = $self->request_url( $url, $o{post_data} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $info = $o{info_sub}->( \$html ) || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$o{'info_sub'}->(\$html) || {}
MC/DC0%0/1
$o{'info_sub'}->(\$html) || {}
$o{'info_sub'}->(\$html)
Truth table0%0/2
A
A= $o{'info_sub'}->(\$html)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my $data_list = $o{item_list} || $o{item_list_sub}->( \$html ) || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$o{'item_list'} || $o{'item_list_sub'}->(\$html) || []
$o{"item_list"} or $o{"item_list_sub"}->(\$html)
MC/DC0%0/2
$o{'item_list'} || $o{'item_list_sub'}->(\$html) || []
$o{"item_list"}
$o{"item_list_sub"}->(\$html)
Truth table0%0/3
A or B
A= $o{"item_list"}B= $o{"item_list_sub"}->(\$html)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $i = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | unless ( $o{stop_sub} and $o{stop_sub}->( $info, $data_list, $i, %o ) or defined $o{item_list}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$o{"stop_sub"} and $o{"stop_sub"}->($info, $data_list, $i, %o) or defined $o{"item_list"}
$o{"stop_sub"} and $o{"stop_sub"}->($info, $data_list, $i, %o)
MC/DC0%0/3
$o{"stop_sub"} and $o{"stop_sub"}->($info, $data_list, $i, %o) or defined $o{"item_list"}
$o{"stop_sub"}
$o{"stop_sub"}->($info, $data_list, $i, %o)
defined $o{"item_list"}
Truth table0%0/5
A and B or C
A= $o{"stop_sub"}B= $o{"stop_sub"}->($info, $data_list, $i, %o)C= defined $o{"item_list"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $data_list = [] if ( $o{min_page_num} and $o{min_page_num} > 1 ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"min_page_num"} and $o{"min_page_num"} > 1
MC/DC0%0/2
$o{"min_page_num"} and $o{"min_page_num"} > 1
$o{"min_page_num"}
$o{"min_page_num"} > 1
Truth table0%0/3
A and B
A= $o{"min_page_num"}B= $o{"min_page_num"} > 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $page_list = exists $o{page_list_sub} ? $o{page_list_sub}->( \$html ) : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | while ( 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | my $u = | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | $page_list ? $page_list->[ $i - 2 ] : | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | ( exists $o{next_page_sub} ? $o{next_page_sub}->( $url, $i, \$html ) : undef ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | last unless ( $u ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | next if ( $o{min_page_num} and $i < $o{min_page_num} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"min_page_num"} and $i < $o{"min_page_num"}
MC/DC0%0/2
$o{"min_page_num"} and $i < $o{"min_page_num"}
$o{"min_page_num"}
$i < $o{"min_page_num"}
Truth table0%0/3
A and B
A= $o{"min_page_num"}B= $i < $o{"min_page_num"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | last if ( $o{max_page_num} and $i > $o{max_page_num} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"max_page_num"} and $i > $o{"max_page_num"}
MC/DC0%0/2
$o{"max_page_num"} and $i > $o{"max_page_num"}
$o{"max_page_num"}
$i > $o{"max_page_num"}
Truth table0%0/3
A and B
A= $o{"max_page_num"}B= $i > $o{"max_page_num"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my ( $u_url, $u_post_data ) = ref( $u ) eq 'HASH' ? @{$u}{qw/url post_data/} : ( $u, undef ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $c = $self->request_url( $u_url, $u_post_data ); | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $fs = $o{item_list_sub}->( \$c ); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | last unless ( $fs ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | push @$data_list, @$fs; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | last if ( $o{stop_sub} and $o{stop_sub}->( $info, $data_list, $i, %o ) ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"stop_sub"} and $o{"stop_sub"}->($info, $data_list, $i, %o)
MC/DC0%0/2
$o{"stop_sub"} and $o{"stop_sub"}->($info, $data_list, $i, %o)
$o{"stop_sub"}
$o{"stop_sub"}->($info, $data_list, $i, %o)
Truth table0%0/3
A and B
A= $o{"stop_sub"}B= $o{"stop_sub"}->($info, $data_list, $i, %o)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } ## end unless ( $o{stop_sub} and ...) | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | #lofter倒序 | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $o{reverse_item_list} ){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $data_list = [ reverse @$data_list ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $max_id = $data_list->[0]{id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if($max_id){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $_->{id} = $max_id - $_->{id} +1 for(@$data_list); | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $info->{item_num} = ( $#$data_list >= 0 and exists $data_list->[-1]{id} ) ? $data_list->[-1]{id} : ( scalar( @$data_list ) || $i ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$data_list->$#* >= 0 and exists $data_list->[-1]{"id"}
scalar @$data_list or $i
MC/DC0%0/2
$data_list->$#* >= 0 and exists $data_list->[-1]{"id"}
$data_list->$#* >= 0
exists $data_list->[-1]{"id"}
MC/DC0%0/2
scalar @$data_list or $i
scalar @$data_list
$i
Truth table0%0/3
A and B
A= $data_list->$#* >= 0B= exists $data_list->[-1]{"id"}
Truth table0%0/3
A or B
A= scalar @$data_listB= $i
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $o{item_sub} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $item_id = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | print "\n\n" if ( $o{progress} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $progress; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $progress = Term::ProgressBar->new( { count => scalar(@$data_list) } ) if ( $o{progress} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | for my $i ( 0 .. $#$data_list ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $r = $data_list->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $r->{id} //= ++$item_id; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$r->{"id"} //= ++$item_id
MC/DC0%0/2
$r->{"id"} //= ++$item_id
$r->{"id"}
++$item_id
Truth table0%0/3
A //= B
A= $r->{"id"}B= ++$item_id
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $r->{url} = URI->new_abs( $r->{url}, $url )->as_string; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | next unless ( $self->is_item_in_range( $r->{id}, $o{min_item_num}, $o{max_item_num} ) ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if(exists $o{back_index}){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | last if($i + $o{back_index} > $#$data_list); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if($r->{url}){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $c = $self->request_url( $r->{url}, $r->{post_data} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $temp_r = $o{item_sub}->( \$c ); | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $r->{$_} //= $temp_r->{$_} for keys(%$temp_r); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$r->{$_} //= $temp_r->{$_}
MC/DC0%0/2
$r->{$_} //= $temp_r->{$_}
$r->{$_}
$temp_r->{$_}
Truth table0%0/3
A //= B
A= $r->{$_}B= $temp_r->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $r = $o{item_sub}->( $r ); | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $next_url = URI->new_abs( $data_list->[$i+1]->{url}, $url )->as_string; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | while($r->{next_url}){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $r->{next_url} = URI->new_abs( $r->{next_url}, $url )->as_string; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if($r->{next_url} ne $next_url){ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my $c = $self->request_url( $r->{next_url}, $r->{post_data} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $temp_r = $o{item_sub}->( \$c ); | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $r->{content} .= "\n\n".$temp_r->{content}; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | last unless(exists $temp_r->{next_url}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $r->{next_url} = $temp_r->{next_url}; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | }else{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $progress->update( $item_id ) if ( $o{progress} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $progress->update( scalar(@$data_list) ) if ( $o{progress} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | print "\n\n" if ( $o{progress} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return ( $info, $data_list ); | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | } ## end sub request_url_whole | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | sub is_item_in_range { | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 6 | ▶ | my ( $self, $id, $min, $max ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_item_in_range: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 6 | ▶ | return 1 unless ( $id ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | ▶ | return 0 if ( $min and $id < $min ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$min and $id < $min
MC/DC0%0/2
$min and $id < $min
$min
$id < $min
Truth table33%1/3
A and B
A= $minB= $id < $min
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 6 | ▶ | return 0 if ( $max and $id > $max ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$max and $id > $max
MC/DC0%0/2
$max and $id > $max
$max
$id > $max
Truth table33%1/3
A and B
A= $maxB= $id > $max
| |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | sub is_list_overflow { | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my ( $self, $r, $max ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_list_overflow: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | return unless ( $max ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $item_num = scalar( @$r ); | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my $id = $r->[-1]{id} // $item_num; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$r->[-1]{"id"} // $item_num
MC/DC0%0/2
$r->[-1]{"id"} // $item_num
$r->[-1]{"id"}
$item_num
Truth table0%0/3
A // B
A= $r->[-1]{"id"}B= $item_num
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | return if ( $id < $max ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $#{$r} = $max - 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | sub request_url { | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | my ( $self, $url, $form ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_url: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | ▶ | return $DEFAULT_URL_CONTENT unless ( $url ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | my $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 2 | for my $i ( 1 .. $self->{retry} ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | eval { $c = $self->request_url_simple( $url, $form ); }; | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | last if ( $c ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | sleep 2; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | return $c || $DEFAULT_URL_CONTENT; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$c || $Novel::Robot::Browser::DEFAULT_URL_CONTENT
MC/DC0%0/2
$c || $Novel::Robot::Browser::DEFAULT_URL_CONTENT
$c
$Novel::Robot::Browser::DEFAULT_URL_CONTENT
Truth table33%1/3
A || B
A= $cB= $Novel::Robot::Browser::DEFAULT_URL_CONTENT
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | sub format_post_content { | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my ( $self, $form ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_post_content: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | return $form unless ( ref( $form ) eq 'HASH' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my @params; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | while ( my ( $k, $v ) = each %$form ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | push @params, uri_escape( $k ) . "=" . uri_escape( $v ); | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my $post_str = join( "&", @params ); | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | return $post_str; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | sub request_url_simple { | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | ▶ | my ( $self, $url, $form ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_url_simple: called CC=9 SCAR=27.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | if ( $form ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | $res = $self->{browser}->request( | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | 'POST', $url, | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | { content => $self->format_post_content( $form ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | headers => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 'content-type' => 'application/x-www-form-urlencoded', | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } elsif ( $self->{use_chrome} ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | $res->{content} = `chrome --no-sandbox --user-data-dir --headless --disable-gpu --dump-dom "$url" 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $res->{success} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | $res = $self->{browser}->get( $url ); | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | ▶ | return $DEFAULT_URL_CONTENT unless ( $res->{success} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | my $html; | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | 2 | my $content = $res->{content}; | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | 2 | ▶ | if ( $res->{headers}{'content-encoding'} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$res->{"headers"}{"content-encoding"} and $res->{"headers"}{"content-encoding"} eq "gzip"
MC/DC0%0/2
$res->{"headers"}{"content-encoding"} and $res->{"headers"}{"content-encoding"} eq "gzip"
$res->{"headers"}{"content-encoding"}
$res->{"headers"}{"content-encoding"} eq "gzip"
Truth table33%1/3
A and B
A= $res->{"headers"}{"content-encoding"}B= $res->{"headers"}{"content-encoding"} eq "gzip"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | and $res->{headers}{'content-encoding'} eq 'gzip' ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | gunzip \$content => \$html, MultiStream => 1, Append => 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | ▶ | my $charset = detect( $html || $content ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$html || $content
MC/DC0%0/2
$html || $content
$html
$content
Truth table33%1/3
A || B
A= $htmlB= $content
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 2 | ▶ | my $r = decode( $charset, $html || $content, Encode::FB_XMLCREF ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$html || $content
MC/DC0%0/2
$html || $content
$html
$content
Truth table33%1/3
A || B
A= $htmlB= $content
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | ▶ | return $r || $DEFAULT_URL_CONTENT; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$r || $Novel::Robot::Browser::DEFAULT_URL_CONTENT
MC/DC0%0/2
$r || $Novel::Robot::Browser::DEFAULT_URL_CONTENT
$r
$Novel::Robot::Browser::DEFAULT_URL_CONTENT
Truth table33%1/3
A || B
A= $rB= $Novel::Robot::Browser::DEFAULT_URL_CONTENT
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } ## end sub request_url_simple | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | sub read_moz_cookie { | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | my ( $self, $cookie, $dom ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_moz_cookie: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my @segment; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if ( -f $cookie and $cookie =~ /\.sqlite$/ ) { # firefox sqlite3 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
-f $cookie and $cookie =~ /\.sqlite$/
MC/DC0%0/2
-f $cookie and $cookie =~ /\.sqlite$/
-f $cookie
$cookie =~ /\.sqlite$/
Truth table0%0/3
A and B
A= -f $cookieB= $cookie =~ /\.sqlite$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my $sqlite3_cookie = | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | `sqlite3 "$cookie" "select host,isSecure,path,isHttpOnly,expiry,name,value from moz_cookies where baseDomain='$dom'"`; | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | @segment = map { [ split /\|/ ] } split /\n/, $sqlite3_cookie; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | ▶ | } elsif ( -f $cookie and $cookie =~ /\.txt$/ ) { # Netscape HTTP Cookie File | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
-f $cookie and $cookie =~ /\.txt$/
MC/DC0%0/2
-f $cookie and $cookie =~ /\.txt$/
-f $cookie
$cookie =~ /\.txt$/
Truth table0%0/3
A and B
A= -f $cookieB= $cookie =~ /\.txt$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my @ck = slurp( $cookie ); | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | @segment = grep { $_->[0] and $_->[0] =~ /(^|\.)\Q$dom\E$/ } map { [ split /\s+/ ] } @ck; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } else { # cookie string : name1=value1; name2=value2 | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my @ck = split /;\s*/, $cookie; | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | @segment = map { my @c = split /=/; [ $dom, undef, '/', undef, 0, $c[0], $c[1] ] } @ck; | |||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | @segment = grep { defined $_->[6] and $_->[6] =~ /\S/ } @segment; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | my @jar = map { "$_->[5]=$_->[6]; Domain=$_->[0]; Path=$_->[2]; Expiry=$_->[4]" } @segment; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $self->{browser}{cookie_jar}->load_cookies( @jar ); | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $cookie = join( "; ", map { "$_->[5]=$_->[6]" } @segment ); | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | #$self->{browser}{cookie_jar}{cookie} = $cookie; | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | return $cookie; | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } ## end sub read_moz_cookie | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||