RSS Generation

If your site includes regular updates or news items, you may want to add an RSS or ATOM feed to allow users who use news readers to track your updates.

CL-HTTP includes built-in support for the older of these two standards, RSS, and the following example shows how to add an RSS feed to your site.

First here's the routine to generate the feed:

(defmethod write-rss-feed ((url url:http-url) stream)
  (with-successful-response (stream '(:text :xml))
    (rss2.0::with-rss-document (:stream stream)
      (rss2.0::with-rss-channel
          ("A CL-HTTP Primer" 
           "http://clhttp.plasticki.com/"
           "Information about the Common Lisp Web Server CL-HTTP"
           :copyright "Copyright (c) 2014 David Johnson-Davies"
           :publication-date (encode-universal-time 0 0 0 3 3 2014)
           :build-date (encode-universal-time 0 0 0 3 3 2014)
           :generator "CL-HTTP RSS"
           :stream stream)
        (rss2.0::declare-rss-item 
         stream
         :title "RSS Generation"
         :author "david@interface.co.uk (David Johnson-Davies)" ; optional
         :publication-date (encode-universal-time 0 0 0 3 3 2014)
         :unique-id "http://clhttp.plasticki.com/show?KO6"
         :description #'write-feed-item)))))

The feed has one channel, "A CL-HTTP Primer", and one item, the introduction to this page.

Finally, here's the routine to generate the feed content:

(defun write-feed-item (stream)
  (with-paragraph (:stream stream)
    (fast-format 
     stream
     "If your site includes regular updates or news items, ~
      you may want to add an RSS or ATOM feed to allow users who use news readers to track your updates."))
  (with-paragraph (:stream stream)
    (fast-format 
     stream
     "CL-HTTP includes built-in support for the older of these two standards, ~
      RSS, and the following example shows how to add an RSS feed to your site."))) 

Here's the routine to export the feed URL:

(export-url "http://localhost:8000/rss"
            :computed
            :response-function #'write-rss-feed
            :expiration `(:interval ,(* 15. 60.))
            :public t)

You can then subscribe to the feed at:

http://localhost:8000/rss

It will appear in your favourite news reader something like this:

Leaf.png

Finally, you might want to declare your RSS feed on your home page with a statement like:

(declare-link :reference "http://clhttp.plasticki.com/rss" 
              :relation "alternate" :title "Mazelog - RSS" 
              :media-type "application/rss+xml" :stream stream)

in the with-document-preamble section.

By the way, I recommend you validate your feeds at:

http://validator.w3.org/feed/

to make sure they'll be accepted by all news readers.


blog comments powered by Disqus