File: check_feed.pl

package info (click to toggle)
libxml-feed-perl 0.61%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 500 kB
  • sloc: perl: 1,137; xml: 682; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,356 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use v5.10;

=head1 DESCRIPTION

Given a URL of an Atom or RSS feed or a filename of an already downloaded
feed, this script will try to parse it and print out what it understands
from the feed.

=cut

use XML::Feed;

my $src = shift;

die "Usage: $0 FILE|URL\n" if not $src;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';

my $source = $src;
if ($src =~ m{^https?://}) {
  $source = URI->new($src);
} else {
  if (not -f $source) {
    die "'$source' does not look like a URL and it does not exist on the file-system either.\n";
  }
}

my @feed_attrs  = qw[Title Tagline Format Author Link Base
                     Language Copyright Modified Generator];

my @entry_attrs = qw[Link Author Title Category Id Issued Modified
                     Lat Long Format Tags Enclosure Summary Content];

my %use_body_attrs = map { $_ => 1 } qw[Summary Content];

my $feed = XML::Feed->parse( $source ) or die XML::Feed->errstr;

for (@feed_attrs) {
  my $method = lc $_;
  printf "%-11s %s\n", "$_:", ($feed->$method // '');
}

for my $entry ($feed->entries) {
  say '';

  for (@entry_attrs) {
    my $method = lc $_;
    my $data;
    if ($use_body_attrs{$_}) {
      $data = $entry->$method->body // '';
    } else {
      $data = $entry->$method // '';
    }

    printf(" * %-11s %s\n", "$_:", $data);
  }
}