Adding date support to my awful RSS feed generator

2020-10-27 00:00

So, someone asked “when are you going to add dates to your RSS feed?”, and I replied “I’m not actually sure!”. I wasn’t sure, because my original goal was to create a bare-minimum, valid RSS feed, which doesn’t actually require dates.

This went well and all, but it kind of messes with people’s RSS feed readers because it doesn’t know how to order them, so it adds dates in for them, which are all just the same date.

I created this generator so friends could keep up with my blog posts, so it kind of made sense to me that they should be able to view them in chronological order in their RSS feed reader haha.

Tonight I set out to do that, and, since my RSS-feed generator was already a mess, I had a lot of fun with this, because I didn’t need to worry about ruining nicely organized code.

I wasn’t sure how I was going to do it, because I was basically generating links and titles in the RSS feed based off the posts.md file, which consisted of a bunch of * [I'm a title](im-a-link.md)s, and just parsed those into RSS-style XML for the <title>, <link>, and <guid>. The problem was that there was no date for me to pull from there!

I realized I was keeping dates in all of my posts, and that I could just take the link my crappy parser found, remove the initial / from it, and it could open the file, split it into lines, grab the third line, which was the date, and plop it into the RSS!

It took a few tries, some referencing of an IRC bot I made to get the output of a shell command from racket, but in the end, I managed to do it!

I ended up using an awful nested list of functions, because, why not? The code is already awful, why not have more fun and keep it that way?

The result? This super awful function, and I love it!:

(define (listof-rss-items->rss-items-string listof-rss-items)
  (format "~a" (string-join listof-rss-items)))

(define (create-item l)
  (let* ([post-title     (car l)]
         [link-fragment  (car (cdr l))] ;; /posts/some-name.html
         [date           (car
                           (cdr
                             (cdr
                               (file->lines
                                 (string-replace (string-replace link-fragment ".html" ".md")
                                                 "/posts/"
                                                 "posts/")))))] ;; creates "yyyy-mm-dd"
         [date-converted (string-trim
                           (with-output-to-string
                             (lambda () (system (format "date -Rd'~a'" date)))))] ;; creates "Day, Mon 31 00:00:00 EDT 2020"
         [link-full      (string-append homepage-url link-fragment)])
    (format
#<<string-block

  <item>
    <title>~a</title>
    <link>~a</link>
    <guid>~a</guid>
    <pubDate>~a</pubDate>
  </item>

string-block
post-title
link-full
link-full
date-converted)))

P.S. If you are following my RSS feed, to get it to regenerate with dates, you might need to delete my feed and reload it. I know I had to do this with Thunderbird while I was testing my feed.