blib/lib/NewsExtractor.pm

filter: Statementstmt 68.8% 31 / 45 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 1 totaltotal 63.4% 40 / 63 CC 3 SCAR 12.2
CC
3
Cov
63%
download
12.0
__ANON__
2.0
__ANON__
2.0
CRAP 3.4
« blib/lib/NewsExtractor/SiteSpecificExtractor/talk_ltn_com_tw.pm ↑ Summary blib/lib/NewsExtractor/SiteSpecificExtractor/ctee_com_tw.pm »
1 package NewsExtractor;
2 1 our $VERSION = v0.43.0;
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::UserAgent::Transactor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Types::Standard qw< Str >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Types::URI qw< Uri >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Importer 'NewsExtractor::TextUtil' => qw(u);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use NewsExtractor::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use NewsExtractor::Download;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has url => ( required => 1, is => 'ro', isa => Uri, coerce => 1 );
17  
18 has user_agent_string => (
19 required => 1,
20 is => 'ro',
21 isa => Str,
22 default => sub {
23 0 ▶ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:75.0) Gecko/20100101 Firefox/75.0'
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 }
25 1 );
26  
27 sub download {
28 0 ▶ my NewsExtractor $self = shift;
Subroutine download: not called CC=3 SCAR=24.8
Pod undocumented
29  
30 0 my $ua = Mojo::UserAgent->new()->transactor(
31 Mojo::UserAgent::Transactor->new()->name( $self->user_agent_string )
32 )->max_redirects(3);
33  
34 0 my ($error, $download);
35  
36 0 my $tx = $ua->get( "". $self->url );
37  
38 0 my $res;
39 try {
40 0 ▶ $res = $tx->result
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 } catch {
42 0 ▶ $error = NewsExtractor::Error->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 is_exception => 0,
44 message => u($_),
45 )
46 0 };
47  
48 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
49 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) { } 0 0 0
50 0 $error = NewsExtractor::Error->new(
51 is_exception => 0,
52 message => u($res->message),
53 );
54 } else {
55 0 $download = NewsExtractor::Download->new( tx => $tx );
56 }
57 }
58 0 return ($error, $download);
59 }
60  
61 1 1;
62  
63 __END__
64  
65 =head1 NAME
66  
67 NewsExtractor - download and extract news articles from Internet.
68  
69 =head1 SYNOPSIS
70  
71 my ($error, $article) = NewsExtractor->new( url => $url )->download->parse;
72 die $error if $error;
73  
74 # $article is an instance of NewsExtractor::Article
75 say "Headline: " . $article->headline;
76 say "When: " . ($article->dateline // "(unknown)");
77 say "By: " . ($article->journalist // "(unknown)");
78 say "\n" . $article->article_body;
79  
80 =head1 SEE Also
81  
82 L<NewsExtractor::Article>
83  
84 =head1 AUTHOR
85  
86 Kang-min Liu <gugod@gugod.org>
87  
88 =head1 LICENSE
89  
90 To the extent possible under law, Kang-min Liu has waived all copyright and related or neighboring rights to NewsExtractor. This work is published from: Taiwan.
91  
92 https://creativecommons.org/publicdomain/zero/1.0/
93  
94 =cut
« blib/lib/NewsExtractor/SiteSpecificExtractor/talk_ltn_com_tw.pm ↑ Summary blib/lib/NewsExtractor/SiteSpecificExtractor/ctee_com_tw.pm »