blib/lib/Sweat/Article.pm

filter: Statementstmt 41.0% 41 / 100 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 4 totaltotal 27.5% 51 / 185 CC 21 SCAR 31.9
CC
21
Cov
28%
_get_random_title_linked_from_title
56.0
_get_summary_for_title
42.0
new_from_newsapi_article
30.0
CRAP 24.4
↑ Summary blib/lib/Sweat.pm »
1 package Sweat::Article;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use utf8::all;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Types::Standard qw( Str Maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTML::Strip;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use MediaWiki::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 has 'text' => (
17 is => 'ro',
18 required => 1,
19 isa => Str,
20 );
21  
22 3 has 'title' => (
23 is => 'ro',
24 required => 1,
25 isa => Str,
26 );
27  
28 3 has 'url' => (
29 is => 'ro',
30 required => 1,
31 );
32  
33 3 our $stripper = HTML::Strip->new;
34 3 our $mw = MediaWiki::API->new;
35  
36 3 $mw->{config}->{max_lag} = 5;
37 3 $mw->{config}->{max_lag_delay} = 1;
38 3 $mw->{ua}->timeout( 10 );
39 3 our $language;
40  
41 3 our %seen_titles;
42  
43 sub new_from_newsapi_article {
44 0 ▶ my ( $class, $newsapi_article ) = @_;
Subroutine new_from_newsapi_article: not called CC=5 SCAR=34.0
Pod undocumented
45  
46 0 ▶ die "Expected a NewsAPI article, got $newsapi_article"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($newsapi_article) and $newsapi_article->isa("Web::NewsAPI::Article") 0 0 0
Condition0%0/3
builtin::blessed($newsapi_article) and $newsapi_article->isa("Web::NewsAPI::Article")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($newsapi_article) and $newsapi_article->isa("Web::NewsAPI::Article")
builtin::blessed($newsapi_article) $newsapi_article->isa("Web::NewsAPI::Article")
Truth table0%0/3
A and B
A= builtin::blessed($newsapi_article)B= $newsapi_article->isa("Web::NewsAPI::Article")
ABresult
0X0
100
111
47 unless blessed($newsapi_article)
48 && $newsapi_article->isa( 'Web::NewsAPI::Article' );
49  
50 0 ▶ my $sweat_article = $class->new(
Condition0%0/4
$newsapi_article->title // ""
l!l
00
$newsapi_article->description // ""
l!l
00
MC/DC0%0/1
$newsapi_article->title // ""
$newsapi_article->title
MC/DC0%0/1
$newsapi_article->description // ""
$newsapi_article->description
Truth table0%0/2
A
A= $newsapi_article->title
Aresult
11
00
Truth table0%0/2
A
A= $newsapi_article->description
Aresult
11
00
51 text => ($newsapi_article->title // q{})
52 . q{. }
53 . ($newsapi_article->description // q{}),
54 url => $newsapi_article->url,
55 title => $newsapi_article->title,
56 );
57  
58 0 return $sweat_article;
59 }
60  
61 sub new_from_random_wikipedia_article {
62 0 ▶ my ($class) = @_;
Subroutine new_from_random_wikipedia_article: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 0 my $title = _get_random_title();
65 0 return $class->new_from_wikipedia_title($title);
66 }
67  
68 sub new_from_linked_wikipedia_article {
69 0 ▶ my ($class, $article) = @_;
Subroutine new_from_linked_wikipedia_article: not called CC=1 SCAR=6.9
Pod undocumented
70  
71 0 my $title = _get_random_title_linked_from_title($article->title);
72 0 return $class->new_from_wikipedia_title($title);
73 }
74  
75 sub new_from_wikipedia_title {
76 0 ▶ my ($class, $title) = @_;
Subroutine new_from_wikipedia_title: not called CC=4 SCAR=30.0
Pod undocumented
77  
78 0 my $summary = _get_summary_for_title($title);
79 0 my $tries = 0;
80 0 ▶ until ($summary || ($tries >= 3) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($summary or $tries >= 3) 0 0 0
Condition0%0/3
$summary or $tries >= 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$summary or $tries >= 3
$summary $tries >= 3
Truth table0%0/3
A or B
A= $summaryB= $tries >= 3
ABresult
1X1
011
000
81 0 $tries++;
82 0 $title = _get_random_title_linked_from_title($title);
83 0 $summary = _get_summary_for_title($title);
84 }
85 0 ▶ unless ( $summary ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($summary) 0 0 0
86 0 $title = _get_random_title();
87 0 $summary = _get_summary_for_title($title);
88 }
89  
90 0 return $class->new(
91 title => $title,
92 text => $summary,
93 url => "https://$language.wikipedia.org/wiki/$title",
94 );
95 }
96  
97 sub _get_random_title {
98 0 ▶ my $result = $mw->api( {
Subroutine _get_random_title: not called CC=1 SCAR=6.9
99 list => 'random',
100 action => 'query',
101 rnnamespace => 0,
102 } );
103  
104 0 return $result->{query}->{random}->[0]->{title};
105 }
106  
107 sub _get_summary_for_title {
108 0 ▶ my ($title) = @_;
Subroutine _get_summary_for_title: not called CC=6 SCAR=37.4
109  
110 0 my $result = $mw->api( {
111 action => 'query',
112 prop => 'extracts',
113 exintro => undef,
114 titles => $title,
115 } );
116  
117 0 my $summary = (values(%{$result->{query}->{pages}}))[0]->{extract};
118  
119 0 ▶ if (defined $summary) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $summary) 0 0 0
120 0 $summary = $stripper->parse( $summary );
121 # Eliminate all parentheticals (birth/death dates, alternate-language
122 # representations, and so on) because they don't read out loud well.
123 0 my $found_some;
124 0 ▶ until ( defined($found_some) && not($found_some) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $found_some and not $found_some) 0 0 0
Condition0%0/3
defined $found_some and not $found_some
!ll&&!rl&&r
000
MC/DC0%0/2
defined $found_some and not $found_some
defined $found_some not $found_some
Truth table0%0/3
A and not(B)
A= defined $found_someB= not $found_some
ABresult
0X0
100
111
125 0 $found_some = $summary =~ s/\([^\(]*?\)//g;
126 }
127 # Clean up redundant whitespace, and whitespace before punctuation.
128 0 $summary =~ s/\s+([,.!?;:])/$1/g;
129 0 $summary =~ s/ {2,}/ /g;
130 }
131 0 ▶ if ( $summary && $summary =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($summary and $summary =~ /\S/u) { } 0 0 0
Condition0%0/3
$summary and $summary =~ /\S/u
!ll&&!rl&&r
000
MC/DC0%0/2
$summary and $summary =~ /\S/u
$summary $summary =~ /\S/u
Truth table0%0/3
A and B
A= $summaryB= $summary =~ /\S/u
ABresult
0X0
100
111
132 0 return $summary;
133 }
134 else {
135 0 return undef;
136 }
137 }
138  
139 sub _erase_parentheticals {
140 0 ▶ my ( $summary, $opener, $closer ) = @_;
Subroutine _erase_parentheticals: not called CC=3 SCAR=24.8
141 0 my $found_some;
142 0 ▶ until ( defined($found_some) && not($found_some) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $found_some and not $found_some) 0 0 0
Condition0%0/3
defined $found_some and not $found_some
!ll&&!rl&&r
000
MC/DC0%0/2
defined $found_some and not $found_some
defined $found_some not $found_some
Truth table0%0/3
A and not(B)
A= defined $found_someB= not $found_some
ABresult
0X0
100
111
143 0 $found_some = $summary =~ s/\([^\(]*?\)//g;
144 0 warn "Found some: $found_some\n";
145 }
146 0 return $summary;
147 }
148  
149  
150 sub _get_random_title_linked_from_title {
151 0 ▶ my ($title) = @_;
Subroutine _get_random_title_linked_from_title: not called CC=7 SCAR=40.3
152  
153 0 my $result = $mw->api( {
154 action => 'query',
155 prop => 'links',
156 titles => $title,
157 plnamespace => 0,
158 pllimit => 100,
159 } );
160  
161 0 my $links_ref = (values(%{$result->{query}->{pages}}))[0]->{links};
162  
163 0 my @links = shuffle(@$links_ref);
164  
165 0 my $linked_title;
166  
167 0 ▶ until ($linked_title || (@links == 0 )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($linked_title or @links == 0) 0 0 0
Condition0%0/3
$linked_title or @links == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$linked_title or @links == 0
$linked_title @links == 0
Truth table0%0/3
A or B
A= $linked_titleB= @links == 0
ABresult
1X1
011
000
168 0 ▶ if (defined $links[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $links[0]) 0 0 0
169 0 my $proposed_title = $links[0]->{title};
170 # Skip:
171 # * Any title we've already seen
172 # * Any title with a numeral in it (to stay away from annual-
173 # statistics gravity wells)
174 # * Any title with a word suggesting it's a just a list or table
175 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($Sweat::Article::seen_titles{$proposed_title} or $proposed_title =~ /\d|^list of\s|^comparison of\s|^table of\s/iu) 0 0 0
Condition0%0/3
$Sweat::Article::seen_titles{$proposed_title} or $proposed_title =~ /\d|^list of\s|^comparison of\s|^table of\s/iu
l!l&&r!l&&!r
000
MC/DC0%0/2
$Sweat::Article::seen_titles{$proposed_title} or $proposed_title =~ /\d|^list of\s|^comparison of\s|^table of\s/iu
$Sweat::Article::seen_titles{$proposed_title} $proposed_title =~ /\d|^list of\s|^comparison of\s|^table of\s/iu
Truth table0%0/3
A or B
A= $Sweat::Article::seen_titles{$proposed_title}B= $proposed_title =~ /\d|^list of\s|^comparison of\s|^table of\s/iu
ABresult
1X1
011
000
176 $seen_titles{$proposed_title}
177 ||
178 $proposed_title =~ /\d|^list of\s|^comparison of\s|^table of\s/i
179 ) {
180 0 $linked_title = $proposed_title;
181 0 $seen_titles{$proposed_title} = 1;
182 }
183 }
184 0 shift @links;
185 }
186  
187 0 ▶ if ($linked_title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linked_title) { } 0 0 0
188 0 return $linked_title;
189 }
190 else {
191 0 return _get_random_title();
192 }
193 }
194  
195  
196 3 1;
197  
198 =head1 Sweat::Article - Library for the `sweat` command-line program
199  
200 =head1 DESCRIPTION
201  
202 This library is intended for internal use by the L<sweat> command-line program,
203 and as such offers no publicly documented methods.
204  
205 =head1 SEE ALSO
206  
207 L<sweat>
208  
209 =head1 AUTHOR
210  
211 Jason McIntosh <jmac@jmac.org>
↑ Summary blib/lib/Sweat.pm »