-
Notifications
You must be signed in to change notification settings - Fork 11
/
feed-to-tsv.pl
executable file
·85 lines (74 loc) · 1.75 KB
/
feed-to-tsv.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
#!/usr/bin/env perl
use v5.26;
use Encode qw(decode_utf8 encode_utf8);
use Getopt::Long qw(GetOptions);
use Mojo::UserAgent;
use XML::Loy;
use File::Slurp qw(read_file);
sub fetch {
my ($url) = @_;
my $body;
if ($url =~ /^file:\/\/(.+)$/) {
my $filename = $1;
$body = decode_utf8 read_file($filename);
} else {
my $ua = Mojo::UserAgent->new;
my $tx = $ua->get($url);
unless ($tx->res->is_success) {
say STDERR "Failed to fetch: $url";
return;
}
$body = decode_utf8 $tx->res->body;
}
return $body;
}
sub fetch_and_print {
my ($url, $opts) = @_;
my $body = fetch($url);
my $xml = XML::Loy->new($body);
my @rows;
# rss
$xml->find("item")->each(
sub {
my $el = $_;
push @rows, {
title => $el->at("title")->text,
link => $el->at("link")->text,
};
}
);
# atom
$xml->find("entry")->each(
sub {
my $el = $_;
my %o = (
title => $el->at("title") // '',
);
if (my $link_el = $el->at("link")) {
$o{link} = $link_el->attr("href");
}
for my $k (keys %o) {
$o{$k} = $o{$k}->text if ref($o{$k});
$o{$k} =~ s/\s+/ /g;
}
push @rows, \%o;
}
);
for my $row (@rows) {
say encode_utf8( join "\t", @$row{@{$opts->{fields}}} );
}
}
## main
my %opts;
GetOptions(
\%opts,
"fields=s",
);
if ($opts{fields}) {
$opts{fields} = [split /,/ => $opts{fields}];
} else {
$opts{fields} = [ "link", "title" ];
}
for my $feed_url (@ARGV) {
fetch_and_print($feed_url, \%opts);
}