Feed.pm

filter: Statementstmt 82.0% 114 / 139 Branchbran 58.0% 36 / 62 Conditioncond 47.6% 10 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 88.0% 22 / 25 Podpod 100% 4 / 4 totaltotal 71.9% 190 / 264 CC 41 SCAR 25.0
CC
41
Cov
72%
_get_home_url
42.0
__ANON__
42.0
_extract_image_urls
10.1
CRAP 12.1
↑ Summary Feed/Run.pm »
1 package BusyBird::Input::Feed;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use XML::FeedPP;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use BusyBird::DateTime::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use WWW::Favicon ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $VERSION = "0.07";
15  
16 5 our @CARP_NOT = qw(Try::Tiny XML::FeedPP);
17  
18 5 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
19 5 my ($class, %args) = @_;
20 my $self = bless {
21 use_favicon => defined($args{use_favicon}) ? $args{use_favicon} : 1,
22 favicon_detector => WWW::Favicon->new,
23 user_agent => defined($args{user_agent}) ? $args{user_agent} : do {
24 5 my $ua = LWP::UserAgent->new;
25 5 $ua->env_proxy;
26 5 $ua->timeout(30);
27 5 $ua->agent("BusyBird::Inpu::Feed-$VERSION"); ## some Web sites ban LWP::UserAgent's default UserAgent...
28 5 ▶ $ua;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'image_max_num'} ? : 2 3 5
29 },
30 5 ▶ image_max_num => defined($args{image_max_num}) ? $args{image_max_num} : 3,
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'use_favicon'} ? : 5 0 5
defined $args{'user_agent'} ? : 0 5 5
31 }, $class;
32  
33 ## Note that WWW::Favicon#ua accessor method is not documented (as of version 0.03001)
34 5 $self->{favicon_detector}->ua($self->{user_agent});
35
36 5 return $self;
37 }
38  
39 522 ▶sub _get_url_head_and_dir {
Subroutine _get_url_head_and_dir: called CC=5 SCAR=16.7
40 522 my ($url_raw) = @_;
41 522 ▶ return (undef, undef) if not defined $url_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url_raw 1 521 522
42 521 my $url = URI->new($url_raw);
43 521 my $scheme = $url->scheme;
44 521 my $authority = $url->authority;
45 521 ▶ return (undef, undef) if !$scheme || !$authority;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme and $authority 0 521 521
Condition33%1/3
$scheme and $authority
!ll&&!rl&&r
00521
MC/DC0%0/2
$scheme and $authority
$scheme $authority
Truth table33%1/3
A and B
A= $schemeB= $authority
ABresult
0X0
100
111
46 521 my $url_head = "$scheme://$authority";
47 521 my $url_dir;
48 521 my $path = $url->path;
49 521 ▶ if($path =~ m{^(.*/)}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^(.*/)]i) { } 521 0 521
50 521 $url_dir = $1;
51 }else {
52 0 $url_dir = "/";
53 }
54 521 return ($url_head, $url_dir);
55 }
56  
57 547 ▶sub _extract_image_urls {
Subroutine _extract_image_urls: called CC=10 SCAR=23.2
58 547 my ($self, $feed_item) = @_;
59 547 ▶ return () if $self->{image_max_num} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"image_max_num"} == 0 25 522 547
60 522 my $content = $feed_item->description;
61 522 ▶ return () if !defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 522 522
62 522 my ($url_head, $url_dir) = _get_url_head_and_dir($feed_item->link);
63 522 my @urls = ();
64 522 ▶ while(($self->{image_max_num} < 0 || @urls < $self->{image_max_num})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"image_max_num"} < 0 || @urls < $self->{"image_max_num"} and $content =~ /<\s*img\s+[^>]*src\s*=\s*(['"])([^>]+?)\1[^>]*>/gi) 558 522 1080
Condition100%6/6
$self->{"image_max_num"} < 0 || @urls < $self->{"image_max_num"} and $content =~ /<\s*img\s+[^>]*src\s*=\s*(['"])([^>]+?)\1[^>]*>/gi
!ll&&!rl&&r
120402558
$self->{"image_max_num"} < 0 or @urls < $self->{"image_max_num"}
l!l&&r!l&&!r
160800120
MC/DC100%3/3
$self->{"image_max_num"} < 0 || @urls < $self->{"image_max_num"} and $content =~ /<\s*img\s+[^>]*src\s*=\s*(['"])([^>]+?)\1[^>]*>/gi
$self->{"image_max_num"} < 0 @urls < $self->{"image_max_num"} $content =~ /<\s*img\s+[^>]*src\s*=\s*(['"])([^>]+?)\1[^>]*>/gi
Truth table100%5/5
A or B and C
A= $self->{"image_max_num"} < 0B= @urls < $self->{"image_max_num"}C= $content =~ /<\s*img\s+[^>]*src\s*=\s*(['"])([^>]+?)\1[^>]*>/gi
ABCresult
00X0
1X00
0100
1X11
0111
65 && $content =~ m{<\s*img\s+[^>]*src\s*=\s*(['"])([^>]+?)\1[^>]*>}ig) {
66 558 my $url = URI->new($2);
67 558 ▶ if(!$url->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url->scheme) 27 531 558
68 ## Only "path" segment is in the src attribute.
69 27 ▶ next if !defined($url_head) || !defined($url_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url_head and defined $url_dir 0 27 27
Condition33%1/3
defined $url_head and defined $url_dir
!ll&&!rl&&r
0027
MC/DC0%0/2
defined $url_head and defined $url_dir
defined $url_head defined $url_dir
Truth table33%1/3
A and B
A= defined $url_headB= defined $url_dir
ABresult
0X0
100
111
70 27 ▶ if(substr("$url", 0, 1) eq "/") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr("$url", 0, 1) eq '/') { } 21 6 27
71 21 $url = "$url_head$url";
72 }else {
73 6 $url = "$url_head$url_dir$url";
74 }
75 }
76 558 push @urls, "$url";
77 }
78 522 return @urls;
79 }
80  
81 0 ▶sub _get_home_url {
Subroutine _get_home_url: not called CC=6 SCAR=37.4
82 0 my ($self, $feed, $statuses) = @_;
83 0 my $home_url = $feed->link;
84 0 ▶ if(defined($home_url) && $home_url =~ m{^https?://}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $home_url and $home_url =~ m[^https?://]i) 0 0 0
Condition0%0/3
defined $home_url and $home_url =~ m[^https?://]i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $home_url and $home_url =~ m[^https?://]i
defined $home_url $home_url =~ m[^https?://]i
Truth table0%0/3
A and B
A= defined $home_urlB= $home_url =~ m[^https?://]i
ABresult
0X0
100
111
85 0 return $home_url;
86 }
87
88 0 foreach my $status (@$statuses) {
89 0 ▶ $home_url = $status->{busybird}{status_permalink} if defined($status->{busybird});
Branch: 1 branch
BranchTrueFalseTotal
if defined $status->{"busybird"} 0 0 0
90 0 ▶ return $home_url if defined $home_url;
Branch: 1 branch
BranchTrueFalseTotal
if defined $home_url 0 0 0
91 }
92 0 return undef;
93 }
94  
95 0 ▶sub _get_favicon_url {
Subroutine _get_favicon_url: not called CC=1 SCAR=6.9
96 0 my ($self, $feed, $statuses) = @_;
97 0 ▶ return try {
Subroutine __ANON__: not called CC=6 SCAR=37.4
98 0 my $home_url = $self->_get_home_url($feed, $statuses);
99 0 ▶ return undef if not defined $home_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $home_url 0 0 0
100 0 my $favicon_url = $self->{favicon_detector}->detect($home_url);
101 0 ▶ return undef if not defined $favicon_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $favicon_url 0 0 0
102 0 my $res = $self->{user_agent}->get($favicon_url);
103 0 ▶ return undef if !$res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
104 0 my $type = $res->header('Content-Type');
105 0 ▶ return undef if defined($type) && $type !~ /^image/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and not $type =~ /^image/i 0 0 0
Condition0%0/3
defined $type and not $type =~ /^image/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and not $type =~ /^image/i
defined $type not $type =~ /^image/i
Truth table0%0/3
A and not(B)
A= defined $typeB= not $type =~ /^image/i
ABresult
0X0
100
111
106 0 return $favicon_url;
107 0 };
108 }
109  
110 547 ▶sub _make_timestamp_datetime {
Subroutine _make_timestamp_datetime: called CC=4 SCAR=14.0
111 547 my ($self, $timestamp_str) = @_;
112 547 ▶ return undef if not defined $timestamp_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timestamp_str 45 502 547
113 502 ▶ if($timestamp_str =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timestamp_str =~ /^\d+$/) 0 502 502
114 0 return DateTime->from_epoch(epoch => $timestamp_str, time_zone => '+0000');
115 }
116 502 ▶ my $datetime = try { DateTime::Format::ISO8601->parse_datetime($timestamp_str) };
Subroutine __ANON__: called CC=1 SCAR=0.0
117 502 ▶ return $datetime if defined $datetime;
Branch: 1 branch
BranchTrueFalseTotal
if defined $datetime 457 45 502
118 45 return BusyBird::DateTime::Format->parse_datetime($timestamp_str);
119 }
120  
121 547 ▶sub _make_status_from_item {
Subroutine _make_status_from_item: called CC=9 SCAR=22.0
122 547 my ($self, $feed_title, $feed_item) = @_;
123 547 my $created_at_dt = $self->_make_timestamp_datetime($feed_item->pubDate);
124 547 my $text = $feed_item->title;
125 547 ▶ $text = "" if !defined($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 547 547
126 547 my $permalink = $feed_item->link;
127 547 ▶ my $status = {
Branch: 2 branches
BranchTrueFalseTotal
defined $permalink ? : 546 1 547
$created_at_dt ? : 502 45 547
128 text => $text,
129 busybird => { defined($permalink) ? (status_permalink => $permalink) : () },
130 created_at => ($created_at_dt ? BusyBird::DateTime::Format->format_datetime($created_at_dt) : undef ),
131 user => { screen_name => $feed_title },
132 };
133 547 my $guid = $feed_item->guid;
134 547 my $item_id;
135 547 ▶ if(defined $guid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $guid) { } 418 129 547
136 418 $item_id = $guid;
137 418 $status->{busybird}{original}{id} = $guid;
138 }else {
139 129 $item_id = $feed_item->link;
140 }
141 547 ▶ if(defined($created_at_dt) && defined($item_id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $created_at_dt and defined $item_id) { } 502 45 547
elsif (defined $item_id) { } 45 0 45
Condition66%2/3
defined $created_at_dt and defined $item_id
!ll&&!rl&&r
450502
MC/DC50%1/2
defined $created_at_dt and defined $item_id
defined $created_at_dt defined $item_id
Truth table66%2/3
A and B
A= defined $created_at_dtB= defined $item_id
ABresult
0X0
100
111
142 502 $status->{id} = $created_at_dt->epoch . '|' . $item_id;
143 }elsif(defined($item_id)) {
144 45 $status->{id} = $item_id;
145 }
146 547 my @image_urls = $self->_extract_image_urls($feed_item);
147 547 ▶ if(@image_urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@image_urls) 210 337 547
148 210 $status->{extended_entities}{media} = [map { +{ media_url => $_, indices => [0,0] } } @image_urls];
149 }
150 547 return $status;
151 }
152  
153 31 ▶sub _make_statuses_from_feed {
Subroutine _make_statuses_from_feed: called CC=4 SCAR=18.7
154 31 my ($self, $feed) = @_;
155 31 my $feed_title = $feed->title;
156 31 my $statuses = [ map { $self->_make_status_from_item($feed_title, $_) } $feed->get_item ];
157 31 ▶ return $statuses if !$self->{use_favicon};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"use_favicon"} 31 0 31
158 0 my $favicon_url = $self->_get_favicon_url($feed, $statuses);
159 0 ▶ return $statuses if not defined $favicon_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $favicon_url 0 0 0
160 0 $_->{user}{profile_image_url} = $favicon_url foreach @$statuses;
161 0 return $statuses;
162 }
163  
164 46 ▶sub _parse_with_feedpp {
Subroutine _parse_with_feedpp: called CC=1 SCAR=0.0
165 46 my ($self, $feed_source, $feed_type) = @_;
166 return $self->_make_statuses_from_feed(XML::FeedPP->new(
167 $feed_source, -type => $feed_type,
168 utf8_flag => 1, xml_deref => 1, lwp_useragent => $self->{user_agent},
169  
170 ## FeedPP and TreePP mess up with User-Agent. It's pretty annoying.
171 46 user_agent => scalar($self->{user_agent}->agent),
172 ));
173 }
174  
175 24 ▶sub parse_string {
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
176 24 my ($self, $string) = @_;
177 24 return $self->_parse_with_feedpp($string, "string");
178 }
179  
180 5 *parse = *parse_string;
181  
182 16 ▶sub parse_file {
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
183 16 my ($self, $filename) = @_;
184 16 return $self->_parse_with_feedpp($filename, "file");
185 }
186  
187 6 ▶sub parse_url {
Subroutine parse_url: called CC=1 SCAR=0.0
Pod documented
188 6 my ($self, $url) = @_;
189 6 return $self->_parse_with_feedpp($url, "url");
190 }
191  
192 5 *parse_uri = *parse_url;
193  
194 5 1;
195 __END__
↑ Summary Feed/Run.pm »