Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var commentsList = $('div.comment_item').map(function (i) {
- var id = this.id.match(/\d+/)[0],
- rate = $("#voting_" + id + " span.score").attr('title').match(/↑(\d+) и ↓(\d+)/i);
- return {
- id: id,
- pluses: +rate[1],
- minuses: +rate[2],
- rating: +rate[1] - rate[2],
- answers: $('#reply_comments_' + id + '>div').size(),
- author: $('div.info[rel=' + id + '] a.username', this).html(),
- message: $(this).children('div.message').html()
- }
- }).get(),
- commentsListFirstLevel = commentsList.filter(function (o) {
- return $('#comment_' + o.id).parent('#comments').size() == 1
- }),
- commentsListNotFirstLevel = commentsList.filter(function (o) {
- return $('#comment_' + o.id).parent('#comments').size() == 0
- }),
- buildTop = function (title, list, mapper, orderBy, limit) {
- limit = limit || 10;
- if (orderBy) {
- list.sort(function (a, b) {
- return b[orderBy] - a[orderBy]
- })
- };
- log('<spoiler title="' + title.replace(/%n/g, limit) + '"><ol><li>' + list.slice(0, limit).map(mapper).join('</li>\n<li>') + "</li></ol></spoiler>");
- },
- log = function (msg) {
- log.msg = (log.msg || "") + msg;
- },
- generateURL = function (o) {
- var msg = o.message.replace(/<[^<>]+>|\s+/ig, ' ').trim();
- return '<a href="http://habrahabr.ru/post/148795/#comment_' + o.id + '">' + msg.substring(0, 64).trim().replace(/\s+/g, ' ') + (msg.length > 64 ? '…' : '') + '</a> — <hh user="' + o.author + '" />'
- }, plural = function (number, one, two, five) {
- one = number + ' ' + one;
- two = number + ' ' + two;
- five = number + ' ' + five;
- number = Math.abs(number);
- number %= 100;
- if (number >= 5 && number <= 20) {
- return five;
- }
- number %= 10;
- if (number == 1) {
- return one;
- }
- if (number >= 2 && number <= 4) {
- return two;
- }
- return five;
- };
- log.print = function () {
- console.log(log.msg)
- };
- log("<h4>Топы по комментариям первого уровня</h4>");
- buildTop('Топ %n по ответам', commentsListFirstLevel, function (o) {
- return generateURL(o) + ' (' + plural(o.pluses, "ответ", "ответа", "ответов") + ', рейтинг ' + o.rating + ')'
- }, 'answers');
- buildTop('Топ %n по плюсам', commentsListFirstLevel, function (o) {
- return generateURL(o) + ' (' + plural(o.pluses, "плюс", "плюса", "плюсов") + ', рейтинг ' + o.rating + ')'
- }, 'pluses');
- buildTop('Топ %n по минусам', commentsListFirstLevel, function (o) {
- return generateURL(o) + ' (' + plural(o.minuses, "минус", "минуса", "минусов") + ', рейтинг ' + o.rating + ')'
- }, 'minuses');
- log("\n<h4>Топы по всем комментариям, исключая комментарии первого уровня</h4>");
- buildTop('Топ %n по ответам', commentsListNotFirstLevel, function (o) {
- return generateURL(o) + ' (' + plural(o.pluses, "ответ", "ответа", "ответов") + ', рейтинг ' + o.rating + ')'
- }, 'answers');
- buildTop('Топ %n по плюсам', commentsListNotFirstLevel, function (o) {
- return generateURL(o) + ' (' + plural(o.pluses, "плюс", "плюса", "плюсов") + ', рейтинг ' + o.rating + ')'
- }, 'pluses');
- buildTop('Топ %n по минусам', commentsListNotFirstLevel, function (o) {
- return generateURL(o) + ' (' + plural(o.minuses, "минус", "минуса", "минусов") + ', рейтинг ' + o.rating + ')'
- }, 'minuses');
- var users = {};
- commentsList.forEach(function (o) {
- users[o.author] = (o.author in users ? users[o.author] : 0) + 1
- });
- buildTop("\nТоп %n пользователей по количеству комментариев", Object.keys(users).map(function (u) {
- return {
- username: u,
- comments: users[u]
- }
- }), function (o) {
- return '<hh user="' + o.username + '" /> — ' + plural(o.comments, "комментарий", "комментария", "комментариев");
- }, 'comments');
- var unappreciated = commentsList.filter(function (o) {
- return o.answers == 0 && $('#comment_' + o.id).parent('#comments').size() == 1
- });
- log("\n<h4>Общая статистика</h4>");
- log("\nВсего выдано плюсов: " + commentsList.reduce(function (a, o) {
- return a + o.pluses
- }, 0));
- log("\nВсего выдано минусов: " + commentsList.reduce(function (a, o) {
- return a + o.minuses
- }, 0));
- log("\nХабраюзеров: " + Object.keys(users).length);
- log("\nСредний рейтинг: " + (commentsList.reduce(function (a, o) {
- return a + o.pluses - o.minuses
- }, 0) / commentsList.length).toFixed(3));
- log('\nОбделённых вниманием: ' + unappreciated.length);
- buildTop('Обделённые вниманием, по рейтингу', unappreciated, function (o) {
- return generateURL(o) + ' (рейтинг ' + (o.pluses - o.minuses) + ')'
- }, 'rating', 100);
- log("Время сбора статистики: " + new Date);
- log.print();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement