-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook-handler.js
62 lines (51 loc) · 1.53 KB
/
webhook-handler.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
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const path = require("path");
const app = express();
const PORT = 3000;
const TRANSCRIPTS_DIR = path.join(__dirname, "transcripts");
// Ensure transcripts directory exists
if (!fs.existsSync(TRANSCRIPTS_DIR)) {
fs.mkdirSync(TRANSCRIPTS_DIR);
}
// Middleware to parse JSON payloads
app.use(bodyParser.json({ limit: "50mb" }));
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000,
})
);
// Single route to save prediction object
app.post("/", (req, res) => {
const video_id = req.query.video_id;
const prediction = req.body;
// Check if video_id and prediction id are provided
if (!video_id || !prediction.id) {
return res
.status(400)
.json({ message: "Missing video_id or prediction id" });
}
const filename = `${video_id}.json`;
const filepath = path.join(TRANSCRIPTS_DIR, filename);
if (prediction.status !== "completed") {
return res
.status(200)
.json({ message: "Skipping incomplete or failed prediction" });
}
// Save JSON object to disk
fs.writeFile(filepath, JSON.stringify(prediction, null, 2), (err) => {
if (err) {
return res
.status(500)
.json({ message: "Error saving the file", error: err });
}
res.status(200).json({ message: "File saved successfully" });
});
console.log(`Saved ${filename}`);
});
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
});