| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use v5.10; |
| 4 |
|
| 5 |
=head1 DESCRIPTION |
| 6 |
|
| 7 |
Given a URL of an Atom or RSS feed or a filename of an already downloaded |
| 8 |
feed, this script will try to parse it and print out what it understands |
| 9 |
from the feed. |
| 10 |
|
| 11 |
=cut |
| 12 |
|
| 13 |
use XML::Feed; |
| 14 |
|
| 15 |
my $src = shift; |
| 16 |
|
| 17 |
die "Usage: $0 FILE|URL\n" if not $src; |
| 18 |
binmode STDOUT, ':encoding(UTF-8)'; |
| 19 |
binmode STDERR, ':encoding(UTF-8)'; |
| 20 |
|
| 21 |
my $source = $src; |
| 22 |
if ($src =~ m{^https?://}) { |
| 23 |
$source = URI->new($src); |
| 24 |
} else { |
| 25 |
if (not -f $source) { |
| 26 |
die "'$source' does not look like a URL and it does not exist on the file-system either.\n"; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
my @feed_attrs = qw[Title Tagline Format Author Link Base |
| 31 |
Language Copyright Modified Generator]; |
| 32 |
|
| 33 |
my @entry_attrs = qw[Link Author Title Category Id Issued Modified |
| 34 |
Lat Long Format Tags Enclosure Summary Content]; |
| 35 |
|
| 36 |
my %use_body_attrs = map { $_ => 1 } qw[Summary Content]; |
| 37 |
|
| 38 |
my $feed = XML::Feed->parse( $source ) or die XML::Feed->errstr; |
| 39 |
|
| 40 |
for (@feed_attrs) { |
| 41 |
my $method = lc $_; |
| 42 |
printf "%-11s %s\n", "$_:", ($feed->$method // ''); |
| 43 |
} |
| 44 |
|
| 45 |
for my $entry ($feed->entries) { |
| 46 |
say ''; |
| 47 |
|
| 48 |
for (@entry_attrs) { |
| 49 |
say get_attribute($entry, $_); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
sub get_attribute { |
| 54 |
my ($entry, $attr) = @_; |
| 55 |
|
| 56 |
my $method = lc $attr; |
| 57 |
my $data; |
| 58 |
if ($use_body_attrs{$attr}) { |
| 59 |
$data = $entry->$method->body // ''; |
| 60 |
} else { |
| 61 |
$data = $entry->$method // ''; |
| 62 |
} |
| 63 |
|
| 64 |
return sprintf(" * %-11s %s", "$attr:", $data); |
| 65 |
} |