-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.js
More file actions
335 lines (296 loc) · 12.5 KB
/
test.js
File metadata and controls
335 lines (296 loc) · 12.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const puppeteer = require('puppeteer');
const moment = require('moment-timezone');
const fs = require('fs-extra');
const axios = require('axios');
const LIMIT_MAX = 50;
const uneiUserId = 23189;
const answerUsersMap = new Map();
const noAnswerQuestions = [];
console.log('today is ' + moment().tz("Asia/Tokyo").format('YYYY-MM-DD HH:mm'));
const thisYear = moment().tz("Asia/Tokyo").format('YYYY');
const thisMonth = moment().tz("Asia/Tokyo").format('MM');
const thisDay = moment().tz("Asia/Tokyo").format('DD');
const resultMonth = moment().tz("Asia/Tokyo").subtract(1, 'month').format('MM');
console.log(`resultMonth = ${resultMonth}`);
/**** API情報 ****/
// 基本URL
const apiBaseURL = `https://api.nnn.ed.nico/v3/forum/admin/ranking`;
// 今月のパラメータ type
const thisMonthParam = `type=current_month`;
// 前月のパラメータ type
const lastMonthParam = `type=last_month`;
// 全期間
const totalParam = `type=total`;
/* チャンネルパラメータ channel_id
2: #N中学
3: #N高グループ
4: #Vantan
5: #近畿大学
6: #全般
7: #運営
8: #大学受験
9: #高校準備
10: #語学
11: #プログラミング
*******/
const resultYear = resultMonth == 12 ? thisYear - 1 : thisYear;
let writeToMonthFileFlag = true;
function fetchThisMonth() {
console.log('fetch this month data!');
axios.get(`${apiBaseURL}?${thisMonthParam}&channel_id=6`).then((response) => {
const thisMonthData = response.data.ranking;
fs.copyFileSync('dataFiles/monthlyAnswers_new_30.json', 'dataFiles/monthlyAnswers_old_30.json');
fs.writeFileSync('dataFiles/monthlyAnswers_new_30.json', JSON.stringify(thisMonthData, null, ' '));
console.log('write this month data to file!');
}).catch((e) => {
console.log(e);
console.log('retry anyway!');
setTimeout(fetchThisMonth, 1000 * 60);
return;
});
}
function fetchTotal() {
console.log('fetch total data!');
axios.get(`${apiBaseURL}?${totalParam}&channel_id=6`).then((response) => {
const totalData = response.data.ranking;
fs.writeFileSync('dataFiles/answerUsers_50.json', JSON.stringify(totalData, null, ' '));
console.log('write total data to file!');
}).catch((e) => {
console.log(e);
console.log('retry anyway!');
setTimeout(fetchTotal, 1000 * 60);
return;
});
}
function fetchForumAPI(offset, limit) {
console.log(`offset = ${offset}, limit = ${limit}`);
return new Promise(function(resolve, reject) {
(async() => {
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
try {
const page = await browser.newPage();
const response = await page.goto(`https://api.nnn.ed.nico/v1/questions?offset=${offset}&limit=${limit}`);
const contentsJson = await page.evaluate(() => {
return JSON.parse(document.querySelector("body pre").innerText);
});
if (contentsJson.questions.length === 0) {
await browser.close();
resolve('finish!');
return;
}
for (let contest of contentsJson.questions) {
const questionId = contest.id;
const title = contest.title;
const tags = contest.tags;
const questionUserId = contest.user.zane_user_id;
const questionUserName = contest.user.name;
const isResolved = contest.is_resolved;
const answersCount = contest.answers_count;
const questionTimeStamp = contest.created_at;
const questionTimeYear = moment(questionTimeStamp * 1000).tz("Asia/Tokyo").format('YYYY');
const questionTimeMonth = moment(questionTimeStamp * 1000).tz("Asia/Tokyo").format('MM');
if (writeToMonthFileFlag === true && (questionTimeYear !== thisYear || questionTimeMonth !== thisMonth)) {
writeToMonthFileFlag = false;
console.log('copy file new to old.');
fs.copyFileSync('dataFiles/monthlyAnswers_new_30.json', 'dataFiles/monthlyAnswers_old_30.json');
console.log('write monthly file to new.');
writeToJsonFile(answerUsersMap, 'monthlyAnswers_new.json');
}
if (isResolved == false && answersCount == 0 && tags.indexOf('運営') < 0) { // 未回答&未解決の質問の時
noAnswerQuestions.push({ id: questionId, title, tags });
}
const answersManyMap = new Map();
for (let answer of contest.answers) {
const answerUserId = answer.user.zane_user_id;
const answerUserName = answer.user.name;
const answerUserIcon = answer.user.icon;
if (questionUserId === answerUserId || answerUserId == uneiUserId) {
continue;
}
if (!answersManyMap.has(answerUserId)) { // 新規
answersManyMap.set(answerUserId, { answerUserName, answerMany: 1, answerUserIcon });
} else { // 重複
const answerManyObj = answersManyMap.get(answerUserId);
answerManyObj.answerMany += 1;
answersManyMap.set(answerUserId, answerManyObj);
}
}
for (let [answerUserId, answerManyObj] of answersManyMap) {
if (!answerUsersMap.has(answerUserId)) { // 新規
answerUsersMap.set(answerUserId, { userName: answerManyObj.answerUserName, total: answerManyObj.answerMany, answeredQuestionMany: 1, icon: answerManyObj.answerUserIcon });
} else { // 重複
const answerUser = answerUsersMap.get(answerUserId);
answerUser.total += answerManyObj.answerMany;
answerUser.answeredQuestionMany += 1;
answerUsersMap.set(answerUserId, answerUser);
}
}
}
await browser.close();
return resolve('sucsess and contiune');
} catch (e) {
await browser.close();
return reject(e);
}
})();
});
}
function fetchConroler(offset) {
fetchForumAPI(offset, LIMIT_MAX).then((nonQuestionAnswers) => {
if (nonQuestionAnswers === 'finish!') {
console.log('crowring finish');
writeToJsonFile(answerUsersMap, 'answerUsers.json');
fs.writeFileSync('dataFiles/noAnswerQuestions.json', JSON.stringify(noAnswerQuestions, null, ' '));
return;
}
// sucsess
const nestOffset = offset + LIMIT_MAX;
setTimeout(fetchConroler, 1000, nestOffset);
}).catch((e) => {
console.log(e);
console.log('retry anyway!');
setTimeout(fetchConroler, 1000 * 60, offset);
return;
});
}
function sortAnswersData(answersData) {
return answersData.sort(function(a, b) {
if (a.answeredQuestionMany > b.answeredQuestionMany) {
return -1;
} else if (a.answeredQuestionMany < b.answeredQuestionMany) {
return 1;
} else {
if (a.total > b.total) {
return -1;
} else if (a.total < b.total) {
return 1;
} else {
return 0;
}
}
});
}
function writeToJsonFile(answerUsersMap, fileName) {
let outPutJSON = [];
for ([userId, answerObj] of answerUsersMap) {
const pushObj = {
userId,
userName: answerObj.userName,
total: answerObj.total,
answeredQuestionMany: answerObj.answeredQuestionMany,
icon: answerObj.icon
};
outPutJSON.push(pushObj);
}
outPutJSON = sortAnswersData(outPutJSON);
if (fileName === 'monthlyAnswers_new.json') {
fs.writeFileSync(`dataFiles/${fileName}`.replace('.json', '_30.json'), JSON.stringify(outPutJSON.slice(0, 30), null, ' '));
}
if (fileName === 'answerUsers.json') {
fs.writeFileSync(`dataFiles/${fileName}`, JSON.stringify(outPutJSON, null, ' '));
fs.writeFileSync(`dataFiles/${fileName}`.replace('.json', '_50.json'), JSON.stringify(outPutJSON.slice(0, 50), null, ' '));
}
}
function takeResults() {
prepareDirsSync();
fs.copySync('dataFiles/monthlyAnswers_new_30.json', `results/${resultYear}/${resultMonth}/monthlyAnswers_${resultYear}${resultMonth}.json`);
resultTextSync();
return takeScreenshot();
}
function prepareDirsSync() {
if (fs.existsSync(`results/${resultYear}/${resultMonth}`) === false) {
fs.ensureDirSync(`results/${resultYear}/${resultMonth}`)
}
}
function resultTextSync() {
const users = fs.readJsonSync(`results/${resultYear}/${resultMonth}/monthlyAnswers_${resultYear}${resultMonth}.json`);
const outPutText = [
`みなさんこんにちは!`,
``,
`フォーラム名誉会員 ${Number(resultMonth)}月 の順位発表です。`,
``,
`1位 ${users[0].nickname} さん!`,
`貢献フォーラム数 ${users[0].reply_count}個 の大活躍でした。`,
``,
`2位 ${users[1].nickname} さん! 貢献フォーラム数 ${users[1].reply_count}個!`,
`3位 ${users[2].nickname} さん! 貢献フォーラム数 ${users[2].reply_count}個!`,
`となっております。`,
``,
`以下 30位 までの順位は画像のようになっております。`,
``,
``,
`今後とも ZEN Study とフォーラムをよろしくお願い致します。`,
`https://progedu.github.io/forum-ranking/`
].join('\n');
fs.writeFileSync(`results/${resultYear}/${resultMonth}/postText_${resultYear}${resultMonth}.txt`, outPutText);
}
function takeScreenshot() {
return new Promise(async function(resolve, reject) {
const browser = await launchPuppeteer()
try {
console.log('new Page')
const page = await browser.newPage();
console.log('go to')
const response = await page.goto(`https://progedu.github.io/forum-ranking/`, { "waitUntil": "load" });
console.log('sleep')
await sleep(5000);
console.log('dom')
const element = await page.$('#monthly-ranking');
console.log('setViewport')
await page.setViewport({
width: 900,
height: 4800
});
console.log('boundingBox')
const boundingBox = await element.boundingBox()
console.log('screenshot')
await page.screenshot({
path: `results/${resultYear}/${resultMonth}/result-${resultYear}${resultMonth}.png`,
clip: {
x: Math.floor(boundingBox.x.toFixed(0)) - 4,
y: Math.floor(boundingBox.y.toFixed(0)) - 4,
width: Math.floor(boundingBox.width.toFixed(0)) + 8,
height: Math.floor(boundingBox.height.toFixed(0)) + 8,
}
});
console.log('browser.close')
await browser.close();
return resolve('sucsess and contiune');
} catch (e) {
console.log('catch')
console.log(e);
await browser.close();
return reject(e);
}
});
}
function launchPuppeteer() {
return new Promise(async function(resolve, reject) {
try {
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
console.log('puppeteer success!');
resolve(browser);
} catch (e) {
console.log('retry launch puppeteer!');
launchPuppeteer().then((browser) => {
resolve(browser);
});
}
});
}
function sleep(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
if (thisDay == '01') {
takeResults().then(() => {
fetchThisMonth();
fetchTotal();
// fetchConroler(0);
});
} else {
fetchThisMonth();
fetchTotal();
// fetchConroler(0);
}