blib/lib/Web/PageMeta.pm

filter: Statementstmt 94.0% 174 / 185 Branchbran 71.0% 27 / 38 Conditioncond 64.7% 11 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 91.6% 33 / 36 Podpod 0.0% 0 / 1 totaltotal 87.0% 249 / 286 CC 30 SCAR 14.9
CC
30
Cov
88%
_build__fetch_page_meta_ft
13.1
__ANON__
6.0
compile_headers
4.4
CRAP 4.4
↑ Summary
1 package Web::PageMeta;
2  
3 3 our $VERSION = '0.09';
4  
5 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use MooseX::Types::URI qw(Uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Future::HTTP::AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Web::Scraper::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Encode qw(find_mime_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use HTTP::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 has 'url' => (
24 isa => Uri,
25 is => 'ro',
26 required => 1,
27 coerce => 1,
28 );
29  
30 3 has 'timeout' => (
31 isa => 'Int',
32 is => 'rw',
33 required => 1,
34 default => 5 * 60,
35 );
36  
37 3 has 'max_size' => (
38 isa => 'Int',
39 is => 'rw',
40 required => 1,
41 default => 100 * 1024 * 1024,
42 );
43  
44 3 has 'user_agent' => (
45 isa => 'Str',
46 is => 'ro',
47 required => 1,
48 default =>
49 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
50 lazy => 1,
51 );
52  
53 has 'extra_headers' => (
54 isa => 'HashRef',
55 is => 'ro',
56 required => 1,
57 18 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
58 3 lazy => 1,
59 );
60  
61 3 has 'cookie_jar' => (
62 isa => 'Object',
63 is => 'ro',
64 predicate => 'has_cookie_jar',
65 );
66  
67 has 'title' => (
68 isa => 'Str',
69 is => 'ro',
70 lazy => 1,
71 7 ▶ default => sub {return $_[0]->page_meta->{title} // ''},
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->page_meta->{"title"} // ""
l!l
30
MC/DC0%0/1
$_[0]->page_meta->{"title"} // ""
$_[0]->page_meta->{"title"}
Truth table50%1/2
A
A= $_[0]->page_meta->{"title"}
Aresult
11
00
72 3 );
73 has 'image' => (
74 isa => 'Str',
75 is => 'ro',
76 lazy => 1,
77 12 ▶ default => sub {return $_[0]->page_meta->{image} // ''},
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->page_meta->{"image"} // ""
l!l
110
MC/DC0%0/1
$_[0]->page_meta->{"image"} // ""
$_[0]->page_meta->{"image"}
Truth table50%1/2
A
A= $_[0]->page_meta->{"image"}
Aresult
11
00
78 3 );
79 has 'description' => (
80 isa => 'Str',
81 is => 'ro',
82 lazy => 1,
83 2 ▶ default => sub {return $_[0]->page_meta->{description} // ''},
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->page_meta->{"description"} // ""
l!l
20
MC/DC0%0/1
$_[0]->page_meta->{"description"} // ""
$_[0]->page_meta->{"description"}
Truth table50%1/2
A
A= $_[0]->page_meta->{"description"}
Aresult
11
00
84 3 );
85  
86 has 'canonical_url' => (
87 isa => Uri,
88 is => 'ro',
89 lazy => 1,
90 0 ▶ default => sub {return $_[0]->page_meta->{url} // $_[0]->url},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$_[0]->page_meta->{"url"} // $_[0]->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]->page_meta->{"url"} // $_[0]->url
$_[0]->page_meta->{"url"} $_[0]->url
Truth table0%0/3
A // B
A= $_[0]->page_meta->{"url"}B= $_[0]->url
ABresult
1X1
011
000
91 3 );
92  
93 has 'image_data' => (
94 isa => 'Str',
95 is => 'ro',
96 lazy => 1,
97 2 ▶ default => sub {$_[0]->fetch_image_data_ft->get},
Subroutine __ANON__: called CC=1 SCAR=0.0
98 3 );
99  
100 has 'page_meta' => (
101 isa => 'HashRef',
102 is => 'rw',
103 lazy => 1,
104 10 ▶ default => sub {$_[0]->fetch_page_meta_ft->get},
Subroutine __ANON__: called CC=1 SCAR=0.0
105 3 );
106  
107 has 'page_body_hdr' => (
108 isa => 'ArrayRef',
109 is => 'ro',
110 lazy => 1,
111 0 ▶ default => sub {$_[0]->fetch_page_body_hdr_ft->get},
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 3 );
113  
114 3 has 'fetch_page_body_hdr_ft' => (
115 isa => 'Future',
116 is => 'ro',
117 lazy => 1,
118 builder => '_build__fetch_page_body_hdr_ft',
119 );
120  
121 3 has 'fetch_page_meta_ft' => (
122 isa => 'Future',
123 is => 'ro',
124 lazy => 1,
125 builder => '_build__fetch_page_meta_ft',
126 );
127  
128 3 has 'fetch_image_data_ft' => (
129 isa => 'Future',
130 is => 'ro',
131 lazy => 1,
132 builder => '_build__fetch_image_data_ft',
133 );
134  
135 has '_ua' => (
136 is => 'ro',
137 lazy => 1,
138 2 ▶ default => sub {Future::HTTP::AnyEvent->new()},
Subroutine __ANON__: called CC=1 SCAR=0.0
139 3 );
140  
141 3 has '_html_meta_scraper' => (
142 is => 'ro',
143 lazy => 1,
144 builder => '_build__html_meta_scraper',
145 );
146  
147 3 has 'extra_scraper' => (
148 is => 'ro',
149 predicate => 'has_extra_scraper',
150 );
151  
152 11 ▶sub _build__html_meta_scraper {
Subroutine _build__html_meta_scraper: called CC=1 SCAR=0.0
153 11 ▶ state $html_meta_scraper = scraper {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 11 process '/html/head/meta[contains(@property, "og:")]',
155 'head_meta_og[]' => {
156 key => '@property',
157 val => '@content',
158 };
159 11 process '/html/head/title', 'title' => 'TEXT';
160 11 process '/html/head/meta[@name="description"]', 'description' => '@content';
161 11 process '/html/head/base', 'base_href' => '@href';
162 11 };
163 11 return $html_meta_scraper;
164 }
165  
166 26 ▶sub compile_headers {
Subroutine compile_headers: called CC=3 SCAR=14.7
Pod undocumented
167 26 my ($self) = @_;
168  
169 26 my %headers = (
170 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
171 'User-Agent' => $self->user_agent,
172 );
173 26 ▶ if ($self->has_cookie_jar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_cookie_jar) 0 26 26
174 0 my $cookies = $self->cookie_jar->get_cookies($self->url);
175 0 ▶ if (%$cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$cookies) 0 0 0
176 0 ▶ $headers{'Cookie'} = join("; ", pairmap {$a . "=" . $b} %$cookies);
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 $headers{'Cookie2'} = '$Version="1"';
178 }
179 }
180 26 %headers = (%headers, %{$self->extra_headers});
181 26 return \%headers;
182 }
183  
184 18 ▶async sub _build__fetch_page_body_hdr_ft {
Subroutine _build__fetch_page_body_hdr_ft: called CC=2 SCAR=6.9
185 18 my ($self) = @_;
186  
187 # await url htmp http download
188 18 my $timer = time();
189 18 my $timeout = $self->timeout;
190 18 my $timeout_at = $timer + $timeout;
191 18 my $max_size = $self->max_size;
192 18 my $cur_size = 0;
193 18 my $body = '';
194 my ( undef, $headers ) = await $self->_ua->http_get(
195 $self->url,
196 headers => $self->compile_headers,
197 timeout => $timeout,
198 12 ▶ on_header => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
199 12 my ($hdrs) = @_;
200 12 ▶ my $content_type = $hdrs->{"content-type"} || '';
Condition100%2/2
$hdrs->{'content-type'} || ''
l!l
111
MC/DC100%1/1
$hdrs->{'content-type'} || ''
$hdrs->{'content-type'}
Truth table100%2/2
A
A= $hdrs->{'content-type'}
Aresult
11
00
201 12 ▶ if ($content_type =~ /^text\/html\s*(?:;|$)/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type =~ m[^text/html\s*(?:;|$)]i) { } 9 3 12
elsif ($hdrs->{'Status'} != 200) { } 2 1 3
202 9 return 1;
203 }
204 elsif ($hdrs->{Status} != 200) {
205 # non-200 status handled below with exception
206 2 return 1;
207 }
208 else {
209 $log->warnf( 'unsupported content-type "%s", status "%d", fetching %s',
210 1 $content_type, $hdrs->{Status}, $self->url );
211 1 return 0;
212 }
213 },
214 17 ▶ on_body => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
215 17 my ( $part_body, undef ) = @_;
216  
217 17 $body .= $part_body;
218  
219 17 ▶ if ( time() > $timeout_at ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Web::PageMeta::time() > $timeout_at) 1 16 17
220 1 $log->warnf( 'timeout %ds fetching %s',
221 $timeout, $self->url );
222 1 return 0;
223 }
224  
225 16 $cur_size += length($part_body);
226 16 ▶ if ($cur_size > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_size > $max_size) 1 15 16
227 1 $log->warnf( 'max size %d exceeded with %d fetching %s',
228 $max_size, $cur_size, $self->url );
229 1 return 0;
230 }
231  
232 15 return 1;
233 },
234 18 );
235 13 my $status = _get_update_status_reason($headers);
236 13 $log->debugf('page meta fetch %d %s finished in %.3fs', $status, $self->url, time() - $timer);
237 HTTP::Exception->throw($status, status_message => $headers->{Reason})
238 13 ▶ if ($status != 200);
Branch: 1 branch
BranchTrueFalseTotal
if $status != 200 2 11 13
239  
240 11 return [$body, $headers];
241 3 }
242  
243 18 ▶async sub _build__fetch_page_meta_ft {
Subroutine _build__fetch_page_meta_ft: called CC=13 SCAR=25.7
244 18 my ($self) = @_;
245  
246 18 my ( $body, $headers ) = @{await $self->fetch_page_body_hdr_ft};
247  
248 # turn body to utf-8
249 11 ▶ if (my $content_type = $headers->{'content-type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content_type = $headers->{"content-type"}) 5 6 11
250 5 ▶ if (my ($charset) = ($content_type =~ m/\bcharset=(.+)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($charset) = $content_type =~ /\bcharset=(.+)/) 5 0 5
251 5 ▶ if (my $decoder = find_mime_encoding($charset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $decoder = &find_mime_encoding($charset)) 5 0 5
252 5 $body = $decoder->decode($body);
253 }
254 }
255 }
256  
257 # scrape default head meta
258 11 my $scraper_data = $self->_html_meta_scraper->scrape(\$body);
259 my %page_meta = (
260 title => $scraper_data->{title} // '',
261 11 ▶ description => $scraper_data->{description} // '',
Condition100%4/4
$scraper_data->{"title"} // ""
l!l
38
$scraper_data->{"description"} // ""
l!l
38
MC/DC100%1/1
$scraper_data->{"title"} // ""
$scraper_data->{"title"}
MC/DC100%1/1
$scraper_data->{"description"} // ""
$scraper_data->{"description"}
Truth table100%2/2
A
A= $scraper_data->{"title"}
Aresult
11
00
Truth table100%2/2
A
A= $scraper_data->{"description"}
Aresult
11
00
262 );
263 11 ▶ foreach my $attr (@{$scraper_data->{head_meta_og} // []}) {
Condition100%2/2
$scraper_data->{"head_meta_og"} // []
l!l
101
MC/DC100%1/1
$scraper_data->{"head_meta_og"} // []
$scraper_data->{"head_meta_og"}
Truth table100%2/2
A
A= $scraper_data->{"head_meta_og"}
Aresult
11
00
264 19 my $key = $attr->{key};
265 19 my $val = $attr->{val};
266 19 ▶ next unless $key =~ m/^og:(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^og:(.+)$/ 0 19 19
267 19 $page_meta{$1} = $val;
268 }
269  
270 # do any other extra scraping
271 11 ▶ if ($self->has_extra_scraper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_extra_scraper) 1 10 11
272 1 my $escraper_data = $self->extra_scraper->scrape(\$body);
273 1 foreach my $key (keys %{$escraper_data}) {
274 1 $page_meta{$key} = $escraper_data->{$key};
275 }
276 }
277  
278 # make image links absolute
279 11 ▶ if ($page_meta{image}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_meta{"image"}) 11 0 11
280 my $base_url = (
281 $scraper_data->{base_href}
282 11 ▶ ? URI::WithBase->new($scraper_data->{base_href}, $self->url)->abs->as_string
Branch: 1 branch
BranchTrueFalseTotal
$scraper_data->{'base_href'} ? : 2 9 11
283 : $self->url
284 );
285 11 $page_meta{image} = URI::WithBase->new($page_meta{image}, $base_url)->abs->as_string;
286 }
287  
288 11 return $self->page_meta(\%page_meta);
289 3 }
290  
291 8 ▶async sub _build__fetch_image_data_ft {
Subroutine _build__fetch_image_data_ft: called CC=3 SCAR=11.4
292 8 my ($self) = @_;
293  
294 # await for image link
295 8 await $self->fetch_page_meta_ft;
296 8 my $fetch_url = $self->image;
297 8 ▶ HTTP::Exception->throw(404, status_message => 'No image found')
Branch: 1 branch
BranchTrueFalseTotal
unless $fetch_url 0 8 8
298 unless $fetch_url;
299  
300 # await image http download
301 8 my $timer = time();
302 8 my $timeout = $self->timeout;
303 8 my $timeout_at = $timer + $timeout;
304 8 my $max_size = $self->max_size;
305 8 my $cur_size = 0;
306 8 my $body = '';
307 my ( undef, $headers ) = await $self->_ua->http_get(
308 $fetch_url,
309 headers => $self->compile_headers,
310 8 ▶ on_body => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
311 8 my ( $part_body, undef ) = @_;
312  
313 8 $body .= $part_body;
314  
315 8 ▶ if ( time() > $timeout_at ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Web::PageMeta::time() > $timeout_at) 0 8 8
316 0 $log->warnf( 'timeout %ds fetching %s',
317 $timeout, $self->url );
318 0 return 0;
319 }
320  
321 8 $cur_size += length($part_body);
322 8 ▶ if ( $cur_size > $max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_size > $max_size) 0 8 8
323 0 $log->warnf( 'max size %d exceeded with %d fetching %s',
324 $max_size, $cur_size, $self->url );
325 0 return 0;
326 }
327  
328 8 return 1;
329 },
330 8 );
331 8 my $status = _get_update_status_reason($headers);
332 8 $log->debugf('img fetch %d %s for %s finished in %.3fs',
333 $status, $fetch_url, $self->url, time() - $timer);
334 HTTP::Exception->throw($status, status_message => $headers->{Reason})
335 8 ▶ if ($status != 200);
Branch: 1 branch
BranchTrueFalseTotal
if $status != 200 0 8 8
336  
337 8 return $self->{image_data} = $body;
338 3 }
339  
340 21 ▶sub _get_update_status_reason {
Subroutine _get_update_status_reason: called CC=2 SCAR=6.9
341 21 my ($headers) = @_;
342 21 my $status = $headers->{Status};
343 21 ▶ unless (HTTP::Status::status_message($status)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::status_message($status)) 1 20 21
344 1 $headers->{Reason} = sprintf('(%d) %s', $status, $headers->{Reason});
345 1 $status = $headers->{Status} = 503;
346 }
347 21 return $status;
348 }
349  
350 3 __PACKAGE__->meta->make_immutable;
351  
352 3 1;
353  
354 __END__
↑ Summary