I have managed to create a function that will read an ATOM feed 
indefinitely and now need a way to stop it ;-)

(defn fetch-atom-chunk
  "Returns an ATOM chunk from the feed-url"
  [feed-url]
  (:ATOM_FEED (:body (client/get feed-url {:as :json}))))

(defn fetch-atom-feed
  "Returns a list of ATOM chunks from the feed-url going back to the 
from-date"
  [feed-url]
  (let [chunk (fetch-atom-chunk feed-url)]
    (lazy-seq
      (cons chunk (fetch-atom-feed (:HREF (:PREVIOUS_LINK chunk)))))))

(defn after?
  "Returns true if first-date is after second-date"
  [first-date second-date]
  (> (.getTime first-date) (.getTime second-date)))

(defn feed-date-to-clj-date
  "Patch for ATOM feed date formats like '2013-05-27T14:35:00.982+02:00'
   we have to remove the final ':' from the string to enable its conversion"
  [feed-date]
  (.parse (java.text.SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    (str/replace feed-date #"(.*)(\d\d):(\d\d)" "$1$2$3")))

(defn more-feed-chunks-needed?
  "Look at the atom chunk and decide if we have enough"
  [from-date atom-chunk]
  (after? from-date (feed-date-to-clj-date (:UPDATED atom-chunk))))

(def date-from (.parse (java.text.SimpleDateFormat. "yyyy-MM-dd") 
"2013-05-27"))

(take-while #(more-feed-chunks-needed? date-from %) (fetch-atom-feed 
"some-url"))

; example of the feed
; {:FEED_ID "5650f3ad-b793-42c6-9fef-80ba105f847d", :TITLE "Delta Feed", 
:UPDATED "2013-05-28T11:15:34.860+02:00", :GENERATOR "Products", :LINK 
{:REL "Self", :HREF "http://some-url"}, :PREVIOUS_LINK {:REL 
"prev-archive", :HREF "http://another-url"}, .....

If I run without the take-while it runs forever (so I am happy that its 
generating data) but when I add the take-while I get an empty list.

I guess I'm missing something simple and would welcome suggestions.

Thanks

Ray

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to