Skip to content

Instantly share code, notes, and snippets.

@ponzao
Created February 20, 2011 23:06
Show Gist options
  • Save ponzao/836407 to your computer and use it in GitHub Desktop.
Save ponzao/836407 to your computer and use it in GitHub Desktop.
An RSS reader ported from Scala http://www.virtuousprogrammer.com/?p=159.
(ns rss-reader
(:import [java.io FileReader
BufferedReader])
(:require [clojure.xml :as xml]
[clojure.zip :as zip])
(:use [clojure.contrib.zip-filter.xml]
[hiccup.core]))
(defn print-feeds [feeds]
(doseq [[channel articles] feeds]
(println "***" channel "***")
(doseq [title articles]
(println "\t" title))))
(defn write-feeds [feeds]
(spit "rss.html"
(html [:html
[:head [:title "RSS Feeds"]]
[:body
(for [[channel articles] feeds]
[:article
[:h2 channel]
[:ul
(for [title articles]
[:li title])]])]])))
(defn get-file-lines [file-name]
(with-open [rdr (BufferedReader.
(FileReader. file-name))]
(doall (line-seq rdr))))
(defn- zip-string [s]
(zip/xml-zip (xml/parse (java.io.ByteArrayInputStream.
(.getBytes s "UTF-8")))))
(defn extract-rss [url]
(let [xz (zip-string (slurp url))]
(for [channel (xml-> xz :channel)]
(list (xml1-> channel :title text)
(xml-> channel :item :title text)))))
(defn combine-feeds [feeds]
(letfn [(combine-feed [feed-name]
(->> (filter #(= (first %) feed-name) feeds)
(map second)
flatten))]
(map
#(list % (combine-feed %))
(distinct (map first feeds)))))
(defn get-rss-feeds [file-name]
(->> (get-file-lines file-name)
(map extract-rss)
(reduce
concat
(list))))
(def feeds (-> "feeds.txt"
get-rss-feeds
combine-feeds))
(print-feeds feeds)
(write-feeds feeds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment