Revision: 4444 Author: hans URL: http://bknr.net/trac/changeset/4444
comments, rename some functions U trunk/projects/planetwit/planetwit.clj
Modified: trunk/projects/planetwit/planetwit.clj =================================================================== --- trunk/projects/planetwit/planetwit.clj 2009-08-20 00:56:01 UTC (rev 4443) +++ trunk/projects/planetwit/planetwit.clj 2009-08-20 11:44:49 UTC (rev 4444) @@ -1,3 +1,14 @@ +;;; planet.lisp.org -> twitter gateway +;;; +;;; Copyright 2008/2009 Hans Huebner + +;;; Generate a twitter status update with the item title and the +;;; permalink URL shortened by tinyurl.com for each new item posted to +;;; planet.lisp.org. + +;;; The guids of all items that has been forwarded to twitter are +;;; stored as a literal set in the text file +state-file+. + (ns planetwit (:require [clojure.zip :as zip] [clojure.xml :as xml]) @@ -23,10 +34,10 @@ (defn write-file [data file-name] (spit file-name (with-out-str (pr data))))
-(defn load-data [] +(defn load-data-from-file [] (read-file +state-file+ #{}))
-(defn save-data [data] +(defn save-data-to-file [data] (write-file data +state-file+))
(defn feed-to-zip [url] @@ -42,7 +53,11 @@ (catch java.io.FileNotFoundException _ (println (format "could not update twitter status (no auth file):\n%s\n" status)))))
-(defn post-twits [items] +(defn post-twits + "Given a list of new item hashes in ITEMS, send a twitter status + update for each of them consisting of the :title string and a tiny + URL pointing to the item itself." + [items] (when items (let [item (first items)] (update-twitter-status @@ -54,16 +69,23 @@ nil)))) (recur (rest items)))))
+(defn load-feed + "Load planet.lisp.org feed, return a hash for every item containing + the item permalink under the :guid key and the item's title under + the :title key" + [] + (map (fn [item] + {:guid (first (xml-> item :guid text)) + :title (first (xml-> item :title text))}) + (xml-> (feed-to-zip "http://planet.lisp.org/rss20.xml") + :channel :item))) + (defn poll "Poll planet lisp, check for new postings, update Twitter status when new postings have appeared" [] - (let [old-urls (load-data) - all-items (map (fn [item] - {:guid (first (xml-> item :guid text)) - :title (first (xml-> item :title text))}) - (xml-> (feed-to-zip "http://planet.lisp.org/rss20.xml") - :channel :item)) + (let [old-urls (load-data-from-file) + all-items (load-feed) new-items (filter (complement #(old-urls (:guid %))) all-items)] (post-twits new-items) - (save-data (into #{} (map #(:guid %) all-items))))) + (save-data-to-file (into #{} (map #(:guid %) all-items)))))