-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-actor.pl
executable file
·205 lines (162 loc) · 4.8 KB
/
rss-actor.pl
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/perl
use v5.14;
use strict;
use warnings;
no if $] >= 5.018, warnings => "experimental::smartmatch";
use XML::RSS;
use LWP::Simple 'get';
use Data::Dumper;
use Data::DPath 'dpath';
use YAML 'LoadFile';
use Getopt::Long;
my $DEBUG = 0;
my $IGNORESEEN = 0;
my $NOEXEC = 0;
my $SEEN_DIR = "$ENV{HOME}/.rss-actor";
GetOptions(
"d|debug!" => \$DEBUG,
"is|ignoreseen!" => \$IGNORESEEN,
"ne|noexec" => \$NOEXEC,
) || usage();
usage() if scalar @ARGV == 0;
process_config($_) foreach @ARGV;
sub usage {
print STDERR "Usage: $0 [-d|-debug] [-is|-ignoreseen] [-ne|-noexec] conffile [conffile...]\n";
exit 1;
}
sub DEBUG {
print join " ", @_ if $DEBUG;
}
sub get_var {
my ($vars, $name, $data) = @_;
for my $var ( @$vars ) {
if ($var->{name} eq $name) {
if ($var->{value}) {
return $var->{value};
}
if ($var->{dpath}) {
my $vals = $data ~~ dpath $var->{dpath};
return $vals->[0];
}
die "Invalid variable type for $var->{name}";
}
}
die "Variable $name not found";
}
sub get_val {
my ( $vars, $v, $data) = @_;
my $nv;
if (my ($a,$b,$c) = ($v =~ /(.*?)%([^%]+)%(.*)/)) {
$nv .= $a;
my $var = get_var($vars, $b, $data);
$nv .= get_val($vars, $var, $data);
$nv .= get_val($vars, $c, $data);
} else {
$nv = $v;
}
return $nv;
}
sub seen_filename {
my $id = shift;
return "$SEEN_DIR/$id.seen.txt";
}
sub read_seen {
my $id = shift;
open(my $F, seen_filename($id)) || return [];
my @seen = ();
while (<$F>) {
chomp;
push @seen, $_;
}
close $F;
return \@seen;
}
sub write_seen {
my ($id, $seen) = @_;
my $fn = seen_filename($id);
open(my $F, ">$fn") || do {
print STDERR "Could not create file $fn";
return;
};
print $F "$_\n" foreach ( @$seen );
close $F;
}
sub process_config {
my $fname = shift;
DEBUG("Processing config $fname\n");
my $config;
eval {
$config = LoadFile($fname);
};
if ($@) {
print STDERR "Could not load config from $fname: $@\n";
return;
}
my $vars = $config->{vars};
my $xml;
if ($config->{channel}) {
my $channel = get_val($vars, $config->{channel});
$xml = get($channel);
die "Could not download channel $channel" unless $xml;
} elsif ($config->{file}) {
open (my $F, get_val($vars, $config->{file})) || die "Cannot open config $config->{file}";
$xml = "";
while (<$F>) { $xml .= $_ };
close($F);
} else {
die "No channel or file defined in $fname";
}
my $rss = new XML::RSS (version => '1.0');
$rss->parse($xml);
mkdir $SEEN_DIR || die "Could not create directory $SEEN_DIR: $?";
my $seen = read_seen($config->{id});
foreach my $item (@{$rss->{'items'}}) {
my $title = $item->{title};
$title =~ s/[^[:ascii:]]+/_/g;
if (!$IGNORESEEN && grep { $_ eq $title} @$seen) {
DEBUG("Already seen: $title\n");
next;
}
DEBUG("New in channel: $title\n");
for my $match ( @{$config->{match}}) {
my $vals = $item ~~ dpath $match->{dpath};
my $val = $vals->[0];
if ($match->{regexp} && $val !~ /$match->{regexp}/) {
DEBUG("No match, ignoring: $val\n");
next;
}
if ($match->{noregexp} && $val =~ /$match->{noregexp}/) {
DEBUG("Ignore match, ignoring: $val\n");
next;
}
DEBUG("OK: $val\n");
my $mvars = $match->{vars}||[];
my @lvars=(@$vars, @$mvars);
for my $do ( @{$match->{do}} ) {
given ($do->{action}) {
when (/^print$/) {
my $str = get_val(\@lvars, $do->{print}, $item);
print "$str\n";
}
when (/^exec$/) {
my $cmd = $do->{cmd};
$cmd = [ $cmd ] if ref $cmd eq "";
my @ecmd = map { get_val(\@lvars, $_, $item) } @$cmd;
DEBUG($NOEXEC ? "NOT" : "", "Executing:", join (" ", @ecmd), "\n");
if (!$NOEXEC) {
system(@ecmd) == 0 || die "Exec @ecmd failed: $?"
}
}
when (/^dump$/) {
print Dumper($item);
}
default {
die "Uknown action: '$do->{action}'\n";
}
}
}
push @$seen, $title;
write_seen($config->{id}, $seen);
}
}
}