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 $ua = $feed_class->build_user_agent;
my $opml = try {
my $response = $ua->get($opml_source);
die sprintf('Got code "%s" processing OPML %s',$response->status_line,$opml_source)
unless $response->is_success;
XML::LibXML->load_xml(string => $response->content);
} 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');
no warnings 'experimental::smartmatch';
next if $uri ~~ @exclude;
feed(%$common_args,title=>$title,uri=>$uri);
}
Log::Log4perl::MDC->put( opml => undef );
return;
}
1;