From ecf544c4b90f6ade5271f475df0d5d60920a282b Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Thu, 24 Sep 2020 11:51:25 +0100 Subject: [PATCH] Added ability to override number of entries for a specific feed --- MANIFEST | 3 +++ bin/perlanet | 5 ++++- lib/Perlanet.pm | 10 +++++++--- lib/Perlanet/Feed.pm | 5 +++++ t/09_entries.t | 25 +++++++++++++++++++++++++ t/5entriesrc | 22 ++++++++++++++++++++++ t/allentriesrc | 22 ++++++++++++++++++++++ 7 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 t/09_entries.t create mode 100644 t/5entriesrc create mode 100644 t/allentriesrc diff --git a/MANIFEST b/MANIFEST index 25a74b4..ac98e4b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -24,6 +24,7 @@ t/05_cache.t t/06_youtube.t t/07_page.t t/08_opml.t +t/09_entries.t t/pod.t t/pod_coverage.t t/testrc @@ -33,6 +34,8 @@ t/cacherc t/mprc t/youtuberc t/youtube2rc +t/allentriesrc +t/5entriesrc t/index.tt README Changes diff --git a/bin/perlanet b/bin/perlanet index 885a6db..0e4ac87 100755 --- a/bin/perlanet +++ b/bin/perlanet @@ -177,7 +177,10 @@ to identify the entries that originate from this feed. The optional C sub-option gives a web site URL associated with the feed (often the address of the web site that the feed comes from). This can be used to create a list of the aggregated sites. The C sub-option becomes mandatory if you are -creating an OPML file. +creating an OPML file. You can also add a C option which will +override the global C setting. If this setting exists +and is set to zero, then the output feed will contain all of the entries +from this input feed. =back diff --git a/lib/Perlanet.pm b/lib/Perlanet.pm index a6dbaf6..af2be06 100755 --- a/lib/Perlanet.pm +++ b/lib/Perlanet.pm @@ -18,7 +18,7 @@ use XML::Feed; use vars qw{$VERSION}; BEGIN { - $VERSION = '2.0.1'; + $VERSION = '2.0.2'; } with 'MooseX::Traits'; @@ -230,8 +230,12 @@ sub select_entries { @entries = @{ $self->sort_entries(\@entries) }; - if ($self->entries_per_feed and @entries > $self->entries_per_feed) { - $#entries = $self->entries_per_feed - 1; + my $number_of_entries = + defined $feed->max_entries ? $feed->max_entries + : $self->entries_per_feed; + + if ($number_of_entries and @entries > $number_of_entries) { + $#entries = $number_of_entries - 1; } push @feed_entries, diff --git a/lib/Perlanet/Feed.pm b/lib/Perlanet/Feed.pm index 8313095..daf608f 100644 --- a/lib/Perlanet/Feed.pm +++ b/lib/Perlanet/Feed.pm @@ -60,6 +60,11 @@ has 'modified' => ( is => 'rw', ); +has 'max_entries' => ( + is => 'ro', + isa => 'Int', +); + has 'entries' => ( isa => 'ArrayRef', is => 'rw', diff --git a/t/09_entries.t b/t/09_entries.t new file mode 100644 index 0000000..dded639 --- /dev/null +++ b/t/09_entries.t @@ -0,0 +1,25 @@ +use strict; +use warnings; +use Test::More; +use FindBin qw($Bin); +use Perlanet::Simple; + +chdir $Bin; + +my @tests = ({ + rc => '5', + count => 5, +}, { + rc => 'all', + count => 10, +}); + +for (@tests) { + my $p = Perlanet::Simple->new_with_config(configfile => "$_->{rc}entriesrc"); + + my $feeds = $p->fetch_feeds($p->feeds); + my $selected = $p->select_entries($feeds); + is(@$selected, $_->{count}, "Override to get $_->{rc} selected entries"); +} + +done_testing(); diff --git a/t/5entriesrc b/t/5entriesrc new file mode 100644 index 0000000..57b4719 --- /dev/null +++ b/t/5entriesrc @@ -0,0 +1,22 @@ +title: planet test +description: Testing stuff +url: http://theplanetarium.org/ +self_link: http://theplanetarium.org/ +agent: Testing Perlanet +entries_per_feed: 1 +author: + name: Dave Cross + email: dave@dave.org.uk +max_entries: 20 +page: + file: index.html + template: index.tt +feed: + file: rss.xml + format: Atom +feeds: + - url: file:data/davblog.xml + title: davblog + web: http://blogs.dave.org.uk/ + max_entries: 5 + diff --git a/t/allentriesrc b/t/allentriesrc new file mode 100644 index 0000000..4f76f46 --- /dev/null +++ b/t/allentriesrc @@ -0,0 +1,22 @@ +title: planet test +description: Testing stuff +url: http://theplanetarium.org/ +self_link: http://theplanetarium.org/ +agent: Testing Perlanet +entries_per_feed: 1 +author: + name: Dave Cross + email: dave@dave.org.uk +max_entries: 20 +page: + file: index.html + template: index.tt +feed: + file: rss.xml + format: Atom +feeds: + - url: file:data/davblog.xml + title: davblog + web: http://blogs.dave.org.uk/ + max_entries: 0 +