-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.ts
More file actions
132 lines (107 loc) · 4.92 KB
/
Copy pathbot.ts
File metadata and controls
132 lines (107 loc) · 4.92 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
import { client, logger } from "./botAuth.js";
import https from 'https';
import fs from 'fs';
import schedule from 'node-schedule';
import config from './config.json' with { type: "json" };
// getRandomArbitrary
function getRandomArbitrary(min: number, max: number) {
return Math.floor(Math.random() * (max - min)) + min;
}
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
if (config.minutes !== 0) logger.warn('"config.minutes" set! Using minutes instead of hours');
logger.info(`Starting process when it hits every ${config.minutes ? `${config.minutes} minute(s)...` : `${config.hours} hour(s)...`}`);
// Make sure the media folder exists
const mediaFolder = fs.existsSync('media/');
if (mediaFolder === false) {
logger.info('Creating "media" folder...');
fs.mkdirSync('media');
logger.info('Created "media" folder!');
}
var currentMedia;
var restartCount = 0;
// Start function
function startProcess(defiendClip?: string) {
// Make video file.
var bumpNum = getRandomArbitrary(1, 8749);;
var url = `https://static.bumpworthy.com/bumps/${bumpNum}.d.mp4` // Get bump video file.
var filepath = `media/${bumpNum}.mp4` // Set export locaiton.
// If the proccess was possibly restarted, we'll skip another download
if (!defiendClip) {
// Download video.
const file = fs.createWriteStream(filepath);
const thing = https.get(url, (res) => {
res.pipe(file);
file.on('finish', async () => { // When file is done downloaded.
file.close();
// read file to see if it returned a html error page
const fileTxt = fs.readFileSync(filepath, { encoding: 'utf8' });
if (
fileTxt.startsWith('<!DOCTYPE html>')
|| file.bytesWritten <= 1000
) {
logger.error(`The bump received (ID: ${bumpNum}) might be not be an actual file.`)
if (restartCount >= 5) {
logger.error(`Too many restarts! Not going to continue.`);
return restartCount = 0;
} else {
logger.error(`Restarting...`);
startProcess();
restartCount++;
}
} else currentMedia = filepath;
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info(`Download Completed - Bump #${bumpNum}`);
getToTweeting(filepath);
});
});
} else getToTweeting(defiendClip);
async function getToTweeting(filepath: string) {
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info('Upload Started...');
var dotfilepath = `./${filepath}`;
// Now we're gonna try to upload the media & tweets.
try {
// Upload media first...
let media = await client.media.create(dotfilepath);
logger.info('Upload Completed -', media);
// Add alt text.
let altTxt = `[source: https://bumpworthy.com/bumps/${bumpNum}]`;
// Then we can send in the tweet.
var mainTweet = await client.tweets.create(config.addReplyInsteadofAlt === false ? altTxt : '', {
mediaIds: [media.media_id_string],
});
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
const me = await client.account.viewer();
logger.info(`Sent main tweet; https://twitter.com/${me.username}/status/${mainTweet.id}`);
// Reply if user wants to use up rate limits.
if (config.addReplyInsteadofAlt == true) {
var replyTweet = await client.tweets.create(altTxt, {
replyTo: mainTweet.id
});
logger.info(`Sent reply tweet; https://twitter.com/${me.username}/status/${replyTweet.id}`);
}
} catch (error: any) {
logger.error(`Tweet failed! "${error.message}"`);
console.error(error);
if (restartCount >= 5) {
logger.error(`Too many restarts! Not going to continue.`);
return restartCount = 0;
} else {
logger.error(`Restarting...`);
startProcess(filepath);
restartCount++;
}
return;
}
}
};
// Start the process for dev purposes.
// startProcess();
// Get time and convert it for use for the schedule
let nodeSchedule = `0 */${config.hours} * * *`;
if (config.minutes !== 0) nodeSchedule = `*/${config.minutes} * * * *`;
var j = schedule.scheduleJob(nodeSchedule, () => { // this for one hour
console.log(`ーーーーーーーーーー✄ーーーーーーーーーー`);
logger.info(`Time hit! Redoing process...`)
startProcess();
});