Feed Generator

home

no hurry, no pause.

Last update: 2024-05-11

I asked ChatGPT 3.5 to write a script that generates a feed.rss file. After some editing and adjusting it worked quite well and I have now included an rss link to this website.

Here’s the code:

#!/bin/bash

# Define the output file for the RSS feed
OUTPUT_FILE="feed.rss"

# Set the variables
TITLE="Your Title"
DESCRIPTION="Some description"
LINK="Your URL"
LANGUAGE="en-ca"
DATE=$(date +"%a, %d %b %Y %T %z") # Current date and time in RFC-822 format
GENERATOR="Custom RSS Generator"

# Find the last 10 HTML files in the root directory
ROOT_DIR="/Users/name/directory/directory"
HTML_FILES=$(ls -1t "$ROOT_DIR"/*.html 2>/dev/null | head -n 10)

# Start writing the RSS feed  
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<rss version=\"2.0\">
<channel>
<title>$TITLE</title>
<description>$DESCRIPTION</description>
<link>$LINK</link>
<language>$LANGUAGE</language>
<lastBuildDate>$DATE</lastBuildDate>
<generator>$GENERATOR</generator>" > "$OUTPUT_FILE"

# Loop through each HTML file and extract required fields
for FILE in $HTML_FILES; do
    MOD_TIME=$(date -R -r "$FILE")
    TITLE=$(grep -o '<title>[^<]*' "$FILE" | sed 's/<title>//')
    DESCRIPTION=$(grep -o '<meta name="description" content="[^"]*' "$FILE" | sed 's/<meta name="description" content="//')
    DESCRIPTION=${DESCRIPTION:-"Some description"}
    URL="your url/$(basename "$FILE")"
    guid="$URL"
    # Write item to RSS feed
    echo "<item>
    <pubDate>$MOD_TIME</pubDate>
    <title>$TITLE</title>
    <link>$URL</link>
    <description>$DESCRIPTION</description>
    <guid>$guid</guid>
    </item>" >> "$OUTPUT_FILE"
done

# Close the RSS feed
echo "</channel>
</rss>" >> "$OUTPUT_FILE"

echo "RSS feed generated successfully at $OUTPUT_FILE"