# Accessing the latest comments on a site from the Stack API

Context:

As a moderator on a relatively small beta site, I often notice spam, chatty, rude or entirely unrelated comments on old posts. It'd be nice if I had an easy way to monitor them. From the Stack Exchange API I can get hold of the ids of the latest 30 comments on my site.

Questions:

1. I'm not sure how to reconstruct the URL for a comment from the id alone. Any idea?

2. Can I get the latest (say) 100 comments from the API, instead of just 30?

3. Is there any existing userscript or feed which makes accessing the latest comments easier (possibly with direct links to the comments and preview of the texts in comment bodies)?

I know that the API returns a JSON file with details of the latest 30 comments, but that's not quite readable. Moreover, it doesn't provide direct links to the comments. For instance, a random entry in the JSON file looks like:

{
"owner": {
"reputation": 101,
"user_id": 2374,
"user_type": "registered",
"display_name": "Licheng Wang",
},
"edited": false,
"score": 0,
"creation_date": 1557073730,
"post_id": 6043,
"comment_id": 9150
}


# Reconstruct Url:

If you assume the variables:

• $site$ to be the website url
• $postid$ to be the id of the post the comment is on
• $commentid$ to be the id of the comment
https:// $site$ /q/ $postid$ ?#comment $commentid$ _ $postid$


will reconstruct that url (without the spaces). Don't get distracted by the /q/ there. It will work for answers as well as the server will redirect your browser correctly.

example:

https://quantumcomputing.stackexchange.com/q/6043?#comment9150_6043

# Get more then 30 comments:

add a &pagesize=100 to your url. But 100 is the maximum. If you want to fetch more, use the &page=2 paramenter as well. Example:

/comments?order=desc&sort=creation&site=quantumcomputing&pagesize=100

Unfortunately there isn't such feature. You either have to support existing ones or seek support with one of the Chat bot owner. I'm told SOBotics has Boson in the making (and does track comments now for quantumcomputing) and there is IPS Comment Bot which is used on IPS.se and polls that same API you use every 2 minutes. Alternatively I might take a feature request to add that to my Glitch app for feeds.

• Thanks. BTW what does the /q/ stand for in the URL? Question? So would that work for comments on answers as well? – Blue May 5 at 17:30
• @Blue It's questions, but will redirect to the answers if used improperly (Q&A share the Ids) – MEE was the missing bracket May 5 at 17:33
• Seems so! Thanks again. :D – Blue May 5 at 17:33
• Indeed, it would be great if you could add it to your Glitch app for feeds, at your convenience. Your Glitch feeds (for HNQs) are used by several mods all over the network (yep, we had a lot of discussion about it in our private SO team, and everyone loves it :). – Blue May 5 at 17:42
• @Blue Oh boy, discussion ... Now I need to get elected to learn where the fuss was about ;) – rene May 5 at 17:51
• @Blue I'm attempting myself at a little user script. Maybe I will be able to do it and will post it as an answer later today/tomorrow. – MEE was the missing bracket May 5 at 17:57
• @MEE Sure, take your time! Unfortunately, I don't know much about these things. so I can't do it myself (I suppose we need a JSON to RSS converter which can be kept running perpetually). – Blue May 5 at 17:57
• @MEE In fact, there was a Stack2RSS converter once upon a time, but that's broken now. :/ It would be wonderful if we could get something like that again. – Blue May 5 at 18:06
• @Blue done. See my answer below :) – MEE was the missing bracket May 5 at 18:59
• @Blue I have added a link to the SO Botics comment tracker in my answer – rene May 6 at 19:37
• @rene Thanks; that helps a bit. It's a bit messy as the QCSE comments are also accompanied by other reports in that chat room. I'm not sure if there's a way to filter the QCSE comments. – Blue May 8 at 21:33
• @Blue, sorry for the late reply, you can do that by using the SE search - chat.stackoverflow.com/…. Also, I have now added a dashboard for the QC bot as well, so it will be visible here higgs.sobotics.org/Higgs-Boson, with the reason QuantumComputing_Comments. (There are none at the moment, as I just started it). – Bhargav Rao Jun 11 at 2:01
• @BhargavRao Thanks, that's very helpful. :) – Blue Jun 11 at 2:27

I wrote a little user script that adds a Comments item to your left nav and creates the related page on /comments. The API does not expose the question titles, however, therefore I was only able to show the comment body (in order to not run out of API quota). The page will also show the following information (additionally to the body):

• vote count (comment score)
• link count (if there are any)
• relative post date (links to comment)
• comment author and their reputation

// ==UserScript==
// @version  1
// @author   MEE (MSE user Id=364003)
// @include  https://*.stackexchange.com/*
// @include  https://*.meta.stackexchange.com/*
// @require     https://code.jquery.com/jquery-3.3.1.min.js
// @grant    none
// ==/UserScript==

(function() {

$ls =$("#left-sidebar ol.nav-links ol.nav-links");
$ls.append($('<li><a id="nav-comments" href="/comments" class="pl8 nav-links--link">Comments</a></li>'));

site_url = location.hostname.split(".");
if(site_url[1] == "meta")
site_url = site_url[0] + ".meta";
else
site_url = site_url[0];

$("#nav-comments").parent().addClass("youarehere");$("title").text("Newest Comments");
$c =$("#content").html("");
$c.append($("<h1>Recent comments</h1>").addClass("fs-headline1 mb24"));

$list =$("<div class='p1'>").text("Loading...");

$.ajax({ url: API_URL, method: "GET", success: function(comments) { if(comments.quota_remaining == 0) {$list.text("Sorry, you have exceeded your API quota for now.");
return;
}
$list.html(""); for(var c=0; c<comments.length; c++) {$list.append(render_comment(comments[c]));
}
}
});

$c.append($list);

}
})();

function render_comment(comment) {

rel_date = moment(comment.creation_date, "X").fromNow();

$item =$("<div>").addClass("question-summary");
$itemst =$("<div>").addClass("statscontainer");
$itemst.append($("<div>").addClass("stats").append($("<div class='status unanswered'><strong>" + comment.score + "</strong>votes</div>"))); if(comment.body.match(/<a/)) { count = comment.body.match(/<a/g).length$itemst.append($("<div class='views'>"+count+" links</div>").addClass("supernova")); } console.log(comment)$item.append($itemst);$items = $("<div>").addClass("summary");$item.append($items);$items.append($("<div class='excerpt'>").html(comment.body));$started = $("<div class='started'>");$items.append($started);$started.append($("<a href='"+comment.link+"'>").addClass("started-link").text("commented " + rel_date));$started.append(" ");
$started.append($("<a href='"+comment.owner.link+"'>").text(comment.owner.display_name + (comment.owner.user_type == "moderator" ? "♦" : "")));
$started.append(" ");$started.append($("<span>").addClass("reputation-score").text(comment.owner.reputation));$items.append($("<div>").addClass("p2").append($("<a href='"+comment.link+"'>").text("Go to comment")));

return $item; }  • Thanks a ton! But there seem to be some errors with some characters. ' shows as &#39; and quotations are shown as &quot (is this intentional?). MathJax doesn't seem to render either. Could this be corrected? Also, you might want to share this on Stack Apps. :) – Blue May 5 at 19:35 • Here's a screenshot... – Blue May 5 at 19:43 • Yeah, sure. There's also another problem. With the script enabled, whenever I try to "add comment", it effectively does a Page Up for me and I'm unable to add the comment. – Blue May 5 at 19:46 • Here's a .gif, with your script enabled. – Blue May 5 at 19:49 • BTW it may be that it seems MathJax is working for you as you have ChatJax enabled. Say, even in your screenshot$A=|a><b|\$ does not render. – Blue May 5 at 19:52
• @Blue fixed the previous issues with my script (Quotes, MathJax now intentionally and fully disabled. However, I can't reproduce the jumping issue. Could it be related to the page not having loaded fully? (You see, I can comment and I have the script enabled right now) – MEE was the missing bracket May 6 at 17:57
• Thanks for the fix; will check tomorrow and will let you know (a bit busy right now). :) – Blue May 6 at 18:04