Created
August 4, 2012 23:01
-
-
Save yogthos/3260456 to your computer and use it in GitHub Desktop.
RSS in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns rss | |
(:use [clojure.xml :only [emit]]) | |
(:import java.util.Date)) | |
(defn format-time [time] | |
(.format (new java.text.SimpleDateFormat | |
"EEE, dd MMM yyyy HH:mm:ss ZZZZ") time)) | |
(defmacro tag [id attrs & content] | |
`{:tag ~id :attrs ~attrs :content [~@content]}) | |
(defn item [site author {:keys [id title content time]}] | |
(let [link (str site "/" id )] | |
(tag :item nil | |
(tag :guid nil link) | |
(tag :title nil title) | |
(tag :dc:creator nil author) | |
(tag :description nil content) | |
(tag :link nil link) | |
(tag :pubDate nil (format-time time)) | |
(tag :category nil "clojure")))) | |
(defn message [site title author posts] | |
(let [date (format-time (new Date))] | |
(tag :rss {:version "2.0" | |
:xmlns:dc "http://purl.org/dc/elements/1.1/" | |
:xmlns:sy "http://purl.org/rss/1.0/modules/syndication/"} | |
(update-in | |
(tag :channel nil | |
(tag :title nil (or (:title (first posts)) "")) | |
(tag :description nil title) | |
(tag :link nil site) | |
(tag :lastBuildDate nil date) | |
(tag :dc:creator nil author) | |
(tag :language nil "en-US") | |
(tag :sy:updatePeriod nil "hourly") | |
(tag :sy:updateFrequency nil "1")) | |
[:content] | |
into (map (partial item site author) posts))))) | |
(defn rss-feed [site title author posts] | |
(with-out-str (emit (message site title author posts)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment