line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
our $VERSION = v0.45.0; |
2
|
|
|
|
|
|
|
use Moo; |
3
|
1
|
|
|
1
|
|
206542
|
|
|
1
|
|
|
|
|
9163
|
|
|
1
|
|
|
|
|
5
|
|
4
|
|
|
|
|
|
|
use Mojo::UserAgent; |
5
|
1
|
|
|
1
|
|
1690
|
use Mojo::UserAgent::Transactor; |
|
1
|
|
|
|
|
323988
|
|
|
1
|
|
|
|
|
7
|
|
6
|
1
|
|
|
1
|
|
37
|
use Try::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
437
|
|
|
1
|
|
|
|
|
1096
|
|
|
1
|
|
|
|
|
51
|
|
8
|
|
|
|
|
|
|
use Types::Standard qw< Str >; |
9
|
1
|
|
|
1
|
|
457
|
use Types::URI qw< Uri >; |
|
1
|
|
|
|
|
59574
|
|
|
1
|
|
|
|
|
9
|
|
10
|
1
|
|
|
1
|
|
1044
|
|
|
1
|
|
|
|
|
78970
|
|
|
1
|
|
|
|
|
11
|
|
11
|
|
|
|
|
|
|
use Importer 'NewsExtractor::TextUtil' => qw(u); |
12
|
1
|
|
|
1
|
|
376
|
use NewsExtractor::Error; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
13
|
1
|
|
|
1
|
|
329
|
use NewsExtractor::Download; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
14
|
1
|
|
|
1
|
|
373
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
265
|
|
15
|
|
|
|
|
|
|
has url => ( required => 1, is => 'ro', isa => Uri, coerce => 1 ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has user_agent_string => ( |
18
|
|
|
|
|
|
|
required => 1, |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
isa => Str, |
21
|
|
|
|
|
|
|
default => sub { |
22
|
|
|
|
|
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:93.0) Gecko/20100101 Firefox/93.0' |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my NewsExtractor $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
0
|
0
|
|
my $ua = Mojo::UserAgent->new()->transactor( |
29
|
|
|
|
|
|
|
Mojo::UserAgent::Transactor->new()->name( $self->user_agent_string ) |
30
|
0
|
|
|
|
|
|
)->max_redirects(3); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my ($error, $download); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $tx = $ua->get( "". $self->url ); |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $res; |
37
|
|
|
|
|
|
|
try { |
38
|
0
|
|
|
|
|
|
$res = $tx->result |
39
|
|
|
|
|
|
|
} catch { |
40
|
0
|
|
|
0
|
|
|
$error = NewsExtractor::Error->new( |
41
|
|
|
|
|
|
|
is_exception => 0, |
42
|
0
|
|
|
0
|
|
|
message => u($_), |
43
|
|
|
|
|
|
|
) |
44
|
|
|
|
|
|
|
}; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
if ($res) { |
47
|
|
|
|
|
|
|
if ($res->is_error) { |
48
|
0
|
0
|
|
|
|
|
$error = NewsExtractor::Error->new( |
49
|
0
|
0
|
|
|
|
|
is_exception => 0, |
50
|
0
|
|
|
|
|
|
message => u($res->message), |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
} else { |
53
|
|
|
|
|
|
|
$download = NewsExtractor::Download->new( tx => $tx ); |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
return ($error, $download); |
57
|
|
|
|
|
|
|
} |
58
|
0
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 NAME |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
NewsExtractor - download and extract news articles from Internet. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 SYNOPSIS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my ($error, $article) = NewsExtractor->new( url => $url )->download->parse; |
69
|
|
|
|
|
|
|
die $error if $error; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# $article is an instance of NewsExtractor::Article |
72
|
|
|
|
|
|
|
say "Headline: " . $article->headline; |
73
|
|
|
|
|
|
|
say "When: " . ($article->dateline // "(unknown)"); |
74
|
|
|
|
|
|
|
say "By: " . ($article->journalist // "(unknown)"); |
75
|
|
|
|
|
|
|
say "\n" . $article->article_body; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 SEE Also |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
L<NewsExtractor::Article> |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 AUTHOR |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Kang-min Liu <gugod@gugod.org> |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 LICENSE |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
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. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
https://creativecommons.org/publicdomain/zero/1.0/ |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |