-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (72 loc) · 2.64 KB
/
index.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
import { getVideos } from "./components/getVideos.js";
import { checkChanges } from "./components/checkVideos.js";
import {
getNewestFile,
writeVideosToFile,
deleteOldFiles,
} from "./components/fileManager.js";
import { sendWebhookMessages } from "./components/webhookManager.js";
import fs from "fs";
import dotenv from "dotenv";
dotenv.config();
const TOKEN = process.env.TOKEN;
const CHANNEL_ID = "UCpB959t8iPrxQWj7G6n0ctQ"; // channel id for the channel 'SSSniperwolf' aka. An awful freebooter
const TMP_MAX = 10; // max number of files to keep in the .tmp folder
async function main() {
console.log("Running function...");
// wait until the clock minutes are 0 or 30
// then get the videos and repeat
let videos = await getVideos(CHANNEL_ID, TOKEN).catch((error) =>
console.error(error)
);
if (!videos) {
throw new Error("No videos found");
}
// create .tmp folder if it doesn't exist
if (!fs.existsSync(".tmp")) {
fs.mkdirSync(".tmp");
}
// if no files exist in the .tmp folder, skip the compare part
let files = fs.readdirSync(".tmp");
if (files.length == 0) {
console.log("no files in .tmp folder");
writeVideosToFile(videos);
return;
}
// get's the newest file in the .tmp folder
let youngestFile = getNewestFile();
let old_videos = JSON.parse(fs.readFileSync(`.tmp/${youngestFile}`));
// write videos to a new json file in a .tmp folder with the name `videos_{timstamp}.json`
writeVideosToFile(videos);
console.log(`comparing 'old' file: ${youngestFile}`);
let changes = await checkChanges(videos, old_videos);
// once the files reach a certain number, delete the oldest file
deleteOldFiles(TMP_MAX);
// add new change data to the changes.json file
let changedArray = JSON.parse(fs.readFileSync("models/changes.json"));
const removedVideos = changes.removed;
const durationVideos = changes.duration;
const newChanges = removedVideos.concat(durationVideos);
sendWebhookMessages(newChanges);
changedArray = changedArray.concat(newChanges);
fs.writeFile(
"models/changes.json",
JSON.stringify(changedArray, null, 2),
(err) => {
if (err) throw err;
console.log("Data written to file");
}
);
scheduleFunction();
}
function scheduleFunction() {
var now = new Date();
var delay = 30 - (now.getMinutes() % 30); // minutes until next half hour mark
delay = delay * 60 * 1000 - now.getSeconds() * 1000 - now.getMilliseconds(); // convert to milliseconds and subtract seconds and milliseconds already passed in the current minute
console.log(`Waiting ${delay / 1000} seconds until next run...`);
setTimeout(main, delay);
}
//scheduleFunction();
scheduleFunction();
// request count: 190
// plan, check every 30 minutes