summaryrefslogtreecommitdiff
path: root/lib/Feed/Utils.pm
blob: 7a6c5bb253b40a648d9d9d4b01bbfd9ab29c4c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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');
 
        no warnings 'experimental::smartmatch';
 
        next if $uri ~~ @exclude;
 
        feed(%$common_args,title=>$title,uri=>$uri);
    }
 
    Log::Log4perl::MDC->put( opml => undef );
 
    return;
}
 
1;