-
Notifications
You must be signed in to change notification settings - Fork 7
/
gBot.js
145 lines (126 loc) · 3.98 KB
/
gBot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var key = "c48435238febe383667fff89e9c73990a5ec4bb0";
var arr = [];
var points = 0;
var html = '<table class="table" id="data"><thead><tr><th class="text-center">Avatar</th><th class="text-center">Name</th><th class="text-center">User Name</th><th class="text-center">Points</th></tr></thead><tbody>';
var sum = 0;
$(document).ready(function() {
var url = 'https://api.gitter.im/v1/rooms?access_token=' + key;
var roomId = "";
var jsonData = [];
var noOfUsers = 0;
$.ajax({
type: 'GET',
url: url,
async: false,
dataType: 'json',
success: function(data) {
//Do stuff with the JSON data
for (var i = 0; i < data.length; i++) { //data.length
if (data[i]["name"] == 'kgisl/campsite') {
roomId = data[i]["id"];
noOfUsers = data[i]["userCount"];
break;
}
}
},
error: function(xhr, textStatus, errorThrown) {
points = 0;
}
});
for (var i = 0; i < noOfUsers; i += 100) { //30){//noOfUsers
$.ajax({
type: 'GET',
url: 'https://api.gitter.im/v1/rooms/' + roomId + '/users?access_token=' + key + '&skip=' + i + '&limit=100',
//data:data,
async: false,
dataType: 'json',
success: function(data) {
$.merge(jsonData, data);
//alert(jsonData);
},
error: function(xhr, textStatus, errorThrown) {
points = 0;
}
});
}
getData(jsonData);
});
$(document).ajaxStop(function() {
console.log('triggered');
sortTable();
});
function getData(jsonData) {
//alert(json["array"].length
var len = jsonData.length;
for (var i = 0; i < len; i++) { //len
if (jsonData[i]["username"] !== 'QuincyLarson') {
browniePointsFetcher(jsonData[i]["username"]);
arr.push({
avatar: jsonData[i]["avatarUrlSmall"],
avatar2: jsonData[i]["avatarUrlMedium"],
name: jsonData[i]["displayName"],
uname: jsonData[i]["username"],
points: points
});
}
}
var j = 0;
html += arr.map(function(a) {
j++;
return '<tr>' + dataFormatter(a.avatar, a.name, a.uname, a.points, a.avatar2) + '</tr>';
}).join('');
html += '</tbody></table>';
$("#data").html(html);
var a = $("#data").html();
$("#campers").html('<h2><span class="label label-info">Total Campers:- ' + j + '</span></h2>');
$("#totalProblems").html('<h2><span class="label label-info">Total Problems:- ' + sum + '</span></h2>');
}
function browniePointsFetcher(uname) {
var points = 0;
var url = 'https://www.freecodecamp.org/api/users/about?username=' + uname.toLowerCase();
$.ajax({
type: 'GET',
url: url,
//data:data,
async: true,
dataType: 'json',
crossDomain: true,
success: function(data) {
//Do stuff with the JSON data
points = data["about"]["browniePoints"];
$('#' + uname).html('<h2 class="pts">' + points + '</h2>');
sum += points;
$("#totalProblems").html('<h2><span class="label label-info">Total Problems:- ' + sum + '</span></h2>');
},
error: function(xhr, textStatus, errorThrown) {
points = 0;
$('#' + uname).html('<h2 class="pts">0</h2>');
}
});
}
function dataFormatter(image, name, uname, points, urlmedium) {
//alert(urlmedium);
var temp_html = '<td>';
temp_html += '<img src=' + image + ' class="img-thumbnail" width="100px" ></img></td>';
temp_html += '<td>';
temp_html += '<h3>' + name + '</h3></td>';
temp_html += '<td>';
temp_html += '<h3><a href="http://freecodecamp.com/' + uname + '" target="_blank">' + uname + '</a></h3></td>';
temp_html += '<td id=' + uname + ' class="points"></td>';
return temp_html;
}
function sortTable() {
var rows = $('table tbody tr').get();
rows.sort(function(a, b) {
var x = parseInt($(a).children('td').eq(3).children().text());
var y = parseInt($(b).children('td').eq(3).children().text());
if (x < y)
return 1;
if (x > y)
return -1;
return 0;
});
$.each(rows, function(index, row) {
$('table').children('tbody').append(row);
});
}