@@ -267,21 +267,52 @@ def ts(item):
267267 ts (item ))
268268 for item in items ]
269269
270- def service_lw (token ):
270+ def lw_style_service (token , service , domain ):
271+ if token .startswith ('posts/' ):
272+ token = token [len ('posts/' ):]
273+
271274 m = re .match ('^[a-zA-Z0-9]+$' , token )
272275 if not m :
273276 return []
274277
275- url = "http://lesswrong.com/lw/%s.rss" % token
276- return pull_reddit_style_rss (url )
278+ response = json .loads (slurp ("%s/graphql" % domain , data = '{"query": "{comments(input: { terms: { view: \\ "postCommentsOld\\ ", postId: \\ "%s\\ ", }}) { results { _id postedAt author parentCommentId contents { html }}}}"}' % token , headers = {'Content-Type' : 'application/json' }))
279+
280+ comments = {}
281+ for comment in response ['data' ]['comments' ]['results' ]:
282+ # output format:
283+ # username
284+ # permalink
285+ # comment id (derived from link)
286+ # comment_html
287+ # timestamp
288+ # children
289+ # parent_id (temporary)
290+ comments [comment ["_id" ]] = [
291+ comment ["author" ],
292+ "%s/posts/%s#%s" % (domain , token , comment ["_id" ]),
293+ "%s-%s" % (service , comment ["_id" ]),
294+ comment ["contents" ]["html" ],
295+ epoch (comment ["postedAt" ]),
296+ [],
297+ comment ["parentCommentId" ]]
298+ root = []
299+ for comment_id , comment in comments .items ():
300+ parent_id = comment [- 1 ]
301+ if parent_id :
302+ parent = comments [parent_id ][- 2 ] # -2 is children
303+ else :
304+ parent = root
305+ parent .append (comment )
306+ for comment in comments .values ():
307+ del comment [- 1 ] # remove temporary parent_id
308+ return root
277309
278- def service_ea (token ):
279- m = re .match ('^[a-zA-Z0-9]+$' , token )
280- if not m :
281- return []
282310
283- url = "http://effective-altruism.com/ea/%s.rss" % token
284- return pull_reddit_style_rss (url )
311+ def service_lw (token ):
312+ return lw_style_service (token , 'lw' , 'https://www.lesswrong.com' )
313+
314+ def service_ea (token ):
315+ return lw_style_service (token , 'ea' , 'https://forum.effectivealtruism.org' )
285316
286317def service_r (token ):
287318 m = re .match ('^[a-zA-Z0-9]+/[a-zA-Z0-9]+$' , token )
0 commit comments