forked from theinternetbae/imgprocessing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
113 lines (97 loc) · 3.91 KB
/
main.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
(function(){
"use strict";
console.log("Bot is starting");
// Load required modules/files
var Twit = require('twit');
var Jimp = require("jimp");
var config = require('./config');
// Initialize new Twit instance
var T = new Twit(config);
var addTextToImage = function (text, output_image_file, success_callback) {
console.log('Getting a random image from Unsplash...');
Jimp.read('https://unsplash.it/400/600/?random').then(function (image) {
// Text font
var text_font = Jimp.FONT_SANS_32_WHITE;
// Left margin
var text_x_offset = 20;
// Top margin
var text_y_offset = 20;
// Text max width.
// Value lower than the image width may make the text get wrapped.
var text_max_width = 2;
// If text is wrapped and there are more than 15 words, change to a smaller font.
if (text_max_width <= 2 && text.split(' ').length > 15) {
text_font = Jimp.FONT_SANS_16_WHITE;
}
// Process the image
image.brightness(-0.5);
// Add text to the image
console.log('Adding text to the image...');
Jimp.loadFont(text_font).then(function (font) {
image.print(font, text_x_offset, text_y_offset, text, text_max_width)
.write(output_image_file, success_callback);
});
}).catch(function (err) {
console.log(err);
});
};
var postTweetImage = function (image_file, alt_text, tweet) {
var fs = require('fs');
var b64content = fs.readFileSync(image_file, { encoding: 'base64' });
// Upload the image first
console.log('Uploading the image...');
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
if (!err) {
var media_id = data.media_id_string;
var meta_params = {
media_id: media_id,
alt_text: {
text: alt_text
}
};
// Assign alt text to the uploaded media, for use by screen readers and
// other text-based presentations and interpreters
T.post('media/metadata/create', meta_params, function (err, data, response) {
if (!err) {
var params = {
status: tweet || '',
media_ids: [ media_id ]
};
// Post the tweet
console.log('Sending tweet...');
T.post('statuses/update', params, function (err, data, response) {
if (!err) {
console.log('Tweet sent!');
} else {
console.log(err);
}
});
} else {
console.log(err);
}
});
} else {
console.log(err);
}
});
};
var deleteDirectMessage = function (direct_message_id) {
T.delete('direct_messages/events/destroy', { id: direct_message_id }, function (err, data, response) {
if (!err) {
console.log('DM deleted!');
} else {
console.log(err);
}
});
};
var stream = T.stream('user');
stream.on('direct_message', function (data) {
console.log('Reading new direct message...');
var dm_text = data.direct_message.text;
var tmp_img = 'tmp/image_tmp.jpg';
addTextToImage(dm_text, tmp_img, function () {
postTweetImage(tmp_img, dm_text);
deleteDirectMessage(data.direct_message.id_str);
});
});
})();