-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (154 loc) · 4.61 KB
/
index.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
/** Constants */
const BASE_PATH = '/v2/band/';
const GET_POSTS = `${BASE_PATH}posts`;
const GET_COMMENT_WARNINGS = `${BASE_PATH}post/comments`;
const CREATE_COMMENT = `${BASE_PATH}post/comment/create`;
const REMOVE_COMMENT_WARNING = `${BASE_PATH}post/comment/remove`;
/** Load libraries */
const querystring = require('querystring');
const fs = require('fs');
const Utils = require('./Utils');
/** Declare variable */
//Read congif file
let config = require('./config.json');
//Init delay 5 seconds
let delay = 5;
//Loop the run each 1 seconds
setInterval(() => {
if (delay === 0) { //When delay is zero
//Set access delay
delay = config.ACCESS_DELAY;
//Read posts each band
config.BAND_KEYS.forEach(band_key => {
process.stdout.write(` read: ${band_key}\r`);
readPosts(band_key);
});
//Save config file
fs.writeFile('./config.json', JSON.stringify(config, null, '\t'), 'utf8', () => {
});
} else {
//Print delay
process.stdout.write(` delay: ${--delay} \r`);
}
}, 1000);
function readPosts(band_key) {
//Request posts (GET)
Utils.httpGet({
path: `${GET_POSTS}?${querystring.stringify({
access_token: config.ACCESS_TOKEN,
band_key
})}`
}, (code, data) => {
if (code === 1) { //When result_code is '1'
if (config.VIOLATIONS[band_key] === undefined) {
config.VIOLATIONS[band_key] = {};
}
data.items.forEach(
item => {
let post_key = item.post_key;
let hash = Utils.createHash(item.content);
if (Utils.containsAny(item.content, config.TAGS)) { //When post contain any tag
if (
config.VIOLATIONS[band_key][post_key] !== undefined //Have warning
&& config.VIOLATIONS[band_key][post_key].hash !== hash //Edited after warning
) {
removeComments(band_key, post_key, () => createComment(band_key, post_key, `${config.COMMENT.THANKS.PREFIX}\n${config.COMMENT.THANKS.CONTENT}`));
}
delete config.VIOLATIONS[band_key][post_key];
} else {
if (
config.VIOLATIONS[band_key][post_key] === undefined //Have never warning
|| config.VIOLATIONS[band_key][post_key].hash !== hash //Edited after warning
|| new Date().getTime() - config.VIOLATIONS[band_key][post_key].time >= 3600000 //1 hour passed after the warning
) {
//Store violation data
config.VIOLATIONS[band_key][post_key] = {
user_key: item.author.user_key,
time: new Date().getTime(),
hash,
};
//Print violation info
console.log(`name: ${item.author.name}, post_key: ${post_key}`);
removeComments(band_key, post_key, () => createComment(band_key, post_key, `${config.COMMENT.WARNING.PREFIX}\n${config.COMMENT.WARNING.CONTENT}`));
}
}
}
);
} else {
console.log(data);
}
});
}
function createComment(band_key, post_key, body) {
//Request comment create (POST)
Utils.httpGet({
method: 'POST',
path: `${CREATE_COMMENT}?${querystring.stringify({
access_token: config.ACCESS_TOKEN,
band_key,
post_key,
body
})}`
}, (code, data) => {
if (code !== 1) { //When failure
//Retry request
createComment(band_key, post_key, body);
if (code !== 1003) { //Ignore Cool down time restriction
console.log(data);
}
}
});
}
function removeComment(band_key, post_key, comment_key) {
//Request comment remove (POST)
Utils.httpGet({
method: 'POST',
path: `${REMOVE_COMMENT_WARNING}?${querystring.stringify({
access_token: config.ACCESS_TOKEN,
band_key,
post_key,
comment_key
})}`
}, (code, data) => {
if (code !== 1) { //When failure
//Retry request
removeComment(band_key, post_key, comment_key);
if (code !== 1003) { //Ignore Cool down time restriction
console.log(data);
}
}
});
}
function removeComments(band_key, post_key, callback = null) {
//Request post comments (GET)
Utils.httpGet({
path: `${GET_COMMENT_WARNINGS}?${querystring.stringify({
access_token: config.ACCESS_TOKEN,
band_key,
post_key
})}`
}, (code, data) => {
if (code === 1) { //When success
data.items.forEach(
item => {
if (item.comment_key !== undefined && (
item.content.indexOf(config.COMMENT.WARNING.PREFIX) === 0 //When comment starts with COMMENT.WARNING.PREFIX
|| item.content.indexOf(config.COMMENT.THANKS.PREFIX) === 0 //or comment starts with COMMENT.THANKS.PREFIX
)) {
removeComment(band_key, post_key, item.comment_key);
}
}
);
//Run call back
if (callback !== null) {
callback();
}
} else {
//Retry request
removeComments(band_key, post_key, callback);
if (code !== 1003) { //Ignore Cool down time restriction
console.log(data);
}
}
});
}