line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::DiffBot; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
32484
|
use 5.006; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
29
|
|
6
|
1
|
|
|
1
|
|
1136
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
53864
|
|
|
1
|
|
|
|
|
35
|
|
7
|
1
|
|
|
1
|
|
1223
|
use JSON::XS; |
|
1
|
|
|
|
|
13271
|
|
|
1
|
|
|
|
|
94
|
|
8
|
1
|
|
|
1
|
|
11
|
use URI::Escape qw(uri_escape); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
70
|
|
9
|
1
|
|
|
1
|
|
7
|
use HTTP::Request; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
965
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $endpoint_url = 'http://www.diffbot.com/api/article'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Net::DiffBot - Interface to the diffbot.com API |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 VERSION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Version 0.02 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
This module is just an interface for www.diffbot.com API. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Net::DiffBot; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $d = Net::DiffBot->new('token' => 'diffbottoken'); |
33
|
|
|
|
|
|
|
my $page_data = $d->get_data_from_url($url) |
34
|
|
|
|
|
|
|
... |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 new |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Constructor method, you need to pass the diffbot token |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $d = Net::DiffBot->new('token' => 'diffbottoken'); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub new { |
49
|
0
|
|
|
0
|
1
|
|
my ($p, %args) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
die "No token provided" if (!exists $args{'token'}); |
52
|
0
|
|
0
|
|
|
|
$p = ref($p) || $p; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my $self = bless { |
55
|
|
|
|
|
|
|
%args |
56
|
|
|
|
|
|
|
}, $p; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return $self |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head2 get_data_from_url |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Fetch diffbot data based on the url , along with the url you can set other options |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $page_data = $d->get_data_from_url($url, 'tags' => 1, summary => 1) |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Valid flags are: callback, html, dontStripAds, tags, comments, summary |
68
|
|
|
|
|
|
|
You can see the use of theses flags at www.diffbot.com |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Returns the page data as an hashref. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 get_data_from_content |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Fetch diffbot data based on sent content , you also need to send the url and the content type ('text/plain', 'text/html'). You can also set other options as with get_data_from_url. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $page_data = my $d->get_data_from_content($url, $content, $content_type, 'tags' => 1, summary => 1) |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Valid flags are: callback, html, dontStripAds, tags, comments, summary |
81
|
|
|
|
|
|
|
You can see the use of theses flags at www.diffbot.com |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Returns the page data as an hashref. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get_data_from_content { |
89
|
0
|
|
|
0
|
1
|
|
my ($self, $url, $content, $content_type, %args) = @_; |
90
|
0
|
0
|
0
|
|
|
|
if (($content_type ne 'text/plain') and ($content_type ne 'text/html') ) { |
91
|
0
|
|
|
|
|
|
warn "Invalid content type, possible values are 'text/plain' or 'text/html'"; |
92
|
0
|
|
|
|
|
|
return undef; |
93
|
|
|
|
|
|
|
} |
94
|
0
|
0
|
|
|
|
|
if (!$url) { |
95
|
0
|
|
|
|
|
|
warn "No url provided"; |
96
|
0
|
|
|
|
|
|
return undef; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my $request_args = $self->get_request_args($url, %args); |
100
|
0
|
|
|
|
|
|
my $request_url = $self->build_request_url(%{$request_args}); |
|
0
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my $content_length = length($content); |
104
|
0
|
|
|
|
|
|
my $headers = HTTP::Headers->new(); |
105
|
0
|
|
|
|
|
|
$headers->header('Content-type' => $content_type); |
106
|
0
|
|
|
|
|
|
$headers->header('Content-length' => $content_length); |
107
|
0
|
|
|
|
|
|
my $http_request = HTTP::Request->new('POST', $request_url, $headers, $content); |
108
|
0
|
|
|
|
|
|
my $response = $ua->request($http_request); |
109
|
0
|
0
|
|
|
|
|
if (!$response->is_success) { |
110
|
0
|
|
|
|
|
|
warn "ERROR with request " . $request_url . " HTTP response" . $response->status_line; |
111
|
0
|
|
|
|
|
|
return undef; |
112
|
|
|
|
|
|
|
} else { |
113
|
0
|
|
|
|
|
|
my $data; |
114
|
0
|
|
|
|
|
|
eval { |
115
|
0
|
|
|
|
|
|
$data = decode_json($response->content); |
116
|
|
|
|
|
|
|
}; |
117
|
0
|
0
|
|
|
|
|
if ($@) { |
118
|
0
|
|
|
|
|
|
warn "ERROR decoding JSON response"; |
119
|
0
|
|
|
|
|
|
return undef; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
return $data; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub get_request_args { |
130
|
0
|
|
|
0
|
0
|
|
my ($self, $url, %args) = @_; |
131
|
0
|
|
|
|
|
|
my @possible_args = qw(callback html dontStripAds tags comments summary); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
my %request_args = ( |
134
|
|
|
|
|
|
|
'url' => $url, |
135
|
|
|
|
|
|
|
); |
136
|
0
|
|
|
|
|
|
for my $arg (@possible_args) { |
137
|
0
|
0
|
0
|
|
|
|
if ((exists $args{$arg}) and ($args{$arg}) ) { |
138
|
0
|
|
|
|
|
|
$request_args{$arg} = 'true'; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
return \%request_args; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
sub get_data_from_url { |
147
|
0
|
|
|
0
|
1
|
|
my ($self, $url, %args) = @_; |
148
|
0
|
0
|
|
|
|
|
if (!$url) { |
149
|
0
|
|
|
|
|
|
warn "No url provided"; |
150
|
0
|
|
|
|
|
|
return undef; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
my $request_args = $self->get_request_args($url, %args); |
155
|
0
|
|
|
|
|
|
my $request_url = $self->build_request_url(%{$request_args}); |
|
0
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $response = $ua->get($request_url); |
159
|
0
|
0
|
|
|
|
|
if (!$response->is_success) { |
160
|
0
|
|
|
|
|
|
warn "ERROR with request " . $request_url . " HTTP response" . $response->status_line; |
161
|
0
|
|
|
|
|
|
return undef; |
162
|
|
|
|
|
|
|
} else { |
163
|
0
|
|
|
|
|
|
my $data; |
164
|
0
|
|
|
|
|
|
eval { |
165
|
0
|
|
|
|
|
|
$data = decode_json($response->content); |
166
|
|
|
|
|
|
|
}; |
167
|
0
|
0
|
|
|
|
|
if ($@) { |
168
|
0
|
|
|
|
|
|
warn "ERROR decoding JSON response"; |
169
|
0
|
|
|
|
|
|
return undef; |
170
|
|
|
|
|
|
|
} |
171
|
0
|
|
|
|
|
|
return $data; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub build_request_url { |
177
|
0
|
|
|
0
|
0
|
|
my ($self, %args) = @_; |
178
|
0
|
|
|
|
|
|
$args{'token'} = $self->{'token'}; |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
my @keys = sort( grep { defined $args{$_} } keys(%args) ); |
|
0
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
if (%args) { |
183
|
0
|
|
|
|
|
|
return "$endpoint_url?" . join( '&', map { uri_escape($_,$self->{'uri_unsafe'}) . '=' . uri_escape( $args{$_} ) } @keys ); |
|
0
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} else { |
185
|
0
|
|
|
|
|
|
return $endpoint_url; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 AUTHOR |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Bruno Martins, C<< >> |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SUPPORT |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
perldoc Net::DiffBot |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Github repo https://github.com/bmartins/Net-DiffBot |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=item * Search CPAN |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
L |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=back |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Copyright 2012 Bruno Martins. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
216
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
217
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=cut |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
1; # End of Net::DiffBot |