| 1 | package Net::DiffBot; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use URI::Escape qw(uri_escape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | 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 | 2 | 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 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | my ($p, %args) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | die "No token provided" if (!exists $args{'token'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $p = ref($p) || $p; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $p || $p
MC/DC0%0/2
ref $p || $p
ref $p
$p
Truth table0%0/3
A || B
A= ref $pB= $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 | 0 | ▶ | sub get_data_from_content { | ||||||||||||||||||||||||||
|
Subroutine
get_data_from_content: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 0 | my ($self, $url, $content, $content_type, %args) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (($content_type ne 'text/plain') and ($content_type ne 'text/html') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$content_type ne "text/plain" and $content_type ne "text/html"
MC/DC0%0/2
$content_type ne "text/plain" and $content_type ne "text/html"
$content_type ne "text/plain"
$content_type ne "text/html"
Truth table0%0/3
A and B
A= $content_type ne "text/plain"B= $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 | ▶ | if (!$url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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}); | |||||||||||||||||||||||||||
| 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 | ▶ | if (!$response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | warn "ERROR decoding JSON response"; | |||||||||||||||||||||||||||
| 119 | 0 | return undef; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | return $data; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub get_request_args { | ||||||||||||||||||||||||||
|
Subroutine
get_request_args: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 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 | ▶ | if ((exists $args{$arg}) and ($args{$arg}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $args{$arg} and $args{$arg}
MC/DC0%0/2
exists $args{$arg} and $args{$arg}
exists $args{$arg}
$args{$arg}
Truth table0%0/3
A and B
A= exists $args{$arg}B= $args{$arg}
| |||||||||||||||||||||||||||||
| 138 | 0 | $request_args{$arg} = 'true'; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | return \%request_args; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub get_data_from_url { | ||||||||||||||||||||||||||
|
Subroutine
get_data_from_url: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 0 | my ($self, $url, %args) = @_; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if (!$url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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}); | |||||||||||||||||||||||||||
| 156 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | my $response = $ua->get($request_url); | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (!$response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | warn "ERROR decoding JSON response"; | |||||||||||||||||||||||||||
| 169 | 0 | return undef; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | return $data; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub build_request_url { | ||||||||||||||||||||||||||
|
Subroutine
build_request_url: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 178 | 0 | $args{'token'} = $self->{'token'}; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my @keys = sort( grep { defined $args{$_} } keys(%args) ); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if (%args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | return "$endpoint_url?" . join( '&', map { uri_escape($_,$self->{'uri_unsafe'}) . '=' . uri_escape( $args{$_} ) } @keys ); | |||||||||||||||||||||||||||
| 184 | } else { | ||||||||||||||||||||||||||||
| 185 | 0 | return $endpoint_url; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Bruno Martins, C<< <bscmartins at gmail.com> >> | ||||||||||||||||||||||||||||
| 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<http://search.cpan.org/dist/Net-DiffBot/> | ||||||||||||||||||||||||||||
| 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 | 2 | 1; # End of Net::DiffBot | |||||||||||||||||||||||||||