package Feed::Utils;
use strict;
use warnings;
use 5.012;
my @methods;
BEGIN {
@methods = qw(
set_feed_args
set_feed_class
feed
feeds
feeds_from_opml
);
}
use Sub::Exporter -setup => {
exports => \@methods,
groups => {
default => \@methods,
},
};
use XML::LibXML;
use Try::Tiny;
my $feed_class = 'Feed';
sub set_feed_class {
$feed_class = shift;
}
my %feed_args = ();
sub set_feed_args {
%feed_args = @_;
}
sub feed {
my (%args) = @_;
my $feed = $feed_class->new({
%feed_args,
%args,
});
try {
$feed->process;
} catch {
$feed->log->error($_);
};
return;
}
sub feeds {
my ($common_args,@args) = @_;
for my $arg (@args) {
if (!ref($arg)) {
feed(%$common_args,uri=>$arg);
}
else {
feed(%$common_args,%$arg);
}
}
return;
}
sub feeds_from_opml {
my ($common_args,$opml_source,$opts) = @_;
my $opml = try {
XML::LibXML->load_xml(location => $opml_source);
} catch { warn $_; return };
return unless $opml;
Log::Log4perl::MDC->put( opml => "$opml_source" );
my @exclude = @{$opts->{exclude} // []};
for my $feed_node ($opml->findnodes(q{/opml/body/outline[@xmlUrl != '']})) {
my $title = $feed_node->findvalue('@title')
// $feed_node->findvalue('@text') // '';
my $uri = $feed_node->findvalue('@xmlUrl');
next if $uri ~~ @exclude;
feed(%$common_args,title=>$title,uri=>$uri);
}
Log::Log4perl::MDC->put( opml => undef );
return;
}
1;