/
app.js
386 lines (334 loc) · 10.8 KB
/
app.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// What shall we do to create art?
// Step 1: Create a random name from a set of firsts and lasts:
// Step 2: Hash the name to
// Step 3: choose a color palette and a style. (Mondrian, Malevich, minimalism, etc.)
// Step 4: draw random shapes in this style on the canvas.
var Canvas = require("canvas");
var fs = require('fs');
var canvas = new Canvas(512, 512);
var context = canvas.getContext("2d");
var config = require("./config.json");
var Twit = require("twit");
var stats;
var MongoClient = require('mongodb').MongoClient;
var mongourl = config.mongodb_url;
var consumer_key = config.consumer_key;
var consumer_secret = config.consumer_secret;
var access_token = config.access_token;
var access_token_secret = config.access_token_secret;
var boogie = false; //Math.random() < 0.5;
var title;
function drawVerticalLine(xoffset, ystart, length) {
var counter = 0;
if(boogie) {
context.fillStyle = "rgb(200, 150, 0)";
context.fillRect(xoffset, ystart, 7, length);
counter = Math.floor(Math.random() * length / 2);
context.fillStyle = "rgb(100, 200, 200)";
while(counter < length) {
context.fillRect(xoffset, ystart + counter, 7, 7);
counter += Math.floor(Math.random() * (length - 7) / 2) + 14;
}
} else {
context.fillStyle = "rgb(0,0,0)";
context.fillRect(xoffset, ystart, 7, length);
}}
function drawHorizontalLine(xstart, yoffset, length) {
var counter = 0;
if(boogie) {
context.fillStyle = "rgb(200, 150, 0)";
context.fillRect(xstart, yoffset, length, 7);
counter = Math.floor(Math.random() * length / 2);
context.fillStyle = "rgb(100, 200, 200)";
while(counter < length) {
context.fillRect(xstart + counter, yoffset, 7, 7);
counter += Math.floor(Math.random() * (length - 7) / 2) + 14;
}
} else {
context.fillStyle = "rgb(0,0,0)";
context.fillRect(xstart, yoffset, length, 7);
}
}
var writePng = function(canvas, fname){
var child_process, out, stream;
child_process = require("child_process");
fname = fname || __dirname + '/images/randrian.' + new Date().toISOString() + '.png';
out = fs.createWriteStream(fname);
stream = canvas.pngStream();
stream.on('data', function(it){
return out.write(it);
});
return stream.on('end', function(){
console.log('saved png to ' + fname);
if(~process.argv.indexOf("-open")) {
child_process.exec("open " + fname);
}
});
};
function asum(arr) {
return arr.reduce(function(a, b) {
return a + b;
}, 0)
}
function createCanvas() {
var xparts = [];
while(asum(xparts) < 512) {
xparts.push(Math.floor(Math.pow(Math.random() * 12, 2)) + 50);
}
var yparts = [];
while(asum(yparts) < 512) {
yparts.push(Math.floor(Math.pow(Math.random() * 12, 2)) + 50);
}
var fills = [];
xparts.forEach(function(xp, x) {
fills.unshift([]);
yparts.forEach(function(yp, y) {
fills[0].push({
fill: "white",
w: 1,
h: 1,
wmax: fills.length - (x === xparts.length - 1 ? 1 : 0), //x direction being done in reverse
hmax: yparts.length - (y === 0 ? 1 : y)
});
});
});
var count = Math.floor(
Math.random() * (xparts.length + yparts.length)
) + 4;
var fillcolors = ["yellow", "red", "blue"];
var xp, yp, fill;
while(count--) {
xp = Math.floor(Math.random() * xparts.length);
yp = Math.floor(Math.random() * yparts.length);
fill = fillcolors[Math.floor(Math.random() * (xp * yp === 0 ? 3 : fillcolors.length))];
fills[xp][yp].fill = fill;
}
var p, xd, yd, sanity;
xp = 0;
yp = 0;
count = Math.floor(
Math.random() * xparts.length * yparts.length * (1 - 1 / Math.E)
) + 10;
while(count--) {
p = Math.floor(Math.random() * xparts.length * yparts.length) + 1;
sanity = xparts.length * yparts.length;
xd = 0;
yd = 0;
// choose a random start point.
while(p-- && sanity) {
xp++;
if(xp >= xparts.length) {
xp = 0;
yp = (yp + 1) % yparts.length;
}
if(p < 1
&& (fills[xp][yp].fill == null
|| fills[xp][yp].w > 1
|| fills[xp][yp].h > 1
|| (fills[xp][yp].wmax < 2 && fills[xp][yp].hmax < 2)
)) {
++p; // large rectangle found. Maybe don't count this iteration.
// But also don't let it loop forever if we run out of blocks to play with.
--sanity;
}
}
// If sanity is 0, The Elder Ones got to this iteration.
// (tried too many times to pass through large blocks. This could mean the whole grid is now large blocks,
// and that's OK, but then it's hard to find more).
if(sanity > 0) {
do {
xd = Math.floor(Math.pow(Math.random(), 3) * fills[xp][yp].wmax + 1);
yd = Math.floor(Math.pow(Math.random(), 3) * fills[xp][yp].hmax + 1);
} while(xd < 2 && yd < 2); // Recalculate if we didn't grow the rectangle.
// null out the following stuff in the blocks; adjust max width/height to preceding blocks
(function() {
var i, j, k;
// first pass: check for wmax/ymax differences in rows and columns if block is 2x2 or greater.
for(i = xp + 1; i < xp + xd; i++) {
for(j = yp + 1; j < yp + yd; j++) {
if(fills[i][j].fill == null || fills[i][j].w > 1 || fills[i][j].h > 1) {
//special case -- another row/column in our block has a lesser wmax/hmax than our origin
if(xd > yd) {
xd = i - xp;
} else {
yd = j - yp;
}
}
}
}
for(i = 0; i < xp + xd; i++) {
for(j = 0; j < yp + yd; j++) {
if(i >= xp && j >= yp) {
if(i > xp || j > yp) { // don't null the upper left corner
fills[i][j].fill = null;
}
} else if(i >= xp) { // vertically above the new block
fills[i][j].hmax = Math.min(yp - j, fills[i][j].hmax);
} else if(j >= yp) { // horizontally left of the new block
fills[i][j].wmax = Math.min(xp - i, fills[i][j].wmax);
}
}
}
})();
fills[xp][yp].w = xd;
fills[xp][yp].h = yd;
}
}
context.fillStyle = "rgb(0,0,0)";
context.fillRect(0, 0, 512, 512);
var colorsused = {};
fills.forEach(function(xfills, i) {
xfills.forEach(function(xyfill, j) {
var xoff = asum(xparts.slice(0, i)),
yoff = asum(yparts.slice(0, j)),
w = xparts[i],
h = yparts[j], ref_i = i, ref_j = j;
if(!xyfill.fill)
return;
context.fillStyle = xyfill.fill;
if(xoff < 505 && yoff < 505) {
// can't see these.
colorsused[xyfill.fill] = true;
}
while(++ref_i < i + xyfill.w) {
w += xparts[ref_i];
}
while(++ref_j < j + xyfill.h) {
h += yparts[ref_j];
}
xoff += (i ? 7 : 0);
yoff += (j ? 7 : 0);
w -= (i ? 7 : 0);
h -= (j ? 7 : 0);
context.fillRect(xoff, yoff, w, h);
});
});
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
delete colorsused["white"];
colorsused = Object.keys(colorsused).sort();
switch(colorsused.length) {
case 0:
title = "White";
break;
case 1:
title = capitalize(colorsused[0]);
break;
case 2:
title = capitalize(colorsused[0]) + " and " + capitalize(colorsused[1]);
break;
default:
title = "Red, Blue, and Yellow";
}
if(stats) {
stats[title] = stats[title] || 0;
stats[title]++;
title = "Composition " + stats[title] + " in " + title;
} else {
title = "Composition in " + title;
}
if(mongourl) {
MongoClient.connect(mongourl, function(err, db) {
if(err) {
console.error(err);
} else {
var collection = db.collection(config.mongodb_collection);
collection.update({"_id": stats._id}, stats);
}
});
}
return canvas;
}
// If deployed to Nodejitsu, it requires an application to respond to HTTP requests
// If you're running locally or on Openshift you don't need this, or express at all.
if(!~process.argv.indexOf("-debug")) {
if(~process.argv.indexOf("-server")) {
var express = require("express");
var app = express();
app.get('/', function(req, res){
canvas.toDataURL('image/png', function(err, str){
res.send(
'<h1><a href="https://twitter.com/neoplastibot">@neoplastibot</a></h1>'
+ '<h2>last image: "' + title + '"</h2><img src="' + str + '">'
);
});
});
try {
app.listen(process.env.PORT || 8080);
} catch(e) {
console.error(e);
//continue app. just forget about serving web
}
}
// insert your twitter app info here
var T = new Twit({
consumer_key: consumer_key,
consumer_secret: consumer_secret,
access_token: access_token,
access_token_secret: access_token_secret
});
function makeTweet(){ // first we must post the media to Twitter
T.post('media/upload', { "media": createCanvas().toBuffer().toString('base64') }, function (err, data, response) {
if(err) {
console.error(err);
if(~process.argv.indexOf("-once")) {
process.exit(0);
} else {
throw err;
}
}
// now we can reference the media and post a tweet (media will attach to the tweet)
var mediaIdStr = data.media_id_string;
var params = { status: title, media_ids: [mediaIdStr] };
T.post('statuses/update', params, function (err, data, response) {
console.log(data);
if(~process.argv.indexOf("-once")) {
process.exit(0);
}
});
});
}
function startService() {
if(~process.argv.indexOf("-dry-run")) {
createCanvas();
} else {
makeTweet();
}
if(!~process.argv.indexOf("-once")) {
setInterval(~process.argv.indexOf("-dry-run") ? createCanvas : makeTweet, config.interval || 86400000);
//here we just ensure that the app doesn't sleep by pinging it every five minutes.
// If we didn't keep the app alive, it wouldn't wake up to run the interval function
// at the appropriate interval.
var http = require("http");
setInterval(function() {
http.get({ hostname: "localhost", port: process.env.PORT || 8080 });
}, 300000);
}
}
if(mongourl) {
MongoClient.connect(mongourl, function(err, db) {
if(err) {
console.error(err);
startService();
}
var collection = db.collection(config.mongodb_collection);
collection.findOne({}, function(err, s) {
if(err) {
console.error(err);
} else {
stats = s;
}
db.close();
startService();
});
});
} else {
//with no stats repo configured, continue without attempting to retrieve stats object.
startService();
}
} else {
writePng(createCanvas());
console.log(title);
}
// Step 5: