line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NewsExtractor; |
2
|
|
|
|
|
|
|
our $VERSION = v0.44.0; |
3
|
1
|
|
|
1
|
|
291388
|
use Moo; |
|
1
|
|
|
|
|
15120
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
2954
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
502647
|
|
|
1
|
|
|
|
|
16
|
|
6
|
1
|
|
|
1
|
|
66
|
use Mojo::UserAgent::Transactor; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
7
|
1
|
|
|
1
|
|
1545
|
use Try::Tiny; |
|
1
|
|
|
|
|
1672
|
|
|
1
|
|
|
|
|
70
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
1837
|
use Types::Standard qw< Str >; |
|
1
|
|
|
|
|
88652
|
|
|
1
|
|
|
|
|
21
|
|
10
|
1
|
|
|
1
|
|
1752
|
use Types::URI qw< Uri >; |
|
1
|
|
|
|
|
121128
|
|
|
1
|
|
|
|
|
22
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
535
|
use Importer 'NewsExtractor::TextUtil' => qw(u); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
12
|
|
13
|
1
|
|
|
1
|
|
493
|
use NewsExtractor::Error; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
70
|
|
14
|
1
|
|
|
1
|
|
755
|
use NewsExtractor::Download; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
344
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:93.0) Gecko/20100101 Firefox/93.0' |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub download { |
28
|
0
|
|
|
0
|
0
|
|
my NewsExtractor $self = shift; |
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
|
|
|
0
|
|
|
$res = $tx->result |
41
|
|
|
|
|
|
|
} catch { |
42
|
0
|
|
|
0
|
|
|
$error = NewsExtractor::Error->new( |
43
|
|
|
|
|
|
|
is_exception => 0, |
44
|
|
|
|
|
|
|
message => u($_), |
45
|
|
|
|
|
|
|
) |
46
|
0
|
|
|
|
|
|
}; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if ($res) { |
49
|
0
|
0
|
|
|
|
|
if ($res->is_error) { |
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; |
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 |