Skip to content

Instantly share code, notes, and snippets.

@hippietrail
Created January 4, 2012 17:16
Show Gist options
  • Select an option

  • Save hippietrail/1561046 to your computer and use it in GitHub Desktop.

Select an option

Save hippietrail/1561046 to your computer and use it in GitHub Desktop.
Tool to list all questions with only 1 or 2 answers on travel.stackexchange.com
<html>
<head>
<title>Travel StackExchange / jQuery / Stack Exchange API / YQL / CORS</title>
<script id="jqs" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<style type="text/css">
.container {
width:100%;
}
.left {
width:75%;
float:left;
}
.right {
width:25%;
float:right;
}
.one, .one a {
color:red
}
.two {
color:blue
}
.warn {
background-color:yellow
}
.err {
background-color:red
}
</style>
<script type="text/javascript">
$(document).ready(function(){
var hit = 0;
var ajaxarray = new Array();
$.getJSON("http://api.travel.stackexchange.com/1.1/questions?jsonp=?",
{
pagesize: 0
},
function(data) {
if (data.error) {
$("<div/>").text("error getting number of pages").appendTo("#output");
} else {
var remainder = data.total % 100;
var pagecount = (data.total - remainder) / 100 + (remainder != 0);
$("#qt").text(data.total);
for (var page = 1; page <= pagecount; ++page) {
ajaxarray.push($.getJSON("http://api.travel.stackexchange.com/1.1/questions?jsonp=?",
{
order: "asc",
page: page,
pagesize: 100,
sort: "creation"
},
function(data) {
if (data.error) {
$("<div/>").text("error getting page " + page).appendTo("#output");
} else {
$.each(data.questions, function(i, q){
if (q.answer_count >= 1 && q.answer_count <= 2 && !("closed_date" in q)) {
// maintain this list in order using binary search
var ol = $("#list");
var needle = q.question_id;
var high = ol.children("li").length;
var low = 0;
var mid;
var id;
while (low < high) {
mid = parseInt((low + high) / 2)
id = $("ol#output li:nth-child(" + (mid+1) + ")").attr('id');
if (needle < id) {
high = mid;
} else {
low = mid + 1;
}
}
id = $("ol#output li:nth-child(" + (low+1) + ")").attr('id');
var li = $("<li id=\"" + q.question_id + "\" class=\"" + ["one", "two"][q.answer_count - 1] + "\">" +
"<a href=\"http:\/\/travel.stackexchange.com\/questions\/" + q.question_id + "\/\">" + q.title + "</a></li>");
if (typeof id == "undefined") {
li.appendTo("#list");
} else {
li.insertBefore("#" + id);
}
$("#hit").text(++hit);
}
});
}
}
));
}
$.when.apply($, ajaxarray).then(function() {
$("<span/>").text("ALL DONE").appendTo("#info");
});
}
}
);
});
</script>
</head>
<body>
<span id="info">Travel Stack Exchange little-answered questions tool. <span id="qt">?</span> questions in total, <span id="hit">?</span> have only 1 or 2 answers...</span>
<hr>
<div id="output"/>
<ol id="list"/>
</body>
</html>
@ankurdotb

Copy link
Copy Markdown

1-2, afaik

@hippietrail

Copy link
Copy Markdown
Author

OK changed the title and renamed the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment