#!/usr/bin/perl # use cPanelUserConfig; #for cpanel servers use 5.010; use strict; use warnings; ################################### # # blankRSS.pl # # This script pulls from a list of RSS feeds and agregates them together into a web page. # It is designed to run as a cron and overwrite the HTML file. # # license GPLv3.0 https://www.gnu.org/licenses/gpl-3.0.en.html # Code repository: https://code.jacobhaddon.com/jake/smhn # Written by Jacob Haddon https://jacobhaddon.com # ################################### # Packages use Time::Piece; # https://perldoc.perl.org/Time::Piece use Time::Seconds; # https://perldoc.perl.org/Time::Seconds use LWP::Simple; # https://metacpan.org/pod/LWP::Simple use XML::RSS; # https://metacpan.org/pod/XML::RSS use HTML::Entities; # https://metacpan.org/pod/HTML::Entities # server file folders # my $rssFilePath = "/home/USER_FOLDER/public_html/feed.xml"; # my $htmlFilePath = "/home/USER_FOLDER/public_html/index.html"; # my $errorFilePath = "/home/USER_FOLDER/public_html/feed.log"; # local file folders my $rssFilePath = "feed.xml"; my $htmlFilePath = "index.html"; my $errorFilePath = "feed.log"; ################################### # RSS Configurations ################################### my $title = "The Title of My Site"; my $homeLink = "http://example.com"; my $feedLink = "http://example/feed.xml"; my $description = 'A description of my feed, it should be one line in length.'; my $webmaster = 'webmaster@example.com'; my $copyright = 'Copyright respective writers'; ################################### # Go through list of URLs, get RSS feed, # take newest 3 that are less than $then old, # add to new RSS feed object ################################### # number of weeks in the past to hold RSS feed my $num_weeks = 2; # get today, subtact time to make cut off my $now = localtime; my $then = $now - (ONE_WEEK * $num_weeks); #number of items to keep from each feed my $number_of_items = 2; # +1 since everything starts at 0 #list to hold the new RSS items my %list; # Make the list of URLS while parsing DATA my $listHTML = "
\n"; ################################### # Write the error file ################################### open(FH, '>', $errorFilePath) or die $!; print FH $listURLError; close(FH); ################################### # Make an RSS Feed! ################################### # date format: Thu, 28 Dec 2023 03:51:42 # $now->strftime("%a, %d %b %Y %H:%M:%S %z"); my $rss2 = XML::RSS->new (version => '2.0'); $rss2->add_module(prefix => 'atom', uri => 'http://www.w3.org/2005/Atom'); $rss2->channel(title => $title, link => $homeLink, language => 'en-US', description => $description, copyright => $copyright, pubDate => $now->strftime('%a, %d %b %Y %H:%M:%S %z'), lastBuildDate => $now->strftime('%a, %d %b %Y %H:%M:%S %z'), webMaster => $webmaster, atom => { 'link' => { 'href' => $feedLink, 'rel' => 'self', 'type' => 'application/rss+xml' } } ); # $rss->channel # foreach ITEM, newest (highest EPOCH) first foreach my $name (reverse sort keys %list) { $rss2->add_item(title => $list{$name}->{'title'}, permaLink => $list{$name}->{'link'}, link => $list{$name}->{'link'}, description => $list{$name}->{'description'}, pubDate => $list{$name}->{'pubDate'}, author => $list{$name}->{'itemAuthor'}, source => $list{$name}->{'feedName'}, sourceUrl => $list{$name}->{'feedURL'}, ); # $rss->channel } # foreach # Save the RSS feed as a file $rss2->save($rssFilePath); ################################### # Format the RSS data for HTML ################################### # make the HTML for the processed RSS my $rssHTML = "
\n"; # print the title and link of each RSS item foreach my $item (@{$rss2->{'items'}}) { $rssHTML .= "

" . encode_entities($item->{'title'}) . "

\n\n"; $rssHTML .= "
" . encode_entities($item->{'author'}) . " - " . formatDate($item->{'pubDate'}) ."
\n\n"; $rssHTML .= "
" . $item->{'description'} . "
\n\n"; } # foreach item # close out the rssHTML $rssHTML .= "
\n\n"; # the webpage HTML # format the pubDate my $printDate = formatDate($rss2->{'channel'}{'pubDate'}); # header for a direct HTML post my $html_header = "Status: 200\nContent-type: text/html\n\n"; ################################### # Make the HTML Page ################################### my $html = <<"HTML_END"; $title

$title

$description

updated: $printDate

The News!

$rssHTML

About $title

$description

Contact

Contact $title at: $webmaster

The List!

This is the list of the feeds that we are checking. If you have an RSS reader, grab them and follow along!

$listHTML


HTML_END # write the file open(FH, '>', $htmlFilePath) or die $!; print FH $html; close(FH); # print $html_header . $html; ################################### # Functions ################################### sub formatDate { my $testDate = $_[0]; $testDate =~ s/((\+|\-)(\d\d\d\d))//; # Convert to a TIME object my $t = Time::Piece->strptime($testDate); return($t->strftime('%a, %d %b %Y %H:%M:%S')); } # FIN ################################### # DATA is list of the feed URLs ################################### __DATA__ https://nnw.ranchero.com/feed.json https://feeds.npr.org/1001/rss.xml