Skip to content

Instantly share code, notes, and snippets.

@rtanglao
Created August 26, 2010 03:52
Show Gist options
  • Select an option

  • Save rtanglao/550766 to your computer and use it in GitHub Desktop.

Select an option

Save rtanglao/550766 to your computer and use it in GitHub Desktop.
retrieve active5TopicsFromGS, work in progress!
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="application/javascript;version=1.8">
// The following code is by protz based on original :-) non working code by Roland Tanglao
if (typeof console === 'undefined' || !console.log) {
var console = { log: function () {}};
};
$(window).load(function () {
$.ajaxSetup({
cache: false // turn off AJAX caching so you actually get the top 5!
});
var topics = {};
// -- Display routines
function output() {
let sorted_topics = [t for each ([, t] in Iterator(topics))];
sorted_topics.sort(function (t1, t2) {
return (new Date(t1.replies_today)) < (new Date(t2.replies_today));
});
let $ol = $(".d1").find("ol");
$.each(sorted_topics, function (i, topic) {
$ol.append(
$("<li class='topic_"+topic.id+"'></li").append(
$("<a href='"+topic.at_sfn+"'>"+topic.subject+"</a>")
).append(
$("<span> (<span class='r'>"+topic.replies_today+"</span> replies today)</span>")
)
);
if (i == 4)
return false;
});
$(".status").text("");
}
// -- JSON stuff
var today = (new Date());
function tooOld(date) {
let d = new Date(date);
return ((today - d) > 24 * 3600 * 1000);
}
var expected = 1; // for the main loop
function top() {
expected--;
if (expected == 0) {
output();
} else if (expected < 0) {
console.log("Errrrrrrrrror");
}
}
var total = 0;
function getTopics(page){
var url =
"http://api.getsatisfaction.com/products/mozilla_thunderbird/topics.json?sort=recently_active&page="
+ page + "&limit=30&callback=?";
$.getJSON(
url,
function _getTopics_loop (gsjs) { // gsjs is the JSON object from getsatisfaction
var keep_going = true;
// iterate on all topics
$.each(gsjs.data, function(i, topic) {
// we've been too far, and we ended up in some other day's topics
if (tooOld(topic.last_active_at)) {
keep_going = false;
return false; // break
}
topic.replies_today = 0;
topics[topic.id] = topic;
if (topic.reply_count > 0) {
expected++;
total++;
// better UI feedback that way
setTimeout(function () { getReplies(topic, topic.reply_count, 1); }, 5000);
}
});
if (keep_going) {
getTopics(page + 1);
} else {
console.log("We're currently expecting "+total+" topics to be examined");
top();
}
});
};
var totalReplies = 0;
function getReplies(topic, remaining, page) {
// update the UI
if (page == 1) {
total--;
$(".status").text("Still "+total+" topics to examine...");
} else if (total == 0) {
$(".status").text("So far, "+totalReplies+" replies examined...");
}
var url =
"http://api.getsatisfaction.com/topics/" +topic.id +
"/replies.json?sort=recently_created&page=" + page + "&limit=30&callback=?";
$.getJSON(
url,
function _getReplies_loop (gsjs) { //gsjs is the JSON object from getsatisfaction
var keep_going = true;
// iterate on all replies
$.each(gsjs.data, function(i, reply) {
totalReplies++;
if (tooOld(reply.created_at)) {
keep_going = false;
return false;
} else {
topic.replies_today++;
remaining--;
}
});
if (remaining <= 0)
keep_going = false;
if (keep_going) {
getReplies(topic, remaining, page + 1);
} else {
top();
}
}
);
};
getTopics(1);
});
</script>
<style>
body {
font-family: sans-serif;
}
h1 {
text-align: center;
border: 5px solid #ccc;
-moz-border-radius: 5px;
border-radius: 5px;
padding: 10px;
}
</style>
</head>
<body>
<h1>Roland @MoMo's dashboard</h1>
<h2>Most active topics in the last 24 hours</h2>
<div class="status">Fetching topics...</div>
<div class="d1">
<ol>
</ol>
</div>
<div class="d2"></div>
<div class="d3"></div>
<div class="d4"></div>
<div class="d5"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment