SponsorBlockServer/index.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-07-09 19:48:03 +02:00
var express = require('express');
var http = require('http');
// Create a service (the app object is just a callback).
var app = express();
//uuid service
var uuidv1 = require('uuid/v1');
2019-07-09 19:48:03 +02:00
//load database
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./databases/sponsorTimes.db');
// Create an HTTP service.
http.createServer(app).listen(80);
2019-07-09 21:12:08 +02:00
//setup CORS correctly
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
2019-07-09 19:48:03 +02:00
//add the get function
2019-07-09 20:44:40 +02:00
app.get('/api/getVideoSponsorTimes', function (req, res) {
2019-07-09 19:48:03 +02:00
let videoID = req.query.videoID;
let sponsorTimes = [];
db.prepare("SELECT startTime, endTime FROM sponsorTimes WHERE videoID = ?").all(videoID, function(err, rows) {
if (err) console.log(err);
for (let i = 0; i < rows.length; i++) {
sponsorTimes[i] = [];
sponsorTimes[i][0] = rows[i].startTime;
sponsorTimes[i][1] = rows[i].endTime;
}
//send result
res.send({
sponsorTimes: sponsorTimes
})
});
});
//add the post function
2019-07-09 20:44:40 +02:00
app.get('/api/postVideoSponsorTimes', function (req, res) {
let videoID = req.query.videoID;
let startTime = req.query.startTime;
let endTime = req.query.endTime;
if (typeof videoID != 'string' || startTime == undefined || endTime == undefined) {
//invalid request
res.sendStatus(400);
return;
}
2019-07-09 21:20:10 +02:00
startTime = parseFloat(startTime);
endTime = parseFloat(endTime);
let UUID = uuidv1();
db.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?, ?)").run(videoID, startTime, endTime, UUID);
2019-07-09 19:48:03 +02:00
res.sendStatus(200);
2019-07-09 19:48:03 +02:00
});
app.get('/database.db', function (req, res) {
res.sendFile("./databases/sponsorTimes.db", { root: __dirname });
2019-07-09 19:48:03 +02:00
});