-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (56 loc) · 1.58 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
const { google } = require("googleapis");
require("dotenv").config();
const cron = require("node-cron");
const moment = require("moment");
const express = require("express");
const app = express();
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
const oauth2Client = new google.auth.OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
process.env.REDIRECT_URI
);
const updateVideo = async () => {
console.log("sysdate ::==", moment());
oauth2Client.setCredentials({ refresh_token: process.env.REFRESH_TOKEN });
// YouTube client
const youtube = google.youtube({
version: "v3",
auth: oauth2Client,
});
try {
// Get video
const result = await youtube.videos.list({
id: "ltG5ZuBZHt0",
part: "statistics,snippet",
});
if (result.data.items.length > 0) {
const stats = result.data.items[0].statistics;
await youtube.videos.update({
part: "snippet",
requestBody: {
id: "ltG5ZuBZHt0",
snippet: {
title: `This video has ${stats.viewCount} views, ${stats.likeCount} likes and ${stats.commentCount} comments.`,
categoryId: 28,
},
},
});
console.log("executed");
}
} catch (error) {
console.log(error);
}
};
// const updateEvery8Mins = new CronJob("*/8 * * * * *", async () => {
// // updateVideo();
// console.log("run");
// });
// updateEvery8Mins.start();
cron.schedule("*/8 * * * *", function () {
updateVideo();
console.log("running a task every 8 minutes");
});