forked from qsniyg/maxurl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
304 lines (261 loc) · 7.93 KB
/
bot.js
File metadata and controls
304 lines (261 loc) · 7.93 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
var bigimage = require('./userscript.user.js');
var sizeOf = require('image-size');
var probe = require('probe-image-size');
var http = require('http');
var https = require('https');
var url = require('url');
const NodeCache = require( "node-cache" );
var fs = require("fs");
var blacklist_json = JSON.parse(fs.readFileSync("./blacklist.json"));
//var env_json = JSON.parse(fs.readFileSync("./.env.json"));
var env_json = {};
require('dotenv').config();
env_json.user_agent = process.env.USERAGENT;
env_json.client_id = process.env.CLIENT_ID;
env_json.client_secret = process.env.CLIENT_SECRET;
env_json.refresh_token = process.env.REFRESH_TOKEN;
env_json.access_token = process.env.ACCESS_TOKEN;
//env_json.username = process.env.REDDIT_USER;
//env_json.password = process.env.REDDIT_PASS;
//console.dir(env_json);
var thresh_px = 200;
const Snoowrap = require('snoowrap');
const Snoostorm = require('snoostorm');
const r = new Snoowrap(env_json);
r.config({requestDelay: 1001});
const client = new Snoostorm(r);
var blacklist = [
// Posts that would be insensitive to comment on
"killed",
"died",
"death",
"murdered",
"murder",
// Posts in-between the first and second category
"embarrassed",
"embarrassing",
"cringe",
"cringiest",
"cringefest",
"shame",
"shaming",
// Posts that people commonly dislike the bot commenting on
"trump",
"hillary",
"punch",
"punchable",
"ugly",
"fat"
];
function inblacklist(x) {
var black = false;
x.toLowerCase().split(" ").forEach((word) => {
word = word
.replace(/^[^a-z]*/, "")
.replace(/[^a-z]*$/, "");
if (blacklist.indexOf(word) >= 0) {
black = true;
return;
}
});
return black;
}
/*function getimagesize(imgUrl, olddata) {
var options = url.parse(imgUrl);
return new Promise((resolve, reject) => {
var getter = http;
if (options.protocol === "https:")
getter = https;
getter.get(options, function(response) {
if (response.statusCode !== 200) {
reject({
"status": response.statusCode
});
return;
}
var finish = function() {
var buffer = Buffer.concat(chunks);
response.destroy();
try {
var dimensions = sizeOf(buffer);
resolve({
length: length,
width: dimensions.width,
height: dimensions.height
});
return;
} catch (e) {
reject(e);
return;
}
};
var length = response.getHeader('content-length');
if (length === olddata.length) {
reject({
"identical_length": length
});
return;
}
var chunks = [];
var size = 0;
response.on('data', function (chunk) {
chunks.push(chunk);
size += chunk.length;
if (size > 2048) {
finish();
}
}).on('end',function() {
finish();
});
});
});
}*/
function getimagesize(url) {
if (typeof(url) === "string") {
return probe(url);
}
if (typeof(url.url) === "string") {
return getimagesize(url.url);
}
return new Promise((resolve, reject) => {
var do_getimage = function(urls, err) {
if (urls.length === 0) {
reject(err);
return;
}
getimagesize(urls[0]).then(
(data) => {
resolve(data);
},
(err) => {
do_getimage(urls.slice(1), err);
}
);
};
do_getimage(url.url);
});
}
function dourl(url, post) {
var big = bigimage(url, {fill_object:true});
if (big.url instanceof Array) {
if (big.url.indexOf(url) >= 0) {
return;
}
} else if (big.url === url) {
return;
}
/*if (big === url) {
return;
}*/
if (post && inblacklist(post.title)) {
console.log("Post blacklisted:\n" + post.title + "\n" + post.permalink + "\n" + post.url + "\n=====\n\n");
return;
}
console.log(url);
console.log(big);
console.log("---");
getimagesize(url).then(
(data) => {
getimagesize(big).then(
(newdata) => {
var wr = newdata.width / data.width;
var hr = newdata.height / data.height;
var r = (wr + hr) / 2;
if (r >= 1.1 && (((newdata.width - data.width) > thresh_px &&
newdata.height > data.height) ||
((newdata.height - data.height) > thresh_px &&
newdata.width > data.width))) {
var times = "" + r.toFixed(1) + "x";
if (r < 1.995) {
times = "" + ((r-1) * 100).toFixed(0) + "%";
}
var filesize_text = "";
var mbs = newdata.length / 1024 / 1024;
if (mbs > 5) {
filesize_text = ", " + mbs.toFixed(1) + "MB";
}
var comment = times + " larger (" + parseInt(newdata.width) + "x" + parseInt(newdata.height) + filesize_text + ") version of linked image:\n\n" + newdata.url + "\n\n";
comment += "*****\n\n";
comment += "^[source code](https://github.com/qsniyg/maxurl) | [website](https://qsniyg.github.io/maxurl/) / [userscript](https://greasyfork.org/en/scripts/36662-image-max-url) (finds larger images)";
console.log(comment);
if (post) {
post.reply(comment).then((comment_data) => {
comment_data.edit(
comment + " | [remove](https://www.reddit.com/message/compose/?to=MaxImageBot&subject=delete:+" + comment_data.id + "&message=delete)"
);
});
}
} else {
console.log("Ratio too small: " + wr + ", " + hr);
}
console.log("========");
},
(err) => {
console.dir(err);
return;
}
);
},
(err) => {
console.dir(err);
return;
}
);
}
const links = new NodeCache({ stdTTL: 600, checkperiod: 1000 });
// large image
//dourl("https://i.guim.co.uk/img/media/856021cc9b024ee18480297110f6a9f38923b4ee/0_0_15637_9599/master/15637.jpg?w=1920&q=55&auto=format&usm=12&fit=max&s=774b471892a2a1870261227f29e9d77a");
//console.dir(blacklist_json.disallowed);
if (true) {
var submissionStream = client.SubmissionStream({
"subreddit": "all",
"results": 100,
"pollTime": 2000
});
setInterval(() => {
r.getInbox({"filter":"messages"}).then((inbox) => {
inbox.forEach((message_data) => {
if (message_data.subject.indexOf("delete:") !== 0 ||
message_data.subject.length >= 50 ||
!message_data["new"]) {
return;
}
var comment = message_data.subject.replace(/.*:[ +]*([A-Za-z0-9_]+).*/, "$1");
if (comment === message_data.subject)
return;
console.log(comment);
r.getComment(comment).fetch().then((comment_data) => {
if (comment_data.author.name.toLowerCase() !== "maximagebot")
return;
r.getComment(comment_data.parent_id).fetch().then((post_data) => {
if (post_data.author.name.toLowerCase() !== message_data.author.name.toLowerCase()) {
return;
}
console.log("Deleting " + comment);
comment_data.delete();
message_data.deleteFromInbox();
});
});
});
});
}, 10*1000);
submissionStream.on("submission", function(post) {
if (post.domain.startsWith("self.") || (post.over_18 && false)) {
return;
}
if (post.subreddit.display_name) {
if (blacklist_json.disallowed.indexOf(post.subreddit.display_name.toLowerCase()) >= 0 ||
blacklist_json.users.indexOf(post.author.name.toLowerCase()) >= 0) {
//console.log(post.subreddit);
return;
}
}
if (links.get(post.permalink) === true) {
//console.log("Already processed " + post.permalink + ", skipping");
return;
}
links.set(post.permalink, true);
var url = post.url;
dourl(url, post);
});
}