orrg

online rss & atom feed reader for gemini
git clone https://git.clttr.info/orrg.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit 5a95a2973524305024fb2bd775b447737073d1d5
parent 08a67a7b51c33a348137b4e6b5dc2366e9f90e47
Author: René Wagner <rwagner@rw-net.de>
Date:   Mon, 22 Feb 2021 20:30:16 +0100

use wget instead of built-in Perl LWP

improve handling of valid requests or parsing
attempts
closes #5
closes #7
closes #6

Diffstat:
MREADME.md | 1+
Morrg.pl | 54+++++++++++++++++++++++++++++++++---------------------
2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/README.md b/README.md @@ -40,3 +40,4 @@ Given this restrictions is not suitable for high traffic feeds which are updated - DateTime::Format::ISO8601 - HTML::Strip - Python 3 for `gcat` +- wget diff --git a/orrg.pl b/orrg.pl @@ -38,21 +38,28 @@ sub create_response my ( $qs ) = @_; my @body = (); - my $feed = feed_get($qs); - if ( !defined($feed) ) { + my $content = feed_get($qs); + if ( !defined($content) || $content eq '' ) { push @body, ('# orrg error', '', 'The requested feed could not be loaded. :(', '', '=> '. $qs .' open feed in browser'); - return @body; + } else { + my $feed = feed_parse($content); + + if (defined($feed)) { + recent_add($qs, trim_ws($feed->title)); + popular_add($qs, trim_ws($feed->title)); + + push @body, '# '. trim_ws($feed->title); + push @body, 'fetched '. strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()); + push @body, ''; + $feed->description eq '' or push @body, trim_ws($feed->description); + $feed->image eq '' or push @body, '=> '. $feed->image .' feed image'; + $feed->link eq '' or push @body, ('=> '.$feed->link.' open website', ''); + + foreach my $it ($feed->get_item()) { push @body, @{item($it)}; } + } else { + push @body, ('# orrg error', '', 'The requested feed could be loaded but not parsed. :(', '', '=> '. $qs .' open feed in native client'); + } } - - push @body, '# '. trim_ws($feed->title); - push @body, 'fetched '. strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()); - push @body, ''; - $feed->description eq '' or push @body, trim_ws($feed->description); - $feed->image eq '' or push @body, '=> '. $feed->image .' feed image'; - $feed->link eq '' or push @body, ('=> '.$feed->link.' open website', ''); - - foreach my $it ($feed->get_item()) { push @body, @{item($it)}; } - push @body, ('', '', '=> ./index.pl [home]'); return @body; } @@ -101,17 +108,22 @@ sub feed_get { my ( $query ) = @_; - my $feed; - if ( $query =~ /^https\:\/\// ) { $feed = XML::FeedPP->new($query, utf8_flag => 1); } + my $content; + if ( $query =~ /^https\:\/\// ) { $content = `wget -qO - $query`; } if ( $query =~ /^gemini\:\/\// ) { - my $content = `./gcat $query`; + $content = `./gcat $query`; $content =~ /20\W/ or return undef; $content =~ s/^[0-9]{0,2}\W.+\r\n//; - $feed = XML::FeedPP->new($content, -type => 'string', utf8_flag => 1); } - - recent_add($query, $feed->title); - popular_add($query, $feed->title); - $feed->sort_item(); + + return $content; +} + +sub feed_parse +{ + my ($content) = @_; + + my $feed = XML::FeedPP->new($content, -type => 'string', utf8_flag => 1); + if (defined($feed)) { $feed->sort_item(); } return $feed; }