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