-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
90 lines (71 loc) · 3.39 KB
/
bot.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
const {client, logger} = require("./botAuth");
const http = require('http');
const fs = require('fs');
const schedule = require('node-schedule');
// getRandomArbitrary
function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Get bot Username (can only be called when Twitter wakes up)
async function getUsername() {
return (await client.v2.me()).data.username;
}
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info(`Starting process every hour...`)
// Start function
function startProcess() {
// Make video file.
var bumpNum = getRandomArbitrary(1, 8749);;
var url = `http://static.bumpworthy.com/bumps/${bumpNum}.d.mp4` // Get bump video file.
var filepath = `media/${bumpNum}.mp4` // Set export locaiton.
// Check to see if the video already exists.
var checkFile = fs.existsSync(filepath)
if (checkFile = false) {
logger.error(`Media failed, a video file with the name is in the "media" folder. Restart to try again.`)
return;
}
// Download video.
const file = fs.createWriteStream(filepath);
const thing = http.get(url, (response) => {
response.pipe(file);
file.on('finish', async () => { // When file is done downloaded.
file.close();
if (file.bytesWritten <= 500) {
logger.error(`The bump received (ID: ${bumpNum}) might be not be an actual file.`)
logger.error(`At the moment, the only way to solve this is to restart, sorry... :(`)
return;
}
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info('Download Completed');
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info('Upload Started...');
var filePath = `./${filepath}`;
// Now we're gonna try to upload the media & tweets.
try {
// Upload media first...
const mediaIds = await Promise.all([
client.v1.uploadMedia(filePath),
]);
logger.info('Upload Completed');
// Then we can send in the tweet.
var mainTweet = await client.v2.tweet('', { media: { media_ids: mediaIds } })
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
// get bot username for logs
var botUsername = await getUsername();
logger.info(`Sent main tweet; https://twitter.com/${botUsername}/status/${mainTweet.data.id}`);
// Oh! Don't forget the reply!
var replyTweet = await client.v2.reply(`[source: https://bumpworthy.com/bumps/${bumpNum}]`, mainTweet.data.id);
logger.info(`Sent reply tweet; https://twitter.com/${botUsername}/status/${replyTweet.data.id}`);
} catch (error) {
logger.error(`Something happened while uploading / trying to tweet!!`);
logger.error(error);
}
});
});
};
// After it hits an hour, start again.
var j = schedule.scheduleJob('0 */1 * * *', function(){ // this for one hour
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info(`Time hit! Redoing process...`)
startProcess();
});