-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweeter.js
56 lines (37 loc) · 1.91 KB
/
tweeter.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
var table = require('./serializer');
var fs = require('fs');
var main = require('./bot');
var downloader = require('./downloader');
var log = require('./logger');
module.exports={
//uses the Twit package object created in the main bot file to send an image
//First get the image path, upload image to twitter and then attach image to tweet and post the tweet
tweetImage : async function(){
var url = await downloader.getImage();
var b64 = fs.readFileSync('./images/x.jpg', { encoding: 'base64' });
log('* Uploading image...');
main.T.post('media/upload', {media_data: b64}, function (err, data, response) {
var mediaId = data.media_id_string;
main.T.post('media/metadata/create', {media_id: mediaId}, function (err, data, response) {
if (!err) {
log(' -> Image uploaded!\r\n* Sending tweet...');
main.T.post('statuses/update', {media_ids: [mediaId]}, function (err, data, response) {
if(err){
log(` -> Error sending tweet!\r\n -> ${new Date().getDate()}/${new Date().getMonth()+1}/${new Date().getFullYear()},${new Date().toLocaleString('en-GB',{timezone: 'Africa/Cairo',hour12: true}).split(',').pop()}\r\n----------------------------------------------------------`);
process.exit();
return;
}
//doesn't add the URL to the hash table unless the tweet is successfully sent
table.add(url);
log(` -> Tweet sent!\r\n -> ${new Date().getDate()}/${new Date().getMonth()+1}/${new Date().getFullYear()},${new Date().toLocaleString('en-GB',{timezone: 'Africa/Cairo',hour12: true}).split(',').pop()}\r\n----------------------------------------------------------`);
return;
});
}else{
log(' -> Error uploading image!');
process.exit();
return;
}
});
});
}
}