Comment replies broken
npomfret opened this issue ยท 11 comments
Describe the issue
I've been trying to get the comments for a post, rather unsuccessfully. I don't know if there is a bug or I'm doing something wrong (can post a gist or two if that's helpful). I've tried a number of different techniques and api calls, but with mixed success. I'm sure a working example is easy to hack together - would really appreciate it.
It should be as simple as iterating through Post.comments
or Comment.replies
, so if that's not working then there might be a bug.
can post a gist or two if that's helpful
That would be very helpful, yeah.
https://gist.github.com/npomfret/4aa86eb48f78f7ec38fbe7c50d647e81
I see the same 10 comments being printed out over and over... although weirdly the score seems to change sometimes
I can see if I log out the query details (by overriding the get
method), it's repeating the same query over and over:
{ path: 'comments/t8fn7m', query: { comment: 't8fn7m' } }
Should there be some sort of pagination implemented here? It looks like the article id is being used maybe where the comment id should be?
(using "snoots": "^1.0.0-dev.23"
)
@thislooksfun any thoughts?
Yeah, I'm guessing that I overlooked something when I tried to fix #67, but I haven't had time to fully investigate and fix it yet.
Yeah, I was right. My fix for #67 broke comment replies, whoops! Fix will be out shortly.
As you requested though, here's an example of how to loop through and print out all of a post's comments.
import type { Comment, Post } from "snoots";
import { Client } from "snoots";
const client = new Client({
// <snip>
});
async function printCommentTree(cmt: Comment, indent: string = "") {
const body = cmt.body.replace(/\n/g, "\n" + indent);
// eslint-disable-next-line no-console
console.log(`${indent}(${cmt.id}) ${cmt.author}: ${body}`);
for await (const reply of cmt.replies) {
await printCommentTree(reply, indent + " ");
}
}
async function printPostAndComments(post: Post) {
// eslint-disable-next-line no-console
console.log(`${post.id} ${post.title}: ${post.url}`);
for await (const comment of post.comments) {
await printCommentTree(comment, " ");
}
}
void (async () => {
const post = await client.subreddits.getHotPosts("funny").first();
if (post) await printPostAndComments(post);
})();
๐ This issue has been resolved in version 1.0.0-dev.24 ๐
The release is available on:
Your semantic-release bot ๐ฆ๐
thanks.
I think maybe I have misunderstood how to iterate over the top level comments (using 1.0.0-dev.24
). The following code doesn't appear to work as I expected:
for await (const comment of post.comments) {
const formatted = comment.body.substring(0, 30).replace(/\s+/g, " ");
console.log(`\tc${++commentIndex} ${comment.id} ${formatted}... score:${comment.score}`);
}
For example, this post
... appears to have lots of 'level 1' comments. But the code above only spits out only 2 comments. These 2 comments are the 2 replies to the first 'level 1' comment (I don't see any 'level 1' comments):
c1 iw5kxzt What's iwae?... score:22
c2 iw7lr3v Was this for one feature or mu... score:2
Is the problem that the initial comment api call looks like this?:
{"path":"comments/yto34q","query":{"comment":"yto34q"}}
Here, yto34q
is the id of the article (it's not a comment id).
If I run your example above, I get similar behaviour. The top level comment is missing, the tree in the output starts at the first reply to the first comment.
My goodness, how many different ways are there for comments to break??
Ok, fixed yet again. This should be the final fix for searched posts' comments. Thank you for using snoots, please let me know if when you find anything else that's broken!
๐ This issue has been resolved in version 1.0.0-dev.25 ๐
The release is available on:
Your semantic-release bot ๐ฆ๐
looks good, thanks!