-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathse_tags.js
269 lines (234 loc) · 8.38 KB
/
se_tags.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// requires se_query.js
//
// Functions for the whole StackExchange network
//
function fetchSites()
{
sites = seQuery("sites", {}, 10000);
return sites.filter(function(x) {return x.site_type === "main_site";})
.sort(function(x,y){
if (x.name > y.name)
return 1;
else
return -1;
});
}
//
// Functions for a particular tag
//
function fetchTopAskers(siteName, tagName)
{
var askersSize = 5;
var tagNameFixed = tagName.replace("#", "%23"); // for "C#" may be problems with other characteres
return seQuery("tags/" + tagNameFixed + "/top-askers/all_time", {site: siteName}, askersSize);
}
function fetchTopAnswerers(siteName, tagName)
{
var answerersSize = 5;
var tagNameFixed = tagName.replace("#", "%23");
return seQuery("tags/" + tagNameFixed + "/top-answerers/all_time", {site: siteName}, answerersSize);
}
function fetchTopQuestions(siteName, tagName)
{
var howMany = 5;
var tagNameFixed = tagName.replace("#", "%23");
return seQuery("questions", {site: siteName, tagged: tagNameFixed, sort: "votes", order: "desc"}, howMany);
// return seQuery("tags/" + tagNameFixed + "/faq", {site: siteName}, howMany);
}
//
// Helpers
//
var arrayOfDictToDict = function(list, field){
var res = {};
for (var i=0; i < list.length; i++){
res[list[i][field]] = list[i];
res[list[i][field]]['pos'] = i;
}
return res;
};
//
// Dealing with tags from a particular StackExchange site
//
var SeDataLoaderPerSite = function(siteName, tagLimit, centralTag, delay){
this.status = "Initializing...";
this.siteName = siteName;
this.tagLimit = tagLimit;
this.centralTag = centralTag || undefined;
this.delay = delay || 100;
// see in SE API documentation:
// "If a single IP is making more than 30 requests a second,
// new requests will be dropped"
// i.e. 33.(3) ms
// but anyway, even for slower requests,
// longer bursts does not look good
this.siteStats = null;
// age of extracted data:
// warning: Date object count in milisceconds,SEapi count in seconds.
this.month = 1*1000*60*60*24*30;
this.today = new Date().getTime(); //actual date
this.todate = Math.floor(new Date(this.today - this.month).getTime()/1000);
this.siteData = sitesDict[siteName];
// at least to have this info here
this.fetchSiteStats = function (siteName)
{
return seQuery("info", {site: siteName}, 1);
};
this.fetchPopularTags = function(siteName, tagLimit)
{
if (this.centralTag == undefined) {
return seQuery("tags",
{site: siteName, sort: "popular", order: "desc"},
tagLimit);
} else {
return seQuery("tags/" + this.centralTag + "/related",
{site: siteName, sort: "popular", order: "desc"},
tagLimit);
}
};
this.run = function(){
this.siteStats = this.fetchSiteStats(siteName)[0];
if (this.centralTag) {
try {
this.centralTagCount = seQuery("tags/" + this.centralTag + "/info", {site: siteName}, 1)[0]['count'];
// this.centralTagText = seQuery("tags/" + this.centralTag + "/wikis", {site: siteName}, 1)[0]['excerpt'];
// if (this.centralTagText.length > 140) {
// this.centralTagText = this.centralTagText.slice(137) + "...";
// }
} catch (e) {
$("#loading_status").html("No such tag.");
alert("No such tag!");
return;
}
}
if (!this.centralTag) {
$(".site_info #site_name").html(this.siteData.name);
$(".site_info #dscr").html(this.siteData.audience);
$(".site_info #site_name").hide().attr("href", this.siteData.site_url).show();
} else {
$(".site_info #site_name").html(this.siteData.name + ": " + this.centralTag);
// $(".site_info #dscr").html(this.centralTagText);
$(".site_info #dscr").html("");
$(".site_info #site_name").hide().attr("href", this.siteData.site_url + "/questions/tagged/" + this.centralTag).show();
}
if (!this.centralTag) {
this.noOfQuestions = this.siteStats.total_questions;
} else {
this.noOfQuestions = this.centralTagCount;
}
$(".site_info #count").html("(" + siNumberApprox(this.noOfQuestions) + ")");
this.retriveTags();
this.retriveRelatedTags();
};
this.tags = [];
this.tagsDict = {};
this.links = [];
this.relatedTagDict = {};
this.lastQuestionsPerTagDict = {};
this.retriveTags = function(){
var tagLimit = this.tagLimit;
this.tags = this.fetchPopularTags(siteName, tagLimit);
this.tagsDict = arrayOfDictToDict(this.tags, "name");
};
// below, tag limit does not need to be the same
this.retriveRelatedTags = function(){
this.relatedTagDict = {};
var tagLimit = this.tagLimit; // this one does not need to be the same
var that = this;
for (var i=0; i < this.tags.length; i++){
(function(){
var tagName = that.tags[i].name;
var tagNames = [tagName];
if (that.centralTag != undefined) {
tagNames.push(that.centralTag);
}
tagNames = tagNames.join(";");
setTimeout( function() {
seQueryAsync("tags/" + tagNames + "/related",
{site: siteName},
tagLimit,
that.putRelatedTagInDict,
[tagName, that.relatedTagDict, that.tags.length, that]);
}, i * that.delay);
})();
}
};
this.retriveLastQuestionsPerTag = function(){
this.lastQuestionsPerTagDict = {};
var tagLimit = this.tagLimit;
var that = this;
for (var i=0; i < this.tags.length; i++){
(function(){
var tagName = that.tags[i].name;
var tagNames = [tagName];
if (that.centralTag != undefined) {
tagNames.push(that.centralTag);
}
tagNames = tagNames.join(";");
setTimeout( function() {
seQueryAsync("questions",
{order: "desc", sort: "creation", tagged: tagNames, site: siteName,
todate: that.todate}, // question age
100, // 100 last questions
that.putLastQuestionsPerTagDict,
[tagName, that.lastQuestionsPerTagDict, that.tags.length, that]);
}, i * that.delay);
})();
}
};
this.putRelatedTagInDict = function(x, tagName, targetDict, tagsLength, that){
targetDict[tagName] = x.items;
var progress = Object.keys(targetDict).length;
if (progress === tagsLength) {
$("#loading_status").show().html("Loading tag neighbors: DONE!");
setTimeout(function(){
$("#loading_status").html("").hide();
}, 1000);
that.processRelatedTags();
} else {
$("#loading_status").show().html("Loading tag neighbors: " + (progress) + "/" + tagsLength + "...");
}
};
this.putLastQuestionsPerTagDict = function(x, tagName, targetDict, tagsLength, that){
targetDict[tagName] = x.items;
var progress = Object.keys(targetDict).length;
if (progress === tagsLength) {
$("#loading_status").show().html("Loading additional tag info: DONE!");
that.status = "Done!";
setTimeout(function(){
$("#loading_status").html("").hide();
}, 1000);
} else {
$("#loading_status").show().html("Loading additional tag info: " + (progress) + "/" + tagsLength + "...");
}
};
this.processRelatedTags = function (includeCentralTag) {
this.links = [];
for (var tag1 in this.relatedTagDict)
{
var tag1info = this.tagsDict[tag1];
var relatedTags = this.relatedTagDict[tag1];
for (var i = 0; i < relatedTags.length; i++){
var tag2info = relatedTags[i];
var tag2 = tag2info.name;
if ((tag2 in this.tagsDict) && (tag1 < tag2))
// isn't this order stuff making sense as lose some entries?
{
var link = {count: tag2info.count,
source: this.tagsDict[tag1].pos,
target: this.tagsDict[tag2].pos,
source_name: tag1,
target_name: tag2,
oe_ratio: (tag2info.count * this.noOfQuestions) / (tag1info.count * this.tagsDict[tag2].count)
};
if ((link.count > 1) && (link.oe_ratio > 1))
{
this.links.push(link);
}
}
}
}
// if (includeCentralTag) {
// }
draw_graph(this);
};
};