FeedDeduplicator/Deduplicator.pm

filter: Statementstmt 65.7% 25 / 38 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 43.0% 34 / 79 CC 13 SCAR 37.9
CC
13
Cov
40%
deduplicate
80.2
find_canonical
20.0
BEGIN
1.0
CRAP 44.4
« FeedDeduplicator/Publisher.pm ↑ Summary FeedDeduplicator/Aggregator.pm »
1 =head1 NAME
2  
3 App::FeedDeduplicator::Deduplicator - Deduplicator class for
4 App::FeedDeduplicator
5  
6 =head1 DESCRIPTION
7  
8 This module is part of the App::FeedDeduplicator application. It is
9 responsible for deduplicating entries from aggregated feeds.
10  
11 It uses the LWP::UserAgent to fetch the feeds and HTML::TreeBuilder::XPath
12 to parse the HTML content for canonical links.
13  
14 The deduplication process is based on the entry's, id canonical link or title.
15 The deduplicated entries are stored in an array for further processing. It is
16 designed to be used in conjunction with the Aggregator and Publisher classes
17 to provide a complete feed deduplication and publishing solution.
18  
19 =head1 SYNOPSIS
20  
21 use App::FeedDeduplicator::Deduplicator;
22  
23 my $deduplicator = App::FeedDeduplicator::Deduplicator->new(
24 entries => $aggregated_entries,
25 ua => LWP::UserAgent->new(),
26 );
27  
28 $deduplicator->deduplicate();
29  
30 =head1 METHODS
31  
32 =head2 new
33  
34 Creates a new instance of App::FeedDeduplicator::Deduplicator. The constructor
35 accepts a list of entries and a user agent as parameters.
36  
37 The entries should be an array reference containing hash references with the
38 keys 'entry' and 'feed'.
39  
40 The 'entry' key should contain an XML::Feed::Entry object, and the 'feed' key
41 should contain a hash reference with the feed information.
42  
43 The user agent should be an instance of LWP::UserAgent.
44  
45 =head2 deduplicate
46  
47 The main method that deduplicates entries from the aggregated feeds. It
48 iterates through the entries and checks for duplicates based on the id,
49 canonical link or title.
50  
51 It uses a hash to keep track of seen entries and filters out duplicates. The
52 deduplicated entries are stored in the $deduplicated attribute.
53  
54 It is designed to be used in conjunction with the Aggregator and Publisher
55 classes to provide a complete feed deduplication and publishing solution.
56  
57 =head2 find_canonical
58  
59 Finds the canonical link for a given entry. It fetches the entry's link using
60 LWP::UserAgent and parses the HTML content using HTML::TreeBuilder::XPath.
61  
62 It looks for the <link rel="canonical"> tag in the HTML content and returns
63 the canonical URL if found. If the canonical link is not found, it returns
64 undef.
65  
66 It is used during the deduplication process to determine the unique
67 identifier for each entry.
68  
69 =cut
70  
71 package App::FeedDeduplicator::Deduplicator; # For MetaCPAN
72  
73 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 1 class App::FeedDeduplicator::Deduplicator {
78 1 ▶ use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 1 field $entries :param;
83 1 field $deduplicated :reader;
84 1 field $ua :param;
85  
86 1 ▶ method deduplicate {
Subroutine deduplicate: called CC=10 SCAR=43.8
Pod documented
87 1 my %seen;
88 my @result;
89  
90 1 for my $entry (@$entries) {
91 # warn ref($entry) . "\n" . ref($entry->{entry}) . "\n";
92 0 ▶ my $canonical = $self->find_canonical($entry->{entry}) // '';
Condition0%0/2
$self->find_canonical($entry->{"entry"}) // ""
l!l
00
MC/DC0%0/1
$self->find_canonical($entry->{"entry"}) // ""
$self->find_canonical($entry->{"entry"})
Truth table0%0/2
A
A= $self->find_canonical($entry->{"entry"})
Aresult
11
00
93 0 ▶ my $title = $entry->{entry}->title // '';
Condition0%0/2
$entry->{"entry"}->title // ""
l!l
00
MC/DC0%0/1
$entry->{"entry"}->title // ""
$entry->{"entry"}->title
Truth table0%0/2
A
A= $entry->{"entry"}->title
Aresult
11
00
94  
95 push @result, $entry
96 unless ($canonical and $seen{$canonical})
97 0 ▶ or ($title and $seen{$title});
Branch: 1 branch
BranchTrueFalseTotal
unless $canonical and $seen{$canonical} or $title and $seen{$title} 0 0 0
Condition0%0/9
$canonical and $seen{$canonical} or $title and $seen{$title}
l!l&&r!l&&!r
000
$canonical and $seen{$canonical}
!ll&&!rl&&r
000
$title and $seen{$title}
!ll&&!rl&&r
000
MC/DC0%0/4
$canonical and $seen{$canonical} or $title and $seen{$title}
$canonical $seen{$canonical} $title $seen{$title}
Truth table0%0/7
A and B or C and D
A= $canonicalB= $seen{$canonical}C= $titleD= $seen{$title}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
98  
99 0 ▶ ++$seen{$canonical} if $canonical;
Branch: 1 branch
BranchTrueFalseTotal
if $canonical 0 0 0
100 0 ▶ ++$seen{$title} if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 0 0 0
101 }
102  
103 1 $deduplicated = \@result;
104 }
105  
106 0 ▶ method find_canonical ($entry) {
Subroutine find_canonical: not called CC=4 SCAR=30.0
Pod documented
107 0 my $link = $entry->link;
108 0 ▶ return unless $link;
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 0 0
109  
110 0 my $response = $ua->get($link);
111 0 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
112  
113 0 my $tree = HTML::TreeBuilder::XPath->new_from_content(
114 $response->decoded_content
115 );
116 0 my $node = $tree->findnodes('//link[@rel="canonical"]')->[0];
117  
118 0 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
119 0 return URI->new($node->attr('href'))->as_string;
120 }
121 }
122  
123 =head1 AUTHOR
124  
125 Dave Cross <dave@perlhacks.com>
126  
127 =head1 COPYRIGHT AND LICENSE
128  
129 This software is copyright (c) 2025 Magnum Solutions Ltd.
130 All rights reserved.
131  
132 This program is free software; you can redistribute it and/or modify it under
133 the same terms as Perl itself.
134  
135 See L<http://dev.perl.org/licenses/artistic.html> for more details.
136  
137 =cut
138  
139 1 1;
« FeedDeduplicator/Publisher.pm ↑ Summary FeedDeduplicator/Aggregator.pm »