| 1 | package Wiki::Toolkit::Plugin::RSS::Reader; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use vars qw( $VERSION @ISA ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | $VERSION = '1.6'; | ||
| 8 | @ISA = qw(Wiki::Toolkit::Plugin); | ||
| 9 | |||
| 10 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use LWP::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use XML::RSS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | my $feed; | ||
| 15 | my $rss = XML::RSS->new; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my $class = shift; | ||
| 20 | my %params = @_; | ||
| 21 | my $self = {}; | ||
| 22 | bless $self, $class; | ||
| 23 | |||
| 24 | return $self->_init(%params); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _init | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | my %params = @_; | ||
| 31 | |||
| 32 | return unless $params{url} || $params{file}; | ||
| 33 | croak "'url' and 'file' cannot both be specified" if $params{url} && $params{file}; | ||
| 34 | |||
| 35 | $self->{_url} = $params{url} if $params{url}; | ||
| 36 | $self->{_file} = $params{file} if $params{file}; | ||
| 37 | $self->{_debug} = 1 if $params{debug} && $params{debug} == 1; | ||
| 38 | |||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub retrieve | ||
| 43 | { | ||
| 44 | my $self = shift; | ||
| 45 | my $content; | ||
| 46 | |||
| 47 | # Retrieve the RSS from the Net or open a local | ||
| 48 | # file depending on how we were invoked. | ||
| 49 | |||
| 50 | if ($self->{_url}) | ||
| 51 | { | ||
| 52 | $content = get($self->{_url}); | ||
| 53 | } | ||
| 54 | else | ||
| 55 | { | ||
| 56 | if (open RSS, $self->{_file}) | ||
| 57 | { | ||
| 58 | $content .= $_ while <RSS>; | ||
| 59 | close RSS; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | my $location; | ||
| 64 | if ($self->{_url}) | ||
| 65 | { | ||
| 66 | $location = $self->{_url}; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | $location = $self->{_file}; | ||
| 71 | } | ||
| 72 | |||
| 73 | # If we couldn't get the RSS, fail silently or not? | ||
| 74 | if (!defined $content) | ||
| 75 | { | ||
| 76 | return unless $self->{_debug}; | ||
| 77 | croak "Couldn't retrieve RSS from [$location]: $!"; | ||
| 78 | } | ||
| 79 | |||
| 80 | my @rss_items; | ||
| 81 | |||
| 82 | $rss->parse($content); | ||
| 83 | |||
| 84 | foreach (@{$rss->{'items'}}) | ||
| 85 | { | ||
| 86 | my $link; | ||
| 87 | |||
| 88 | # RSS 2.0 has GUIDs, which may or may not be the item's URL. Read | ||
| 89 | # http://diveintomark.org/archives/2004/02/04/incompatible-rss | ||
| 90 | # and weep. May I take the soapbox for a moment here and state | ||
| 91 | # publically that I think Dave Winer sucks? Thank you. | ||
| 92 | |||
| 93 | if ($_->{guid} && $_->{link}) | ||
| 94 | { | ||
| 95 | $link = $_->{link}; | ||
| 96 | } | ||
| 97 | elsif ($_->{guid}) | ||
| 98 | { | ||
| 99 | $link = $_->{guid}; | ||
| 100 | } | ||
| 101 | else | ||
| 102 | { | ||
| 103 | $link = $_->{link}; | ||
| 104 | } | ||
| 105 | |||
| 106 | push @rss_items, { | ||
| 107 | title => $_->{title}, | ||
| 108 | link => $link, | ||
| 109 | description => $_->{description}, | ||
| 110 | }; | ||
| 111 | } | ||
| 112 | |||
| 113 | return @rss_items; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ | ||