Hi Clojurians,
I have a program which generates a lazy sequence to read ATOM feed chunks.
That part works OK but now I want to only take a limited number of the
chunks based on some predicate. My predicate is currently returning an
empty list so I think (hope!) I am missing something simple.
Here is an excerpt from the feed (yes the feed is in JSON and I have used
cheshire to map to Clojure data):
{: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"}, .....
Here is the sequence generator:
(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 from-date] (fetch-atom-feed feed-url from-date ())
[feed-url]
(let [chunk (fetch-atom-chunk feed-url)]
(lazy-seq
(cons chunk (fetch-atom-feed (:HREF (:PREVIOUS_LINK chunk)))))))
Here is the predicate:
(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 after?
"Returns true if first-date is after second-date"
[first-date second-date]
(> (.getTime first-date) (.getTime second-date)))
(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))))
And here is how I run it forever:
(fetch-atom-feed "https://producta.blah.com/feed/current")
And here is how I run it with a predicate:
(def date-from (.parse (java.text.SimpleDateFormat. "yyyy-MM-dd")
"2013-05-27"))
(take-while #(more-feed-chunks-needed? date-from %) (fetch-atom-feed
"https://producta.blah.com/feed/current"))
Maybe this is too detailed for the group (do let me know) but otherwise I
would appreciate your advice / tips on what I could try.
Thanks
Ray
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.