lib/Novel/Robot/Parser.pm

filter: Statementstmt 39.8% 163 / 409 Branchbran 25.3% 38 / 150 Conditioncond 14.4% 18 / 125 MC/DCmcdc 3.0% 2 / 66 Subroutinesub 39.3% 26 / 66 Podpod 8.1% 3 / 37 totaltotal 29.3% 250 / 853 CC 139 SCAR 59.1
CC
139
Cov
32%
guess_item_list
1722.0
guess_novel_item
210.0
parse_novel_item
30.0
CRAP 368.2
« lib/Novel/Robot/Browser.pm ↑ Summary lib/Novel/Robot.pm »
1 # ABSTRACT: get novel / bbs content from website
2 package Novel::Robot::Parser;
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 #use Data::Dumper;
9 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Novel::Robot::Browser;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 #use Smart::Comments;
16  
17 3 our $VERSION = 0.32;
18  
19  
20 3 our %SITE_DOM_NAME = (
21 'bbs.jjwxc.net' => 'hjj',
22 'www.jjwxc.net' => 'jjwxc',
23 'm.jjwxc.net' => 'jjwxc',
24 'tieba.baidu.com' => 'tieba',
25  
26 'www.ddshu.net' => 'ddshu',
27 'www.kanunu8.com' => 'kanunu8',
28 );
29  
30 3 our %NULL_INDEX = (
31 url => '',
32 book => '',
33 writer => '',
34 writer_url => '',
35 item_list => [],
36  
37 intro => '',
38 series => '',
39 progress => '',
40 word_num => '',
41 );
42  
43 3 our %NULL_CHAPTER = (
44 content => '',
45 id => 0,
46 pid => 0,
47 time => '',
48 title => '',
49 url => '',
50 writer => '',
51 writer_say => '',
52 abstract => '',
53 word_num => '',
54 type => '',
55 );
56  
57  
58 sub new {
59 2 ▶ my ( $self, %opt ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
60  
61 2 $opt{site} = $self->detect_site( $opt{site} );
62  
63 2 my $module = "Novel::Robot::Parser::$opt{site}";
64 2 eval "require $module;";
65  
66 2 my $browser = Novel::Robot::Browser->new( %opt );
67 2 bless { browser => $browser, %opt }, $module;
68 }
69  
70 ▶sub domain { }
Subroutine domain: not called CC=1 SCAR=0.0
Pod undocumented
71  
72 sub detect_domain {
73 0 ▶ my ( $self, $url ) = @_;
Subroutine detect_domain: not called CC=3 SCAR=24.8
Pod undocumented
74 0 ▶ return ( $url, $url ) unless ( $url =~ /^https?:/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^https?:/ 0 0 0
75  
76 0 my ( $dom ) = $url =~ m#^.*?\/\/(.+?)(?:/|$)#;
77  
78 0 my $base_dom = $dom;
79 0 $base_dom =~ s/^[^.]+\.//;
80 0 ▶ $base_dom = $base_dom =~ /\./ ? $base_dom : $dom;
Branch: 1 branch
BranchTrueFalseTotal
$base_dom =~ /\./ ? : 0 0 0
81 0 return ( $dom, $base_dom );
82 }
83  
84 sub detect_site {
85 2 ▶ my ( $self, $s ) = @_;
Subroutine detect_site: called CC=7 SCAR=30.7
Pod undocumented
86  
87 2 ▶ if ( $s and $s =~ /^https?:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s and $s =~ /^https?:/) 0 2 2
Condition33%1/3
$s and $s =~ /^https?:/
!ll&&!rl&&r
020
MC/DC0%0/2
$s and $s =~ /^https?:/
$s $s =~ /^https?:/
Truth table33%1/3
A and B
A= $sB= $s =~ /^https?:/
ABresult
0X0
100
111
88 0 my ( $dom ) = $s =~ m#^.*?\/\/(.+?)/#;
89 0 ▶ return $SITE_DOM_NAME{$dom} if ( exists $SITE_DOM_NAME{$dom} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $Novel::Robot::Parser::SITE_DOM_NAME{$dom} 0 0 0
90 0 return 'default';
91 }
92  
93 2 ▶ if($s and -f $s){
Branch: 1 branch
BranchTrueFalseTotal
if ($s and -f $s) 0 2 2
Condition33%1/3
$s and -f $s
!ll&&!rl&&r
020
MC/DC0%0/2
$s and -f $s
$s -f $s
Truth table33%1/3
A and B
A= $sB= -f $s
ABresult
0X0
100
111
94 0 my ( $suffix ) = $s =~ m#\.([^.]+)$#;
95 0 ▶ return 'txt' if(lc($suffix) eq 'txt');
Branch: 1 branch
BranchTrueFalseTotal
if lc $suffix eq "txt" 0 0 0
96 0 return 'default';
97 }
98  
99 2 return $s;
100 }
101  
102 2 ▶sub site_type { 'novel' }
Subroutine site_type: called CC=1 SCAR=0.0
Pod undocumented
103 0 ▶sub charset { 'cp936' }
Subroutine charset: not called CC=1 SCAR=6.9
Pod undocumented
104 ▶sub base_url { }
Subroutine base_url: not called CC=1 SCAR=0.0
Pod undocumented
105  
106 sub generate_novel_url {
107 0 ▶ my ( $self, $index_url, @args ) = @_;
Subroutine generate_novel_url: not called CC=1 SCAR=6.9
Pod undocumented
108 0 return ( $index_url, @args );
109 }
110  
111  
112  
113 sub get_novel_info {
114 1 ▶ my ( $self, $url ) = @_;
Subroutine get_novel_info: called CC=1 SCAR=0.0
Pod documented
115 1 my ( $i_url, $post_data ) = $self->generate_novel_url( $url );
116 1 my $c = $self->{browser}->request_url( $i_url, $post_data );
117  
118 1 my $r = $self->extract_elements(
119 \$c,
120 path => $self->scrape_novel(),
121 sub => $self->can( "parse_novel" ),
122 );
123 1 $r->{url} = $i_url;
124 1 $r->{item_list} = $self->parse_item_list( \$c, $r );
125 1 ( $r->{item_list}, $r->{item_num} ) = $self->update_item_list( $r->{item_list}, $url );
126 1 return $r;
127 }
128  
129 sub get_novel_ref {
130 2 ▶ my ( $self, $index_url, %o ) = @_;
Subroutine get_novel_ref: called CC=9 SCAR=27.1
Pod documented
131  
132 2 ▶ return $self->get_tiezi_ref( $index_url, %o ) if ( $self->site_type() eq 'tiezi' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->site_type eq "tiezi" 0 2 2
133  
134 2 my ( $r, $item_list, $max_item_num );
135 2 ▶ if ( $index_url !~ /^https?:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $index_url =~ /^https?:/) { } 2 0 2
136 2 $r = $self->parse_novel( $index_url, %o );
137 } else {
138 0 my ( $i_url, $post_data ) = $self->generate_novel_url( $index_url );
139  
140 ( $r, $item_list ) = $self->{browser}->request_url_whole(
141 $i_url,
142 post_data => $post_data,
143 info_sub => sub {
144 0 ▶ $self->extract_elements(
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 @_,
146 path => $self->scrape_novel(),
147 sub => $self->can( "parse_novel" ),
148 );
149 },
150 0 ▶ item_list_sub => sub { $self->can( "parse_item_list" )->( $self, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 item_sub => sub {
152 0 ▶ $self->extract_elements(
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 @_,
154 path => $self->scrape_novel_item(),
155 sub => $self->can( "parse_novel_item" ),
156 );
157 },
158 0 %o,
159 );
160  
161 0 $r->{url} = $index_url;
162 0 ▶ $r->{item_list} = $item_list || [];
Condition0%0/2
$item_list || []
l!l
00
MC/DC0%0/1
$item_list || []
$item_list
Truth table0%0/2
A
A= $item_list
Aresult
11
00
163  
164 #$r->{item_num} = $max_item_num || undef;
165 } ## end else [ if ( $index_url !~ /^https?:/)]
166  
167 2 ( $r->{item_list}, $r->{item_num} ) = $self->update_item_list( $r->{item_list}, $index_url );
168 2 $self->filter_item_list( $r, %o );
169 2 $r->{writer_url} = $self->format_abs_url( $r->{writer_url}, $index_url );
170  
171 2 for my $k ( qw/writer book/ ) {
172 4 ▶ $r->{$k} = $o{$k} if ( exists $o{$k} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $o{$k} 0 4 4
173 }
174 2 ▶ $r->{$_} ||= $NULL_INDEX{$_} for keys( %NULL_INDEX );
Condition66%2/3
$r->{$_} ||= $Novel::Robot::Parser::NULL_INDEX{$_}
l!l&&r!l&&!r
6012
MC/DC50%1/2
$r->{$_} ||= $Novel::Robot::Parser::NULL_INDEX{$_}
$r->{$_} $Novel::Robot::Parser::NULL_INDEX{$_}
Truth table66%2/3
A ||= B
A= $r->{$_}B= $Novel::Robot::Parser::NULL_INDEX{$_}
ABresult
1X1
011
000
175 2 $r->{$_} = $self->tidy_string( $r->{$_} ) for qw/writer book/;
176  
177 2 return $r;
178 } ## end sub get_novel_ref
179  
180 sub scrape_novel {
181 1 ▶ my ( $self ) = @_;
Subroutine scrape_novel: called CC=5 SCAR=19.3
Pod undocumented
182 1 my $r = {};
183 1 ▶ push @{$r->{book}}, { path => $self->{book_path} } if ( exists $self->{book_path} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"book_path"} 0 1 1
184 1 ▶ push @{$r->{book}}, { regex => $self->{book_regex} } if ( exists $self->{book_regex} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"book_regex"} 0 1 1
185 1 ▶ push @{$r->{writer}}, { path => $self->{writer_path} } if ( exists $self->{writer_path} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"writer_path"} 0 1 1
186 1 ▶ push @{$r->{writer}}, { regex => $self->{writer_regex} } if ( exists $self->{writer_regex} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"writer_regex"} 0 1 1
187 1 return $r;
188 }
189  
190 sub parse_novel {
191 0 ▶ my ( $self, $h, $r ) = @_;
Subroutine parse_novel: not called CC=4 SCAR=30.0
Pod undocumented
192  
193 0 ▶ $r->{book} ||= $self->scrape_element_try(
Condition0%0/3
$r->{"book"} ||= $self->scrape_element_try($h, [{"path", "//meta[\@name=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:title\"]", "extract", "\@content"}, {"path", "//div[\@id=\"title\"]"}, {"path", "//div[\@class=\"title\"]"}, {"regex", qr/<title>[^<]+?([^,<]+?)\x{5168}\x{6587}\x{9605}\x{8bfb},/is}, {"regex", qr/<title>[^<]*?\x{300a}([^,<]+?)\x{300b}/is}, {"regex", qr/<title>[^<]+?,([^,<]+?)\x{6700}\x{65b0}\x{7ae0}\x{8282}/is}, {"path", "//h1"}, {"path", "//h2"}], "sub", $self->can("tidy_writer_book"))
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"book"} ||= $self->scrape_element_try($h, [{"path", "//meta[\@name=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:title\"]", "extract", "\@content"}, {"path", "//div[\@id=\"title\"]"}, {"path", "//div[\@class=\"title\"]"}, {"regex", qr/<title>[^<]+?([^,<]+?)\x{5168}\x{6587}\x{9605}\x{8bfb},/is}, {"regex", qr/<title>[^<]*?\x{300a}([^,<]+?)\x{300b}/is}, {"regex", qr/<title>[^<]+?,([^,<]+?)\x{6700}\x{65b0}\x{7ae0}\x{8282}/is}, {"path", "//h1"}, {"path", "//h2"}], "sub", $self->can("tidy_writer_book"))
$r->{"book"} $self->scrape_element_try($h, [{"path", "//meta[\@name=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:title\"]", "extract", "\@content"}, {"path", "//div[\@id=\"title\"]"}, {"path", "//div[\@class=\"title\"]"}, {"regex", qr/<title>[^<]+?([^,<]+?)\x{5168}\x{6587}\x{9605}\x{8bfb},/is}, {"regex", qr/<title>[^<]*?\x{300a}([^,<]+?)\x{300b}/is}, {"regex", qr/<title>[^<]+?,([^,<]+?)\x{6700}\x{65b0}\x{7ae0}\x{8282}/is}, {"path", "//h1"}, {"path", "//h2"}], "sub", $self->can("tidy_writer_book"))
Truth table0%0/3
A ||= B
A= $r->{"book"}B= $self->scrape_element_try($h, [{"path", "//meta[\@name=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:book_name\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:title\"]", "extract", "\@content"}, {"path", "//div[\@id=\"title\"]"}, {"path", "//div[\@class=\"title\"]"}, {"regex", qr/<title>[^<]+?([^,<]+?)\x{5168}\x{6587}\x{9605}\x{8bfb},/is}, {"regex", qr/<title>[^<]*?\x{300a}([^,<]+?)\x{300b}/is}, {"regex", qr/<title>[^<]+?,([^,<]+?)\x{6700}\x{65b0}\x{7ae0}\x{8282}/is}, {"path", "//h1"}, {"path", "//h2"}], "sub", $self->can("tidy_writer_book"))
ABresult
1X1
011
000
194 $h,
195 [ { path => '//meta[@name="og:novel:book_name"]', extract => '@content' },
196 { path => '//meta[@property="og:novel:book_name"]', extract => '@content' },
197 { path => '//meta[@property="og:title"]', extract => '@content' },
198 { path => '//div[@id="title"]', },
199 { path => '//div[@class="title"]', },
200 { regex => qr#<title>[^<]+?([^,<]+?)全文阅读,#si, },
201 { regex => qr#<title>[^<]*?《([^,<]+?)》#si, },
202 { regex => qr#<title>[^<]+?,([^,<]+?)最新章节#si, },
203 { path => '//h1', },
204 { path => '//h2', },
205 ],
206 sub => $self->can( "tidy_writer_book" ),
207 );
208  
209 0 ▶ $r->{writer} ||= $self->scrape_element_try(
Condition0%0/3
$r->{"writer"} ||= $self->scrape_element_try($h, [{"path", "//meta[\@name=\"author\"]", "extract", "\@content"}, {"path", "//meta[\@name=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//*[\@class=\"author\"]"}, {"path", "//*[\@class=\"writer\"]"}, {"regex", qr"<span>\x{4f5c}\x{8005}\x{ff1a}</span>([^<]+)"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}<span>([^<]+)</span>"is}, {"regex", qr"<(?:em|i|h3|h2|span)>\x{4f5c}\x{8005}\x{ff1a}([^<]+)</(?:em|i|h3|h2|span)>"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}(?:<span>)?<a[^>]*>([^<]+)</a>"is}, {"regex", qr"<p>\x{4f5c}(?:&nbsp;|\s)*\x{8005}\x{ff1a}([^<]+)</p>"is}, {"regex", qr/\x{4f5c}\x{8005}\x{ff1a}([^<]+?) \x{53d1}\x{5e03}\x{65f6}\x{95f4}\x{ff1a}/s}, {"regex", qr/content="([^"]+?)\x{6700}\x{65b0}\x{8457}\x{4f5c}/s}, {"regex", qr/<title>[^<,]+?\x{6700}\x{65b0}\x{7ae0}\x{8282}\(([^<,]+?)\),/is}, {"regex", qr/<title>[^<,]+?\x{4f5c}\x{8005}\x{ff1a}([^<,]+?)_/is}, {"regex", qr/content="[^"]+?,([^",]+?)\x{4f5c}\x{54c1}/s}], "sub", $self->can("tidy_writer_book"))
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"writer"} ||= $self->scrape_element_try($h, [{"path", "//meta[\@name=\"author\"]", "extract", "\@content"}, {"path", "//meta[\@name=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//*[\@class=\"author\"]"}, {"path", "//*[\@class=\"writer\"]"}, {"regex", qr"<span>\x{4f5c}\x{8005}\x{ff1a}</span>([^<]+)"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}<span>([^<]+)</span>"is}, {"regex", qr"<(?:em|i|h3|h2|span)>\x{4f5c}\x{8005}\x{ff1a}([^<]+)</(?:em|i|h3|h2|span)>"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}(?:<span>)?<a[^>]*>([^<]+)</a>"is}, {"regex", qr"<p>\x{4f5c}(?:&nbsp;|\s)*\x{8005}\x{ff1a}([^<]+)</p>"is}, {"regex", qr/\x{4f5c}\x{8005}\x{ff1a}([^<]+?) \x{53d1}\x{5e03}\x{65f6}\x{95f4}\x{ff1a}/s}, {"regex", qr/content="([^"]+?)\x{6700}\x{65b0}\x{8457}\x{4f5c}/s}, {"regex", qr/<title>[^<,]+?\x{6700}\x{65b0}\x{7ae0}\x{8282}\(([^<,]+?)\),/is}, {"regex", qr/<title>[^<,]+?\x{4f5c}\x{8005}\x{ff1a}([^<,]+?)_/is}, {"regex", qr/content="[^"]+?,([^",]+?)\x{4f5c}\x{54c1}/s}], "sub", $self->can("tidy_writer_book"))
$r->{"writer"} $self->scrape_element_try($h, [{"path", "//meta[\@name=\"author\"]", "extract", "\@content"}, {"path", "//meta[\@name=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//*[\@class=\"author\"]"}, {"path", "//*[\@class=\"writer\"]"}, {"regex", qr"<span>\x{4f5c}\x{8005}\x{ff1a}</span>([^<]+)"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}<span>([^<]+)</span>"is}, {"regex", qr"<(?:em|i|h3|h2|span)>\x{4f5c}\x{8005}\x{ff1a}([^<]+)</(?:em|i|h3|h2|span)>"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}(?:<span>)?<a[^>]*>([^<]+)</a>"is}, {"regex", qr"<p>\x{4f5c}(?:&nbsp;|\s)*\x{8005}\x{ff1a}([^<]+)</p>"is}, {"regex", qr/\x{4f5c}\x{8005}\x{ff1a}([^<]+?) \x{53d1}\x{5e03}\x{65f6}\x{95f4}\x{ff1a}/s}, {"regex", qr/content="([^"]+?)\x{6700}\x{65b0}\x{8457}\x{4f5c}/s}, {"regex", qr/<title>[^<,]+?\x{6700}\x{65b0}\x{7ae0}\x{8282}\(([^<,]+?)\),/is}, {"regex", qr/<title>[^<,]+?\x{4f5c}\x{8005}\x{ff1a}([^<,]+?)_/is}, {"regex", qr/content="[^"]+?,([^",]+?)\x{4f5c}\x{54c1}/s}], "sub", $self->can("tidy_writer_book"))
Truth table0%0/3
A ||= B
A= $r->{"writer"}B= $self->scrape_element_try($h, [{"path", "//meta[\@name=\"author\"]", "extract", "\@content"}, {"path", "//meta[\@name=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//meta[\@property=\"og:novel:author\"]", "extract", "\@content"}, {"path", "//*[\@class=\"author\"]"}, {"path", "//*[\@class=\"writer\"]"}, {"regex", qr"<span>\x{4f5c}\x{8005}\x{ff1a}</span>([^<]+)"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}<span>([^<]+)</span>"is}, {"regex", qr"<(?:em|i|h3|h2|span)>\x{4f5c}\x{8005}\x{ff1a}([^<]+)</(?:em|i|h3|h2|span)>"is}, {"regex", qr"\x{4f5c}\x{8005}\x{ff1a}(?:<span>)?<a[^>]*>([^<]+)</a>"is}, {"regex", qr"<p>\x{4f5c}(?:&nbsp;|\s)*\x{8005}\x{ff1a}([^<]+)</p>"is}, {"regex", qr/\x{4f5c}\x{8005}\x{ff1a}([^<]+?) \x{53d1}\x{5e03}\x{65f6}\x{95f4}\x{ff1a}/s}, {"regex", qr/content="([^"]+?)\x{6700}\x{65b0}\x{8457}\x{4f5c}/s}, {"regex", qr/<title>[^<,]+?\x{6700}\x{65b0}\x{7ae0}\x{8282}\(([^<,]+?)\),/is}, {"regex", qr/<title>[^<,]+?\x{4f5c}\x{8005}\x{ff1a}([^<,]+?)_/is}, {"regex", qr/content="[^"]+?,([^",]+?)\x{4f5c}\x{54c1}/s}], "sub", $self->can("tidy_writer_book"))
ABresult
1X1
011
000
210 $h,
211 [ { path => '//meta[@name="author"]', extract => '@content' },
212  
213 { path => '//meta[@name="og:novel:author"]', extract => '@content' },
214 { path => '//meta[@property="og:novel:author"]', extract => '@content' },
215 { path => '//*[@class="author"]', },
216 { path => '//*[@class="writer"]', },
217 { regex => qr#<span>作者:</span>([^<]+)#si, },
218 { regex => qr#作者:<span>([^<]+)</span>#si, },
219 { regex => qr#<(?:em|i|h3|h2|span)>作者:([^<]+)</(?:em|i|h3|h2|span)>#si, },
220 { regex => qr#作者:(?:<span>)?<a[^>]*>([^<]+)</a>#si, },
221 { regex => qr#<p>作(?:&nbsp;|\s)*者:([^<]+)</p>#si, },
222 { regex => qr#作者:([^<]+?) 发布时间:#s, },
223 { regex => qr#content="([^"]+?)最新著作#s, },
224 { regex => qr#<title>[^<,]+?最新章节\(([^<,]+?)\),#si, },
225 { regex => qr#<title>[^<,]+?作者:([^<,]+?)_#si, },
226 { regex => qr#content="[^"]+?,([^",]+?)作品#s, },
227 ],
228 sub => $self->can( "tidy_writer_book" ),
229 );
230  
231 0 $r->{$_} = $self->tidy_writer_book( $r->{$_} ) for qw/writer book title/;
232  
233 0 return $r;
234 } ## end sub parse_novel
235  
236 sub scrape_item_list {
237 0 ▶ my ( $self ) = @_;
Subroutine scrape_item_list: not called CC=2 SCAR=17.9
Pod undocumented
238 0 my $r = {};
239 0 ▶ $r->{path} = $self->{item_list_path} if ( exists $self->{item_list_path} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"item_list_path"} 0 0 0
240 0 return $r;
241 }
242  
243 sub parse_item_list {
244 1 ▶ my ( $self, $h, $r ) = @_;
Subroutine parse_item_list: called CC=5 SCAR=30.5
Pod undocumented
245  
246 1 ▶ return $r->{item_list} if ( exists $r->{item_list} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{"item_list"} 1 0 1
247  
248 0 my $path_r = $self->scrape_item_list();
249  
250 0 ▶ return $self->guess_item_list( $h ) unless ( exists $path_r->{path} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $path_r->{"path"} 0 0 0
251  
252 my $parse_novel = scraper {
253 process $path_r->{path},
254 0 ▶ 'item_list[]' => {
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 'title' => 'TEXT',
256 'url' => '@href'
257 };
258 0 };
259 0 my $ref = $parse_novel->scrape( $h );
260  
261 0 ▶ my @chap = grep { exists $_->{url} and $_->{url} } @{ $ref->{item_list} };
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{"url"} and $_->{"url"} 0 0 0
262  
263 0 ▶ if ( $path_r->{sort} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_r->{"sort"}) 0 0 0
264 0 @chap = sort { $a->{url} cmp $b->{url} } @chap;
265 }
266  
267 0 return \@chap;
268 } ## end sub parse_item_list
269  
270 sub guess_item_list {
271 0 ▶ my ( $self, $h, %opt ) = @_;
Subroutine guess_item_list: not called CC=41 SCAR=74.5
Pod undocumented
272  
273 0 my $new_h = $$h;
274 0 $new_h=~s#<dt>[^<]+最新\d+章节</dt>.+?<dt>#<dt>#s;
275  
276 0 my $tree = HTML::TreeBuilder->new();
277 0 $tree->parse( $new_h );
278  
279 0 my @links = $tree->look_down( '_tag', 'a' );
280 0 @links = grep { $_->attr( 'href' ) } @links;
281 0 for my $x ( @links ) {
282 0 my $up_url = $x->attr( 'href' );
283 0 $up_url =~ s#/[^/]+/?$#/#;
284 0 ▶ $up_url = '.' if ( $up_url !~ m#/# );
Branch: 1 branch
BranchTrueFalseTotal
unless $up_url =~ m[/] 0 0 0
285  
286 0 $x = { parent => $up_url, depth => $x->depth(), url => $x->attr( 'href' ), title => $x->as_text() };
287 }
288  
289 0 my @out_links;
290 0 my @temp_arr = ( $links[0] );
291 0 my $parent = $links[0]{parent};
292 0 my $depth = $links[0]{depth};
293 0 ▶ for ( my $i = 1 ; $i <= $#links ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#links) 0 0 0
294 0 ▶ if ( $depth == $links[$i]{depth} and $parent eq $links[$i]{parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == $links[$i]{'depth'} and $parent eq $links[$i]{'parent'}) { } 0 0 0
Condition0%0/3
$depth == $links[$i]{"depth"} and $parent eq $links[$i]{"parent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$depth == $links[$i]{"depth"} and $parent eq $links[$i]{"parent"}
$depth == $links[$i]{"depth"} $parent eq $links[$i]{"parent"}
Truth table0%0/3
A and B
A= $depth == $links[$i]{"depth"}B= $parent eq $links[$i]{"parent"}
ABresult
0X0
100
111
295 0 push @temp_arr, $links[$i];
296 } else {
297 0 push @out_links, [@temp_arr];
298 0 @temp_arr = ( $links[$i] );
299 0 $depth = $links[$i]{depth};
300 0 $parent = $links[$i]{parent};
301 }
302 }
303  
304 0 ▶ push @out_links, \@temp_arr if ( @temp_arr );
Branch: 1 branch
BranchTrueFalseTotal
if @temp_arr 0 0 0
305  
306 0 @out_links = sort { scalar( @$b ) <=> scalar( @$a ) } @out_links;
307  
308 0 my $res_arr;
309 0 my $title_regex =
310 qr/引子|楔子|内容简介|正文|序言|文案|第\s*[0123456789零○〇一二三四五六七八九十百千\d]+\s*(章|节)|(^[0-9]+)/;
311 0 my $chap_num_regex = qr/(^|\/)\d+(\.html)?$/;
312 0 for my $arr ( @out_links ) {
313 0 my $x = $arr->[0];
314 0 my $y = $arr->[1];
315 0 my $z = $arr->[-1];
316  
317 0 ▶ $res_arr = $arr if ( $opt{chapter_url_regex} and $x->{url} =~ /$opt{chapter_url_regex}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"chapter_url_regex"} and $x->{"url"} =~ /$opt{"chapter_url_regex"}/ 0 0 0
Condition0%0/3
$opt{"chapter_url_regex"} and $x->{"url"} =~ /$opt{"chapter_url_regex"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"chapter_url_regex"} and $x->{"url"} =~ /$opt{"chapter_url_regex"}/
$opt{"chapter_url_regex"} $x->{"url"} =~ /$opt{"chapter_url_regex"}/
Truth table0%0/3
A and B
A= $opt{"chapter_url_regex"}B= $x->{"url"} =~ /$opt{"chapter_url_regex"}/
ABresult
0X0
100
111
318 0 ▶ $res_arr = $arr if ( $opt{chapter_title_regex} and $x->{title} =~ /$opt{chapter_title_regex}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"chapter_title_regex"} and $x->{"title"} =~ /$opt{"chapter_title_regex"}/ 0 0 0
Condition0%0/3
$opt{"chapter_title_regex"} and $x->{"title"} =~ /$opt{"chapter_title_regex"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"chapter_title_regex"} and $x->{"title"} =~ /$opt{"chapter_title_regex"}/
$opt{"chapter_title_regex"} $x->{"title"} =~ /$opt{"chapter_title_regex"}/
Truth table0%0/3
A and B
A= $opt{"chapter_title_regex"}B= $x->{"title"} =~ /$opt{"chapter_title_regex"}/
ABresult
0X0
100
111
319 $res_arr = $arr
320 0 ▶ if ( ($x and $x->{title} =~ /$title_regex/) or ( $y and $y->{title} =~ /$title_regex/ ) or ( $z and $z->{title} =~ /$title_regex/ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $x and $x->{"title"} =~ /$title_regex/ or $y and $y->{"title"} =~ /$title_regex/ or $z and $z->{"title"} =~ /$title_regex/ 0 0 0
Condition0%0/15
$x and $x->{"title"} =~ /$title_regex/ or $y and $y->{"title"} =~ /$title_regex/ or $z and $z->{"title"} =~ /$title_regex/
l!l&&r!l&&!r
000
$x and $x->{"title"} =~ /$title_regex/ or $y and $y->{"title"} =~ /$title_regex/
l!l&&r!l&&!r
000
$x and $x->{"title"} =~ /$title_regex/
!ll&&!rl&&r
000
$y and $y->{"title"} =~ /$title_regex/
!ll&&!rl&&r
000
$z and $z->{"title"} =~ /$title_regex/
!ll&&!rl&&r
000
MC/DC0%0/6
$x and $x->{"title"} =~ /$title_regex/ or $y and $y->{"title"} =~ /$title_regex/ or $z and $z->{"title"} =~ /$title_regex/
$x $x->{"title"} =~ /$title_regex/ $y $y->{"title"} =~ /$title_regex/ $z $z->{"title"} =~ /$title_regex/
Truth table0%0/15
A and B or C and D or E and F
A= $xB= $x->{"title"} =~ /$title_regex/C= $yD= $y->{"title"} =~ /$title_regex/E= $zF= $z->{"title"} =~ /$title_regex/
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
321 0 ▶ $res_arr = $arr if ( ( $x->{url} =~ /$chap_num_regex/ or $z->{url} =~ /$chap_num_regex/ ) and scalar( @$arr ) > 50 );
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"url"} =~ /$chap_num_regex/ || $z->{"url"} =~ /$chap_num_regex/ and scalar @$arr > 50 0 0 0
Condition0%0/6
$x->{"url"} =~ /$chap_num_regex/ || $z->{"url"} =~ /$chap_num_regex/ and scalar @$arr > 50
!ll&&!rl&&r
000
$x->{"url"} =~ /$chap_num_regex/ or $z->{"url"} =~ /$chap_num_regex/
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->{"url"} =~ /$chap_num_regex/ || $z->{"url"} =~ /$chap_num_regex/ and scalar @$arr > 50
$x->{"url"} =~ /$chap_num_regex/ $z->{"url"} =~ /$chap_num_regex/ scalar @$arr > 50
Truth table0%0/5
A or B and C
A= $x->{"url"} =~ /$chap_num_regex/B= $z->{"url"} =~ /$chap_num_regex/C= scalar @$arr > 50
ABCresult
00X0
1X00
0100
1X11
0111
322  
323 #$res_arr= $arr if( ($x->{url}=~/\/?\d+$/ or $z->{url}=~/\/?\d+$/) and scalar(@$arr)>50);
324 0 ▶ last if ( $res_arr );
Branch: 1 branch
BranchTrueFalseTotal
if $res_arr 0 0 0
325 }
326  
327 #remove not chapter url
328 0 while ( 1 ) {
329 0 my $x = $res_arr->[0];
330 0 my $y = $res_arr->[ int( $#$res_arr / 2 ) ];
331 0 ▶ if ( defined $y->{title} and $y->{title} =~ /$title_regex/ and defined $y->{url} and $y->{url} =~ /\.html$/ and $x->{url} !~ /\.html$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $y->{'title'} and $y->{'title'} =~ /$title_regex/ and defined $y->{'url'} and $y->{'url'} =~ /\.html$/ and not $x->{'url'} =~ /\.html$/) { } 0 0 0
elsif (defined $y->{'title'} and $y->{'title'} =~ /$title_regex/ and defined $y->{'url'} and $y->{'url'} =~ /$chap_num_regex/ and not $x->{'url'} =~ /$chap_num_regex/) { } 0 0 0
Condition0%0/12
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"} and $y->{"url"} =~ /\.html$/ and not $x->{"url"} =~ /\.html$/
!ll&&!rl&&r
000
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"} and $y->{"url"} =~ /\.html$/
!ll&&!rl&&r
000
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"}
!ll&&!rl&&r
000
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/
!ll&&!rl&&r
000
MC/DC0%0/5
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"} and $y->{"url"} =~ /\.html$/ and not $x->{"url"} =~ /\.html$/
defined $y->{"title"} $y->{"title"} =~ /$title_regex/ defined $y->{"url"} $y->{"url"} =~ /\.html$/ not $x->{"url"} =~ /\.html$/
Truth table0%0/6
A and B and C and D and not(E)
A= defined $y->{"title"}B= $y->{"title"} =~ /$title_regex/C= defined $y->{"url"}D= $y->{"url"} =~ /\.html$/E= not $x->{"url"} =~ /\.html$/
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
332 0 shift( @$res_arr );
333 ▶ } elsif ( defined $y->{title} and $y->{title} =~ /$title_regex/ and defined $y->{url} and $y->{url} =~ /$chap_num_regex/ and $x->{url} !~ /$chap_num_regex/ ) {
Condition0%0/12
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"} and $y->{"url"} =~ /$chap_num_regex/ and not $x->{"url"} =~ /$chap_num_regex/
!ll&&!rl&&r
000
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"} and $y->{"url"} =~ /$chap_num_regex/
!ll&&!rl&&r
000
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"}
!ll&&!rl&&r
000
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/
!ll&&!rl&&r
000
MC/DC0%0/5
defined $y->{"title"} and $y->{"title"} =~ /$title_regex/ and defined $y->{"url"} and $y->{"url"} =~ /$chap_num_regex/ and not $x->{"url"} =~ /$chap_num_regex/
defined $y->{"title"} $y->{"title"} =~ /$title_regex/ defined $y->{"url"} $y->{"url"} =~ /$chap_num_regex/ not $x->{"url"} =~ /$chap_num_regex/
Truth table0%0/6
A and B and C and D and not(E)
A= defined $y->{"title"}B= $y->{"title"} =~ /$title_regex/C= defined $y->{"url"}D= $y->{"url"} =~ /$chap_num_regex/E= not $x->{"url"} =~ /$chap_num_regex/
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
334 0 shift( @$res_arr );
335 } else {
336 0 last;
337 }
338 }
339  
340 #sort chapter url
341 0 ▶ if ( $res_arr and defined $res_arr->[0]{url} and $res_arr->[0]{url} =~ /$chap_num_regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_arr and defined $res_arr->[0]{"url"} and $res_arr->[0]{"url"} =~ /$chap_num_regex/) 0 0 0
Condition0%0/6
$res_arr and defined $res_arr->[0]{"url"} and $res_arr->[0]{"url"} =~ /$chap_num_regex/
!ll&&!rl&&r
000
$res_arr and defined $res_arr->[0]{"url"}
!ll&&!rl&&r
000
MC/DC0%0/3
$res_arr and defined $res_arr->[0]{"url"} and $res_arr->[0]{"url"} =~ /$chap_num_regex/
$res_arr defined $res_arr->[0]{"url"} $res_arr->[0]{"url"} =~ /$chap_num_regex/
Truth table0%0/4
A and B and C
A= $res_arrB= defined $res_arr->[0]{"url"}C= $res_arr->[0]{"url"} =~ /$chap_num_regex/
ABCresult
0XX0
10X0
1100
1111
342 0 ▶ my $trim_sub = sub { my $s = $_[0]; $s =~ s/^.+\///; $s =~ s/\.html$//; return $s };
Subroutine __ANON__: not called CC=1 SCAR=6.9
343 0 my @sort_arr;
344 0 ▶ if($opt{sort_chapter_url}){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'sort_chapter_url'}) { } 0 0 0
345 0 @sort_arr = sort { $trim_sub->( $a->{url} ) <=> $trim_sub->( $b->{url} ) } grep { $_->{url} =~ /$chap_num_regex/ } @$res_arr;
346 }else{
347 0 @sort_arr = @$res_arr;
348 }
349 0 my @s = map { $trim_sub->( $_->{url} ) } @sort_arr;
350 0 my $random_sort = 0;
351 0 for my $i ( 0 .. $#s - 1 ) {
352 0 ▶ $random_sort = 1 if ( $s[$i] > $s[ $i + 1 ] );
Branch: 1 branch
BranchTrueFalseTotal
if $s[$i] > $s[$i + 1] 0 0 0
353 0 ▶ last if ( $random_sort );
Branch: 1 branch
BranchTrueFalseTotal
if $random_sort 0 0 0
354 }
355 0 ▶ return \@sort_arr if ( $random_sort == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $random_sort == 0 0 0 0
356 }
357  
358 0 ▶ return $res_arr || [];
Condition0%0/2
$res_arr || []
l!l
00
MC/DC0%0/1
$res_arr || []
$res_arr
Truth table0%0/2
A
A= $res_arr
Aresult
11
00
359 } ## end sub guess_item_list
360  
361 sub scrape_novel_item {
362 1 ▶ my ( $self ) = @_;
Subroutine scrape_novel_item: called CC=3 SCAR=11.8
Pod undocumented
363 1 my $r = {};
364 1 ▶ push @{$r->{content}}, { path => $self->{content_path}, extract => 'HTML' } if( exists $self->{content_path} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"content_path"} 0 1 1
365 1 ▶ push @{$r->{content}}, { regex => $self->{content_regex} } if( exists $self->{content_regex} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"content_regex"} 0 1 1
366 1 push @{$r->{content}}, (
367 { path => '//div[@class="novel_content"]' },
368 { path => '//div[@id="content"]' },
369 );
370 1 return $r;
371 }
372  
373 sub parse_novel_item {
374 0 ▶ my ( $self, $h, $r ) = @_;
Subroutine parse_novel_item: not called CC=5 SCAR=34.0
Pod undocumented
375  
376 0 ▶ $r = $self->guess_novel_item( $h ) unless ( $r->{content} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"content"} 0 0 0
377 0 ▶ $r->{$_} ||= $NULL_CHAPTER{$_} for keys( %NULL_CHAPTER );
Condition0%0/3
$r->{$_} ||= $Novel::Robot::Parser::NULL_CHAPTER{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{$_} ||= $Novel::Robot::Parser::NULL_CHAPTER{$_}
$r->{$_} $Novel::Robot::Parser::NULL_CHAPTER{$_}
Truth table0%0/3
A ||= B
A= $r->{$_}B= $Novel::Robot::Parser::NULL_CHAPTER{$_}
ABresult
1X1
011
000
378 0 $r->{content} = $self->tidy_content( $r->{content} );
379  
380 0 my $next_url = $self->scrape_element_try($h, [
381 { path => '//a[@id="next_url"]', extract =>'@href' },
382 { path => '//a[contains(text(),"下一页")]', extract => '@href' },
383 ]);
384 0 ▶ if($next_url){
Branch: 1 branch
BranchTrueFalseTotal
if ($next_url) 0 0 0
385 0 $r->{next_url} = $next_url;
386 }
387  
388 0 return $r;
389 }
390  
391 sub guess_novel_item {
392 0 ▶ my ( $self, $h, %opt ) = @_;
Subroutine guess_novel_item: not called CC=14 SCAR=53.5
Pod undocumented
393  
394 0 $$h =~ s#<!--.+?-->##sg;
395 0 $$h =~ s#<script[^>]*>[^<]*</script>##isg;
396  
397 0 my $tree = HTML::TreeBuilder->new();
398 0 $tree->parse( $$h );
399  
400 0 my @links = $tree->look_down( 'text', undef );
401 0 for my $x ( @links ) {
402 0 $x = { content => $x->as_HTML( '<>&' ) };
403 0 $self->calc_content_wordnum( $x );
404 }
405 0 my @out_links = sort { $b->{word_num} <=> $a->{word_num} } @links;
406  
407 0 my $no_next_r;
408 0 for my $r ( @out_links ) {
409 0 ▶ next if ( $r->{content} =~ m#</(style|head|body|html)>#s );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"content"} =~ m[</(style|head|body|html)>]s 0 0 0
410 0 ▶ next if ( $r->{content} =~ m#^\s*<div id="footer">#s );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"content"} =~ /^\s*<div id="footer">/s 0 0 0
411 0 ▶ next if ( $r->{content} =~ /(上|下)一(章|页|篇)/s );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"content"} =~ /(\x{4e0a}|\x{4e0b})\x{4e00}(\x{7ae0}|\x{9875}|\x{7bc7})/s 0 0 0
412 0 ▶ next if ( $r->{content} =~ m#</h(2|1)>#s );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"content"} =~ m[</h(2|1)>]s 0 0 0
413 0 ▶ next if ( $r->{content} =~ m#All rights reserved#s );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"content"} =~ /All rights reserved/s 0 0 0
414 0 ▶ next if ( $r->{content} =~ m#(.+?</a>){5,}#s );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"content"} =~ m[(.+?</a>){5,}]s 0 0 0
415  
416 0 $no_next_r = $r;
417 0 last;
418 }
419  
420 #my @grep_next_r = grep { $_->{content} =~ /(上|下)一(章|页|篇)\w{0,20}$/s and $_->{word_num} > 50 } @out_links;
421 0 my @grep_next_r = grep { $_->{content} =~ /(上|下)一(章|页|篇)/s
422 0 ▶ and $_->{word_num} > 50
Branch: 1 branch
BranchTrueFalseTotal
$_->{"content"} =~ /(\x{4e0a}|\x{4e0b})\x{4e00}(\x{7ae0}|\x{9875}|\x{7bc7})/s and $_->{"word_num"} > 50 0 0 0
423 } @out_links;
424  
425 0 my $cc = $no_next_r->{content};
426 0 my $cc_n = $cc =~ s/(\n|<p[^>]*>|<br[^>]*>)//sg;
427 0 ▶ return $no_next_r if ( ( $cc_n > 5 and $no_next_r->{word_num} > 50) or !@grep_next_r );
Branch: 1 branch
BranchTrueFalseTotal
if $cc_n > 5 and $no_next_r->{"word_num"} > 50 or not @grep_next_r 0 0 0
Condition0%0/6
$cc_n > 5 and $no_next_r->{"word_num"} > 50 or not @grep_next_r
l!l&&r!l&&!r
000
$cc_n > 5 and $no_next_r->{"word_num"} > 50
!ll&&!rl&&r
000
MC/DC0%0/3
$cc_n > 5 and $no_next_r->{"word_num"} > 50 or not @grep_next_r
$cc_n > 5 $no_next_r->{"word_num"} > 50 not @grep_next_r
Truth table0%0/5
A and B or not(C)
A= $cc_n > 5B= $no_next_r->{"word_num"} > 50C= not @grep_next_r
ABCresult
11X1
0X11
1011
0X00
1000
428  
429 0 ▶ return $grep_next_r[-1] || {};
Condition0%0/2
$grep_next_r[-1] || {}
l!l
00
MC/DC0%0/1
$grep_next_r[-1] || {}
$grep_next_r[-1]
Truth table0%0/2
A
A= $grep_next_r[-1]
Aresult
11
00
430 } ## end sub guess_novel_item
431  
432  
433 sub get_page_ref {
434 0 ▶ my ( $self, $url, %o ) = @_;
Subroutine get_page_ref: not called CC=2 SCAR=17.9
Pod undocumented
435 0 my $class = 'novel';
436  
437 0 my ( $c ) = $self->{browser}->request_url_simple($url);
438 0 ▶ my $r = $self->{site} ne 'default' ? $self->extract_item($c) : $self->guess_novel_item( \$c );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'site'} ne 'default' ? : 0 0 0
439 0 $r->{content} = $self->tidy_content( $r->{content} );
440  
441 my $page= {
442 writer => $o{writer},
443 book => $o{book},
444 0 url => $url,
445 item_list => [ $r ],
446 };
447  
448 0 return $page;
449 }
450  
451 sub get_tiezi_ref {
452 0 ▶ my ( $self, $url, %o ) = @_;
Subroutine get_tiezi_ref: not called CC=5 SCAR=34.0
Pod undocumented
453  
454 0 my $class = 'novel';
455 my ( $topic, $item_list ) = $self->{browser}->request_url_whole(
456 $url,
457  
458 #post_data => $o{post_data},
459 info_sub => sub {
460 0 ▶ $self->extract_elements(
Subroutine __ANON__: not called CC=1 SCAR=6.9
461 @_,
462 path => $self->can( "scrape_$class" )->( $self ),
463 sub => $self->can( "parse_$class" ),
464 );
465 },
466 0 ▶ item_list_sub => sub { $self->can( "parse_${class}_item" )->( $self, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
467 0 ▶ page_list_sub => sub { $self->can( "parse_${class}_list" )->( $self, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
468  
469 #min_page_num => $o{"min_page_num"},
470 #max_page_num => $o{"max_page_num"},
471 stop_sub => sub {
472 0 ▶ my ( $info, $data_list, $i ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
473 0 $self->{browser}->is_list_overflow( $data_list, $o{"max_item_num"} );
474 },
475 0 %o,
476 );
477  
478 0 $item_list = $self->update_item_list( $item_list, $url );
479  
480 0 ▶ unshift @$item_list, $topic if ( $topic->{content} );
Branch: 1 branch
BranchTrueFalseTotal
if $topic->{"content"} 0 0 0
481 my %r = (
482 %$topic,
483 writer => $o{writer} || $topic->{writer},
484 book => $o{book} || $topic->{book} || $topic->{title},
485 0 ▶ url => $url,
Condition0%0/9
$o{'writer'} || $topic->{'writer'}
l!l&&r!l&&!r
000
$o{'book'} || $topic->{'book'} || $topic->{'title'}
l!l&&r!l&&!r
000
$o{"book"} or $topic->{"book"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{'writer'} || $topic->{'writer'}
$o{'writer'} $topic->{'writer'}
MC/DC0%0/3
$o{'book'} || $topic->{'book'} || $topic->{'title'}
$o{"book"} $topic->{"book"} $topic->{'title'}
Truth table0%0/3
A || B
A= $o{'writer'}B= $topic->{'writer'}
ABresult
1X1
011
000
Truth table0%0/4
A or B || C
A= $o{"book"}B= $topic->{"book"}C= $topic->{'title'}
ABCresult
1XX1
01X1
0011
0000
486 item_list => $item_list,
487 );
488 0 $self->filter_item_list( \%r, %o );
489  
490 0 return \%r;
491 } ## end sub get_tiezi_ref
492  
493  
494 sub get_iterate_ref {
495 0 ▶ my ( $self, $url, %o ) = @_;
Subroutine get_iterate_ref: not called CC=1 SCAR=6.9
Pod undocumented
496 my ( $info, $item_list ) = $self->{browser}->request_url_whole(
497 $url,
498 post_data => $o{post_data},
499 info_sub => sub {
500 $self->extract_elements(
501 @_,
502 path => {},
503 0 ▶ sub => sub { my ( $self, $html_ref, $r ) = @_; return $r; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
504 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
505 },
506 0 ▶ item_sub => sub { my ( $self, $html_ref ) = @_; return {}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
507 0 ▶ item_list_sub => sub { my ( $self, $html_ref ) = @_; return []; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
508  
509 #min_page_num => $o{"min_page_num"},
510 #max_page_num => $o{"max_page_num"},
511 stop_sub => sub {
512 0 ▶ my ( $info, $data_list, $i ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
513 0 $self->{browser}->is_list_overflow( $data_list, $o{"max_item_num"} );
514 },
515 0 %o,
516 );
517  
518 0 $info->{item_list} = $self->update_item_list( $item_list, $url );
519  
520 0 return $info;
521 } ## end sub get_iterate_ref
522  
523  
524  
525 sub update_item_list {
526 5 ▶ my ( $self, $arr, $base_url ) = @_;
Subroutine update_item_list: called CC=15 SCAR=29.7
Pod undocumented
527  
528 5 my %rem;
529 5 for my $chap (@$arr){
530 22 ▶ $chap = { url => $chap || '' } if ( ref( $chap ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $chap ne "HASH" 0 22 22
Condition0%0/2
$chap || ''
l!l
00
MC/DC0%0/1
$chap || ''
$chap
Truth table0%0/2
A
A= $chap
Aresult
11
00
531 22 ▶ if ( $chap->{url} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chap->{"url"}) 10 12 22
532 10 $chap->{url} = $self->format_abs_url( $chap->{url}, $base_url );
533 10 $rem{ $chap->{url} }++;
534 }
535 }
536  
537 5 my $i = 0;
538 5 my @res;
539 5 for my $chap ( @$arr ) {
540 22 ▶ if($chap->{url} and $rem{ $chap->{url} }>1){
Branch: 1 branch
BranchTrueFalseTotal
if ($chap->{'url'} and $rem{$chap->{'url'}} > 1) { } 0 22 22
Condition66%2/3
$chap->{"url"} and $rem{$chap->{"url"}} > 1
!ll&&!rl&&r
12100
MC/DC0%0/2
$chap->{"url"} and $rem{$chap->{"url"}} > 1
$chap->{"url"} $rem{$chap->{"url"}} > 1
Truth table66%2/3
A and B
A= $chap->{"url"}B= $rem{$chap->{"url"}} > 1
ABresult
0X0
100
111
541 0 $rem{$chap->{url}}--;
542 }else{
543 22 ++$i;
544 22 ▶ $chap->{pid} //= $i; #page id
Condition66%2/3
$chap->{"pid"} //= $i
l!l&&r!l&&!r
6160
MC/DC0%0/2
$chap->{"pid"} //= $i
$chap->{"pid"} $i
Truth table66%2/3
A //= B
A= $chap->{"pid"}B= $i
ABresult
1X1
011
000
545 22 ▶ $chap->{id} //= $i; #item id
Condition66%2/3
$chap->{"id"} //= $i
l!l&&r!l&&!r
1660
MC/DC0%0/2
$chap->{"id"} //= $i
$chap->{"id"} $i
Truth table66%2/3
A //= B
A= $chap->{"id"}B= $i
ABresult
1X1
011
000
546 22 ▶ $chap->{content} //= '';
Condition100%2/2
$chap->{"content"} //= ""
l!l
1210
MC/DC100%1/1
$chap->{"content"} //= ""
$chap->{"content"}
Truth table100%2/2
A
A= $chap->{"content"}
Aresult
11
00
547 22 ▶ push @res, $chap unless($chap->{content}=~m#正在手打中#s);
Branch: 1 branch
BranchTrueFalseTotal
unless $chap->{"content"} =~ /\x{6b63}\x{5728}\x{624b}\x{6253}\x{4e2d}/s 22 0 22
548 }
549 }
550  
551 5 ▶ while(@res and $res[-1]{content}=~m#正在手打中#s ){
Branch: 1 branch
BranchTrueFalseTotal
if (@res and $res[-1]{"content"} =~ /\x{6b63}\x{5728}\x{624b}\x{6253}\x{4e2d}/s) 0 5 5
Condition33%1/3
@res and $res[-1]{"content"} =~ /\x{6b63}\x{5728}\x{624b}\x{6253}\x{4e2d}/s
!ll&&!rl&&r
050
MC/DC0%0/2
@res and $res[-1]{"content"} =~ /\x{6b63}\x{5728}\x{624b}\x{6253}\x{4e2d}/s
@res $res[-1]{"content"} =~ /\x{6b63}\x{5728}\x{624b}\x{6253}\x{4e2d}/s
Truth table33%1/3
A and B
A= @resB= $res[-1]{"content"} =~ /\x{6b63}\x{5728}\x{624b}\x{6253}\x{4e2d}/s
ABresult
0X0
100
111
552 0 pop @res;
553 }
554  
555 #$i = $arr->[-1]{id} if ( $#$arr >= 0 and exists $arr->[-1]{id} and $arr->[-1]{id} > $i );
556 5 ▶ return wantarray ? ( \@res, $i ) : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 2 5
557 } ## end sub update_item_list
558  
559 sub format_abs_url {
560 12 ▶ my ( $self, $url, $base_url ) = @_;
Subroutine format_abs_url: called CC=3 SCAR=11.0
Pod undocumented
561 12 ▶ return $url unless ( $base_url );
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url 0 12 12
562 12 ▶ return $url unless ( $base_url =~ /^https?:/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url =~ /^https?:/ 2 10 12
563 10 my $abs_url = URI->new_abs( $url, $base_url )->as_string;
564 }
565  
566 sub extract_elements {
567 2 ▶ my ( $self, $h, %o ) = @_;
Subroutine extract_elements: called CC=4 SCAR=14.0
Pod undocumented
568 2 ▶ $o{path} ||= {};
Condition50%1/2
$o{"path"} ||= {}
l!l
20
MC/DC0%0/1
$o{"path"} ||= {}
$o{"path"}
Truth table50%1/2
A
A= $o{"path"}
Aresult
11
00
569  
570 2 my $r = {};
571 2 ▶ while ( my ( $xk, $xr ) = each %{ $o{path} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$o{"path"};}) 1 2 3
572 1 $r->{$xk} = $self->scrape_element_try( $h, $xr );
573 }
574 2 ▶ $r = $o{sub}->( $self, $h, $r ) if ( $o{sub} );
Branch: 1 branch
BranchTrueFalseTotal
if $o{"sub"} 2 0 2
575 2 return $r;
576 }
577  
578 sub scrape_element_try {
579 1 ▶ my ( $self, $h, $r_list, %o ) = @_;
Subroutine scrape_element_try: called CC=5 SCAR=21.7
Pod undocumented
580 1 my $c;
581 1 for my $path_or_regex ( @$r_list ) {
582 2 $c = $self->scrape_element( $h, $path_or_regex );
583 2 ▶ next unless ( $c );
Branch: 1 branch
BranchTrueFalseTotal
unless $c 2 0 2
584 0 ▶ $c = $o{sub}->( $self, $c ) if ( exists $o{sub} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $o{"sub"} 0 0 0
585 0 ▶ next unless ( $c );
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 0 0
586 0 return $c;
587 }
588 1 return;
589 }
590  
591 sub scrape_element {
592 2 ▶ my ( $self, $h, $o ) = @_;
Subroutine scrape_element: called CC=7 SCAR=24.0
Pod undocumented
593 2 ▶ return $self->extract_regex_element( $h, $o->{regex} ) if ( $o->{regex} );
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"regex"} 0 2 2
594 2 ▶ return $o->{sub}->( $h ) unless ( $o->{path} );
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"path"} 0 2 2
595  
596 2 ▶ $o->{extract} ||= 'TEXT';
Condition50%1/2
$o->{"extract"} ||= "TEXT"
l!l
02
MC/DC0%0/1
$o->{"extract"} ||= "TEXT"
$o->{"extract"}
Truth table50%1/2
A
A= $o->{"extract"}
Aresult
11
00
597  
598 my $parse = $o->{is_list}
599 0 ▶ ? scraper { process $o->{path}, 'data[]' => $o->{extract}; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
600 2 ▶ : scraper { process_first $o->{path}, 'data' => $o->{extract}; };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$o->{'is_list'} ? : 0 2 2
601 2 my $r = $parse->scrape( $h );
602 2 ▶ return unless ( defined $r->{data} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"data"} 2 0 2
603  
604 0 ▶ return $r->{data} unless ( $o->{sub} );
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"sub"} 0 0 0
605 0 return $o->{sub}->( $r->{data} );
606 }
607  
608 sub extract_regex_element {
609 0 ▶ my ( $self, $h, $reg ) = @_;
Subroutine extract_regex_element: not called CC=1 SCAR=6.9
Pod undocumented
610 0 my ( $d ) = $$h =~ m#$reg#s;
611 0 return $d;
612 }
613  
614 sub filter_item_list {
615 2 ▶ my ( $self, $r, %o ) = @_;
Subroutine filter_item_list: called CC=9 SCAR=24.8
Pod undocumented
616  
617 2 my $flist = $r->{item_list};
618  
619 #$r->{item_num} //= $flist->[-1]{id} // scalar( @$flist );
620  
621 2 $flist->[$_]{content} = $self->tidy_content( $flist->[$_]{content} ) for ( 0 .. $#$flist );
622  
623 2 $flist = [ grep { $self->{browser}->is_item_in_range( $_->{id}, $o{min_item_num}, $o{max_item_num} ) } @$flist ];
624  
625 2 $self->calc_content_wordnum( $_ ) for @$flist;
626  
627 0 $flist = [ grep { $_->{word_num} >= $o{min_content_word_num} } @$flist ]
628 2 ▶ if ( $o{min_content_word_num} );
Branch: 1 branch
BranchTrueFalseTotal
if $o{"min_content_word_num"} 0 2 2
629  
630 0 $flist = [ grep { $_->{writer} eq $r->{writer} } @$flist ]
631 2 ▶ if ( $o{only_poster} );
Branch: 1 branch
BranchTrueFalseTotal
if $o{"only_poster"} 0 2 2
632  
633 0 $flist = [ grep { $_->{content} =~ /$o{grep_content}/s } @$flist ]
634 2 ▶ if ( $o{grep_content} );
Branch: 1 branch
BranchTrueFalseTotal
if $o{"grep_content"} 0 2 2
635  
636 0 $flist = [ grep { $_->{content} !~ /$o{filter_content}/s } @$flist ]
637 2 ▶ if ( $o{filter_content} );
Branch: 1 branch
BranchTrueFalseTotal
if $o{"filter_content"} 0 2 2
638  
639 2 ▶ $flist = [ grep { defined $_->{content} and $_->{content} =~ /\S/s } @$flist ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{"content"} and $_->{"content"} =~ /\S/s 6 0 6
640  
641 2 ▶ $r->{item_list} = $flist || [];
Condition50%1/2
$flist || []
l!l
20
MC/DC0%0/1
$flist || []
$flist
Truth table50%1/2
A
A= $flist
Aresult
11
00
642  
643 2 return $self;
644 } ## end sub filter_item_list
645  
646 sub calc_content_wordnum {
647 6 ▶ my ( $self, $f ) = @_;
Subroutine calc_content_wordnum: called CC=3 SCAR=11.2
Pod undocumented
648 6 ▶ return if ( $f->{word_num} );
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"word_num"} 0 6 6
649 6 ▶ my $wd = $f->{content} || '';
Condition50%1/2
$f->{'content'} || ''
l!l
60
MC/DC0%0/1
$f->{'content'} || ''
$f->{'content'}
Truth table50%1/2
A
A= $f->{'content'}
Aresult
11
00
650 6 $wd =~ s/<[^>]+>//gs;
651 6 $wd =~ s/\s+//sg;
652 6 $f->{word_num} = $wd =~ s/\S//gs;
653 6 return $f;
654 }
655  
656 sub tidy_writer_book {
657 0 ▶ my ( $self, $c ) = @_;
Subroutine tidy_writer_book: not called CC=3 SCAR=24.8
Pod undocumented
658 0 ▶ return unless ( defined $c );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
659 0 for ( $c ) {
660 0 s/作\s*者://;
661 0 s/^\s*作者-\s*//;
662 0 s/小说全集//;
663 0 s/作品全集//;
664 0 s/专栏//;
665 0 s/^.*版权属于作者([^,]+)$/$1/;
666 0 s/\s*最新章节\s*$//;
667 0 s/全文阅读//;
668 0 s/在线阅读//;
669 0 s/全集下载//;
670 0 s/章节目录//;
671 0 s/^\s*《(.*)》\s*$/$1/;
672 0 s/^\s+|\s+$//g;
673 0 s/\s+//g;
674 }
675 0 return $c;
676 } ## end sub tidy_writer_book
677  
678 sub tidy_content {
679 6 ▶ my ( $self, $c ) = @_;
Subroutine tidy_content: called CC=3 SCAR=11.0
Pod undocumented
680 6 for ( $c ) {
681 6 ▶ last unless ( $c );
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 6 6
682 6 s# ##sg;
683 6 s# #\n#sg;
684 6 s#\s{5,}#\n#sg;
685 6 s#<script(\s+[^>]+\>|\>)[^<]*</script>##sg;
686 6 s#\s*\<[^>]+?\>#\n#sg;
687 6 s{\n+}{\n}sg;
688 6 s{\s*(\S.*?)\s*\n}{\n<p>$1</p>}sg;
689 6 s#\s+上一章\s+.+?下一章.+$##s;
690 6 s#[^\n]+加入书签[^\n]+##s;
691 }
692 6 return $c;
693 }
694  
695 sub tidy_string {
696 4 ▶ my ( $self, $c ) = @_;
Subroutine tidy_string: called CC=3 SCAR=11.0
Pod undocumented
697 4 ▶ $c ||= '';
Condition50%1/2
$c ||= ""
l!l
40
MC/DC0%0/1
$c ||= ""
$c
Truth table50%1/2
A
A= $c
Aresult
11
00
698 4 for ( $c ) {
699 4 s/^\s+|\s+$//gs;
700 4 s/[\*\/\\\[\(\)]+//g;
701 4 s/[[:punct:]]//sg;
702 4 s/[\]\s+\/\\]/-/g;
703 }
704  
705 4 return $c;
706 }
707  
708 sub get_inner_html {
709 0 ▶ my ( $self, $h ) = @_;
Subroutine get_inner_html: not called CC=2 SCAR=17.9
Pod undocumented
710  
711 0 ▶ return '' unless ( $h );
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
712  
713 0 my $head_i = index( $h, '>' );
714 0 substr( $h, 0, $head_i + 1 ) = '';
715  
716 0 my $tail_i = rindex( $h, '<' );
717 0 substr( $h, $tail_i ) = '';
718  
719 0 return $h;
720 }
721  
722 sub unescape_js {
723 0 ▶ my ( $self, $s ) = @_;
Subroutine unescape_js: not called CC=1 SCAR=6.9
Pod undocumented
724 0 $s =~ s/%u([0-9a-f]{4})/chr(hex($1))/eigs;
725 0 $s =~ s/%([0-9a-f]{2})/chr(hex($1))/eigs;
726 0 return $s;
727 }
728  
729 sub encode_cjk_for_url {
730 0 ▶ my ( $self, $key ) = @_;
Subroutine encode_cjk_for_url: not called CC=1 SCAR=6.9
Pod undocumented
731 0 my $b = uc( unpack( "H*", encode( $self->charset(), $key ) ) );
732 0 $b =~ s/(..)/%$1/g;
733 0 return $b;
734 }
735  
736 sub get_query_ref {
737 0 ▶ my ( $self, $keyword, %o ) = @_;
Subroutine get_query_ref: not called CC=1 SCAR=6.9
Pod undocumented
738 0 my ($url, $post_data) = $self->make_query_request($keyword, %o);
739 my $r = $self->get_iterate_ref($url,
740 post_data => $post_data,
741 0 ▶ info_sub => sub { return { title => "query: $keyword"} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
742 0 ▶ page_list_sub => sub { $self->can( "parse_query_list" )->( $self, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
743 0 ▶ item_list_sub => sub { $self->can( "parse_query_item" )->( $self, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
744 0 %o,
745 );
746 0 return $r;
747 }
748  
749 sub get_board_ref {
750 0 ▶ my ( $self, $board_url, %o ) = @_;
Subroutine get_board_ref: not called CC=1 SCAR=6.9
Pod undocumented
751 my $r = $self->get_iterate_ref(
752 $board_url,
753 0 ▶ info_sub => sub { $self->extract_elements( @_, sub => $self->can( "parse_board" ),); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
754 0 ▶ item_list_sub => sub { $self->can( "parse_board_item" )->( $self, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
755 0 %o,
756 );
757 0 return $r;
758 }
759  
760 3 1;
761  
« lib/Novel/Robot/Browser.pm ↑ Summary lib/Novel/Robot.pm »