| 1 | # WebFetch::RSS | ||
| 2 | # ABSTRACT: generate or read an RSS feed for WebFetch | ||
| 3 | # | ||
| 4 | # Copyright (c) 1998-2022 Ian Kluft. This program is free software; you can | ||
| 5 | # redistribute it and/or modify it under the terms of the GNU General Public | ||
| 6 | # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
| 7 | |||
| 8 | # pragmas to silence some warnings from Perl::Critic | ||
| 9 | ## no critic (Modules::RequireExplicitPackage) | ||
| 10 | # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first | ||
| 11 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | ## use critic (Modules::RequireExplicitPackage) | ||
| 15 | |||
| 16 | package WebFetch::RSS; | ||
| 17 | $WebFetch::RSS::VERSION = '0.3.2'; | ||
| 18 | 2 | ▶ | use base "WebFetch"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 2 | ▶ | use if $] < 5.010, "version"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 2 | ▶ | use WebFetch v0.15.5; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 2 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 2 | ▶ | use Scalar::Util qw( blessed ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 0 | ▶ | use XML::RSS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 27 | |||
| 28 | #use Data::Dumper; # TODO remove after troubleshooting | ||
| 29 | use Exception::Class (); | ||
| 30 | |||
| 31 | # configuration parameters | ||
| 32 | Readonly::Scalar my $default_rss_version => "2.0"; | ||
| 33 | |||
| 34 | # no user-servicable parts beyond this point | ||
| 35 | |||
| 36 | # register capabilities with WebFetch | ||
| 37 | WebFetch->config( "Options", [qw(rss_config:s)] ); | ||
| 38 | WebFetch->config( "Usage", "--rss_config=filename" ); | ||
| 39 | __PACKAGE__->module_register( "input:rss", "output:rss" ); | ||
| 40 | |||
| 41 | # called from WebFetch main routine | ||
| 42 | sub fetch | ||
| 43 | { | ||
| 44 | my ($self) = @_; | ||
| 45 | |||
| 46 | # set up Webfetch Embedding API data | ||
| 47 | $self->data->add_fields( "pubDate", "title", "link", "category", "description", "author", "id" ); | ||
| 48 | |||
| 49 | # defined which fields match to which "well-known field names" | ||
| 50 | $self->data->add_wk_names( | ||
| 51 | "title" => "title", | ||
| 52 | "url" => "link", | ||
| 53 | "date" => "pubDate", | ||
| 54 | "summary" => "description", | ||
| 55 | "category" => "category", | ||
| 56 | "author" => "author", | ||
| 57 | "id" => "id", | ||
| 58 | ); | ||
| 59 | |||
| 60 | # parse data file | ||
| 61 | $self->parse_input(); | ||
| 62 | |||
| 63 | # return and let WebFetch handle the data | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | # extract a string value from a scalar/ref if possible | ||
| 68 | sub extract_value | ||
| 69 | { | ||
| 70 | my $thing = shift; | ||
| 71 | |||
| 72 | ( defined $thing ) or return; | ||
| 73 | if ( ref $thing ) { | ||
| 74 | if ( !blessed $thing ) { | ||
| 75 | |||
| 76 | # it's a HASH/ARRAY/etc ref | ||
| 77 | if ( ref $thing eq "HASH" ) { | ||
| 78 | |||
| 79 | # we need sub-hashes for module namespaces | ||
| 80 | return $thing; | ||
| 81 | } | ||
| 82 | |||
| 83 | # other refs are not useable here | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | if ( $thing->can("as_string") ) { | ||
| 87 | return $thing->as_string; | ||
| 88 | } | ||
| 89 | return; | ||
| 90 | } else { | ||
| 91 | $thing =~ s/\s+$//xs; | ||
| 92 | length $thing > 0 or return; | ||
| 93 | return $thing; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | # parse RSS feed into hash structure | ||
| 98 | sub parse_rss | ||
| 99 | { | ||
| 100 | my @args = @_; | ||
| 101 | my $version = $default_rss_version; | ||
| 102 | my %params; | ||
| 103 | if ( ref $args[0] eq "HASH" ) { | ||
| 104 | %params = %{ shift @args }; | ||
| 105 | } | ||
| 106 | my $text = shift @args; | ||
| 107 | my $rss = XML::RSS->new( version => $version ); | ||
| 108 | try { | ||
| 109 | $rss->parse($text); | ||
| 110 | } catch { | ||
| 111 | WebFetch::throw_network_get( error => "" . $_, client => $rss ); | ||
| 112 | }; | ||
| 113 | my ( %feed, @buckets ); | ||
| 114 | |||
| 115 | # copy RSS channel data to WebFetch feed data | ||
| 116 | if ( exists $rss->{channel} ) { | ||
| 117 | $feed{info} = $rss->{channel}; | ||
| 118 | } | ||
| 119 | |||
| 120 | # parse values from top of structure | ||
| 121 | foreach my $field ( keys %$rss ) { | ||
| 122 | if ( ref $rss->{$field} eq "HASH" ) { | ||
| 123 | push @buckets, $field; | ||
| 124 | } | ||
| 125 | my $value = extract_value( $rss->{$field} ); | ||
| 126 | ( defined $value ) or next; | ||
| 127 | $feed{$field} = $value; | ||
| 128 | } | ||
| 129 | |||
| 130 | # parse hashes, i.e. channel parameters, XML/RSS modeules, etc | ||
| 131 | foreach my $bucket (@buckets) { | ||
| 132 | ( exists $rss->{$bucket} ) or next; | ||
| 133 | $feed{$bucket} = {}; | ||
| 134 | foreach my $field ( keys %{ $rss->{$bucket} } ) { | ||
| 135 | my $value = extract_value( $rss->{$bucket}{$field} ); | ||
| 136 | ( defined $value ) or next; | ||
| 137 | $feed{$bucket}{$field} = $value; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | # parse each item from the news feed | ||
| 142 | $feed{items} = []; | ||
| 143 | foreach my $item ( @{ $rss->{items} } ) { | ||
| 144 | my $f_item = {}; | ||
| 145 | foreach my $field ( keys %$item ) { | ||
| 146 | my $value = extract_value( $item->{$field} ); | ||
| 147 | ( defined $value ) or next; | ||
| 148 | $f_item->{$field} = $value; | ||
| 149 | } | ||
| 150 | push @{ $feed{items} }, $f_item; | ||
| 151 | } | ||
| 152 | |||
| 153 | return \%feed; | ||
| 154 | } | ||
| 155 | |||
| 156 | # retrieve first of multiple keys found in a hash | ||
| 157 | # The keys should be given in order from highest to lowest priority. | ||
| 158 | # This handles various RSS feeds which may use original RSS field names or new Dublin Core (dc) synonyms. | ||
| 159 | sub get_first | ||
| 160 | { | ||
| 161 | my ( $hashref, @keys ) = @_; | ||
| 162 | my $result = ""; | ||
| 163 | foreach my $key (@keys) { | ||
| 164 | |||
| 165 | # search field alternatives in modules such as syndication(sy) or Dublin Core(dc) | ||
| 166 | if ( index( $key, ':' ) != -1 ) { | ||
| 167 | my ( $module, $field ) = split /:/x, $key, 2; | ||
| 168 | if ( exists $hashref->{$module}{$field} ) { | ||
| 169 | $result = $hashref->{$module}{$field}; | ||
| 170 | last; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | # search for key string | ||
| 175 | if ( exists $hashref->{$key} ) { | ||
| 176 | $result = $hashref->{$key}; | ||
| 177 | last; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | return $result; | ||
| 181 | } | ||
| 182 | |||
| 183 | # parse RSS input | ||
| 184 | sub parse_input | ||
| 185 | { | ||
| 186 | my $self = shift; | ||
| 187 | |||
| 188 | # parse data file | ||
| 189 | my $raw_rss = $self->get(); | ||
| 190 | my %params; | ||
| 191 | if ( exists $self->{rss_version} ) { | ||
| 192 | $params{version} = $self->{rss_version}; | ||
| 193 | } | ||
| 194 | my $feed = parse_rss( \%params, $$raw_rss ); | ||
| 195 | |||
| 196 | # copy channel info if present | ||
| 197 | if ( exists $feed->{info} ) { | ||
| 198 | $self->{data}{feed} = $feed->{info}; | ||
| 199 | } | ||
| 200 | |||
| 201 | # translate parsed RSS feed into the WebFetch Embedding API data table | ||
| 202 | foreach my $item ( @{ $feed->{items} } ) { | ||
| 203 | |||
| 204 | # save the data record | ||
| 205 | my $date = get_first( $item, qw(pubDate date dc:date) ); | ||
| 206 | my $title = get_first( $item, qw(title dc:title) ); | ||
| 207 | my $link = get_first( $item, qw(link dc:source) ); | ||
| 208 | my $category = get_first( $item, qw(category) ); | ||
| 209 | my $description = get_first( $item, qw(description dc:description) ); | ||
| 210 | my $author = get_first( $item, qw(author dc:creator) ); | ||
| 211 | my $id = get_first( $item, qw(id identifier dc:identifier) ); | ||
| 212 | $self->{data}->add_record( $date, $title, $link, $category, $description, $author, $id ); | ||
| 213 | } | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | # RSS-output format handler | ||
| 218 | sub fmt_handler_rss | ||
| 219 | { | ||
| 220 | my ( $self, $filename ) = @_; | ||
| 221 | |||
| 222 | # this is not implemented yet - you'll get an empty RSS structure from it right now | ||
| 223 | |||
| 224 | # generate RSS | ||
| 225 | # TODO remove Data::Dumper after troubleshooting | ||
| 226 | my $rss = XML::RSS->new( version => '2.0' ); | ||
| 227 | |||
| 228 | #WebFetch::debug "self: ".Dumper($self); | ||
| 229 | $self->raw_savable( $filename, $rss->as_string() ); | ||
| 230 | return 1; | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; | ||
| 234 | |||
| 235 | =pod | ||
| 236 | |||
| 237 | =encoding UTF-8 | ||
| 238 | |||
| 239 | =head1 NAME | ||
| 240 | |||
| 241 | WebFetch::RSS - generate or read an RSS feed for WebFetch | ||
| 242 | |||
| 243 | =head1 VERSION | ||
| 244 | |||
| 245 | version 0.3.2 | ||
| 246 | |||
| 247 | =head1 SYNOPSIS | ||
| 248 | |||
| 249 | In perl scripts: | ||
| 250 | |||
| 251 | C<use WebFetch::RSS;> | ||
| 252 | |||
| 253 | From the command line: | ||
| 254 | |||
| 255 | C<perl -w -MWebFetch::RSS -e "&fetch_main" -- --dir directory --source rss-feed-url [...output options...]> | ||
| 256 | |||
| 257 | or | ||
| 258 | |||
| 259 | C<perl -w -MWebFetch::RSS -e "&fetch_main" -- --dir directory [...input options...]> --dest_format=rss --dest=file | ||
| 260 | |||
| 261 | =head1 DESCRIPTION | ||
| 262 | |||
| 263 | This module reads news items from an RSS feed, or writes previously-fetched data to an RSS file. | ||
| 264 | |||
| 265 | For input it uses WebFetch's I<--source> parameter to specify the URL of an RSS feed or a local file | ||
| 266 | containing RSS XML text. | ||
| 267 | |||
| 268 | For output it uses WebFetch's I<--dest> parameter to specify the RSS output file. | ||
| 269 | (RSS output is not complete yet as of this release.) | ||
| 270 | |||
| 271 | =head1 RSS FORMAT | ||
| 272 | |||
| 273 | RSS is an XML format defined at http://www.rssboard.org/rss-specification | ||
| 274 | |||
| 275 | WebFetch::RSS uses Perl's XML::RSS to generate or parse RSS "Really Simple | ||
| 276 | Syndication" version 0.9, 0.91, 1.0 or 2.0, whichever is provided by the server. | ||
| 277 | An optional "--rss_version" command-line parameter or "rss_version" initialization parameter | ||
| 278 | can set the RSS version number for the parser. If not specified, it defaults to RSS 2.0. | ||
| 279 | |||
| 280 | =head1 SEE ALSO | ||
| 281 | |||
| 282 | L<WebFetch> | ||
| 283 | L<https://github.com/ikluft/WebFetch> | ||
| 284 | |||
| 285 | =head1 BUGS AND LIMITATIONS | ||
| 286 | |||
| 287 | Please report bugs via GitHub at L<https://github.com/ikluft/WebFetch/issues> | ||
| 288 | |||
| 289 | Patches and enhancements may be submitted via a pull request at L<https://github.com/ikluft/WebFetch/pulls> | ||
| 290 | |||
| 291 | =head1 AUTHOR | ||
| 292 | |||
| 293 | Ian Kluft <https://github.com/ikluft> | ||
| 294 | |||
| 295 | =head1 COPYRIGHT AND LICENSE | ||
| 296 | |||
| 297 | This software is Copyright (c) 1998-2022 by Ian Kluft. | ||
| 298 | |||
| 299 | This is free software, licensed under: | ||
| 300 | |||
| 301 | The GNU General Public License, Version 3, June 2007 | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | __END__ | ||