Reddit API get comments as they come

Viewed 516

I have a function that takes in a Reddit comment and spits out a reply. I want this function to map over every new comment posted on Reddit.

How should I get the comments from reddit? It seems a bit wasteful to just pull in the 100 most recent comments very often so none are missed. A way to get all comments newer than a specific comment be very good.

Ideally the comments would just be a lazy list so I just could do

f :: Comment -> IO ()

getComments :: IO [Comment]

main :: IO ()
main = do
  comments <- getComments
  traverse_ f comments

Or I could write an interact function:

f :: Comment -> Comment

interact :: (Comment -> Comment) -> IO ()

main :: IO ()
main = interact f
1 Answers

The comments endpoint has a query parameter before. A request with this query parameter set to the name of any comment will return only the comments which are newer than that comment.

Note that the name of a comment is not its link_id, which is what I was stuck on.

For example, a request to www.reddit.com/r/foobar/comments.json?sort=new&before=t1_e9eud8s will return a listing of the comments that were posted after the comment with name: t1_e9eud8s

Related