mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 05:57:04 +03:00
Added new segment getting endpoint
This commit is contained in:
13
src/app.js
13
src/app.js
@@ -8,8 +8,7 @@ var corsMiddleware = require('./middleware/cors.js');
|
|||||||
var loggerMiddleware = require('./middleware/logger.js');
|
var loggerMiddleware = require('./middleware/logger.js');
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
var getVideoSponsorTimes = require('./routes/getVideoSponsorTimes.js');
|
var getSkipSegments = require('./routes/getSkipSegments.js');
|
||||||
var oldSubmitSponsorTimes = require('./routes/oldSubmitSponsorTimes.js');
|
|
||||||
var postSkipSegments = require('./routes/postSkipSegments.js');
|
var postSkipSegments = require('./routes/postSkipSegments.js');
|
||||||
var voteOnSponsorTime = require('./routes/voteOnSponsorTime.js');
|
var voteOnSponsorTime = require('./routes/voteOnSponsorTime.js');
|
||||||
var viewedVideoSponsorTime = require('./routes/viewedVideoSponsorTime.js');
|
var viewedVideoSponsorTime = require('./routes/viewedVideoSponsorTime.js');
|
||||||
@@ -23,12 +22,19 @@ var getTopUsers = require('./routes/getTopUsers.js');
|
|||||||
var getTotalStats = require('./routes/getTotalStats.js');
|
var getTotalStats = require('./routes/getTotalStats.js');
|
||||||
var getDaysSavedFormatted = require('./routes/getDaysSavedFormatted.js');
|
var getDaysSavedFormatted = require('./routes/getDaysSavedFormatted.js');
|
||||||
|
|
||||||
|
// Old Routes
|
||||||
|
var getVideoSponsorTimes = require('./routes/getVideoSponsorTimes.js');
|
||||||
|
var oldSubmitSponsorTimes = require('./routes/oldSubmitSponsorTimes.js');
|
||||||
|
|
||||||
|
|
||||||
//setup CORS correctly
|
//setup CORS correctly
|
||||||
app.use(corsMiddleware);
|
app.use(corsMiddleware);
|
||||||
app.use(loggerMiddleware);
|
app.use(loggerMiddleware);
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
|
|
||||||
|
// Setup pretty JSON
|
||||||
|
if (config.mode === "development") app.set('json spaces', 2);
|
||||||
|
|
||||||
//add the get function
|
//add the get function
|
||||||
app.get('/api/getVideoSponsorTimes', getVideoSponsorTimes);
|
app.get('/api/getVideoSponsorTimes', getVideoSponsorTimes);
|
||||||
|
|
||||||
@@ -36,7 +42,8 @@ app.get('/api/getVideoSponsorTimes', getVideoSponsorTimes);
|
|||||||
app.get('/api/postVideoSponsorTimes', oldSubmitSponsorTimes);
|
app.get('/api/postVideoSponsorTimes', oldSubmitSponsorTimes);
|
||||||
app.post('/api/postVideoSponsorTimes', oldSubmitSponsorTimes);
|
app.post('/api/postVideoSponsorTimes', oldSubmitSponsorTimes);
|
||||||
|
|
||||||
//add the post function
|
//add the skip segments functions
|
||||||
|
app.get('/api/skipSegments', getSkipSegments);
|
||||||
app.post('/api/skipSegments', postSkipSegments);
|
app.post('/api/skipSegments', postSkipSegments);
|
||||||
|
|
||||||
//voting endpoint
|
//voting endpoint
|
||||||
|
|||||||
@@ -203,71 +203,85 @@ function getVoteOrganisedSponsorTimes(sponsorTimes, votes, UUIDs) {
|
|||||||
|
|
||||||
|
|
||||||
module.exports = function (req, res) {
|
module.exports = function (req, res) {
|
||||||
const videoID = req.body.videoID || req.query.videoID;
|
const videoID = req.body.videoID || req.query.videoID;
|
||||||
// Default to sponsor
|
// Default to sponsor
|
||||||
// If using params instead of JSON, only one category can be pulled
|
// If using params instead of JSON, only one category can be pulled
|
||||||
const categories = req.body.categories || [req.query.category] ["sponsor"];
|
const categories = req.body.categories || (req.query.category ? [req.query.category] : ["sponsor"]);
|
||||||
|
|
||||||
let sponsorTimes = [];
|
/**
|
||||||
let votes = []
|
* @type {Array<{
|
||||||
let UUIDs = [];
|
* segment: number[],
|
||||||
|
* category: string,
|
||||||
|
* UUID: string
|
||||||
|
* }>
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
let segments = [];
|
||||||
|
|
||||||
let hashedIP = getHash(getIP(req) + config.globalSalt);
|
let hashedIP = getHash(getIP(req) + config.globalSalt);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let rows = db.prepare("SELECT startTime, endTime, votes, UUID, shadowHidden FROM sponsorTimes WHERE videoID = ? ORDER BY startTime").all(videoID);
|
for (const category of categories) {
|
||||||
|
let rows = db.prepare("SELECT startTime, endTime, votes, UUID, shadowHidden FROM sponsorTimes WHERE videoID = ? and category = ? ORDER BY startTime")
|
||||||
for (let i = 0; i < rows.length; i++) {
|
.all(videoID, category);
|
||||||
//check if votes are above -1
|
|
||||||
if (rows[i].votes < -1) {
|
|
||||||
//too untrustworthy, just ignore it
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//check if shadowHidden
|
let sponsorTimes = [];
|
||||||
//this means it is hidden to everyone but the original ip that submitted it
|
let votes = []
|
||||||
if (rows[i].shadowHidden == 1) {
|
let UUIDs = [];
|
||||||
//get the ip
|
|
||||||
//await the callback
|
for (let i = 0; i < rows.length; i++) {
|
||||||
let hashedIPRow = privateDB.prepare("SELECT hashedIP FROM sponsorTimes WHERE videoID = ?").all(videoID);
|
//check if votes are above -1
|
||||||
|
if (rows[i].votes < -1) {
|
||||||
|
//too untrustworthy, just ignore it
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//check if shadowHidden
|
||||||
|
//this means it is hidden to everyone but the original ip that submitted it
|
||||||
|
if (rows[i].shadowHidden == 1) {
|
||||||
|
//get the ip
|
||||||
|
//await the callback
|
||||||
|
let hashedIPRow = privateDB.prepare("SELECT hashedIP FROM sponsorTimes WHERE videoID = ?").all(videoID);
|
||||||
|
|
||||||
|
if (!hashedIPRow.some((e) => e.hashedIP === hashedIP)) {
|
||||||
|
//this isn't their ip, don't send it to them
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sponsorTimes.push([rows[i].startTime, rows[i].endTime]);
|
||||||
|
votes.push(rows[i].votes);
|
||||||
|
UUIDs.push(rows[i].UUID);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sponsorTimes.length == 0) {
|
||||||
|
res.sendStatus(404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
organisedData = getVoteOrganisedSponsorTimes(sponsorTimes, votes, UUIDs);
|
||||||
|
sponsorTimes = organisedData.sponsorTimes;
|
||||||
|
UUIDs = organisedData.UUIDs;
|
||||||
|
|
||||||
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||||
|
segments.push({
|
||||||
|
segment: sponsorTimes[i],
|
||||||
|
category: category,
|
||||||
|
UUID: UUIDs[i]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(error) {
|
||||||
|
console.error(error);
|
||||||
|
res.send(500);
|
||||||
|
|
||||||
if (!hashedIPRow.some((e) => e.hashedIP === hashedIP)) {
|
return;
|
||||||
//this isn't their ip, don't send it to them
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sponsorTimes.push([]);
|
if (segments.length == 0) {
|
||||||
|
res.sendStatus(404);
|
||||||
let index = sponsorTimes.length - 1;
|
} else {
|
||||||
|
//send result
|
||||||
sponsorTimes[index][0] = rows[i].startTime;
|
res.send(segments)
|
||||||
sponsorTimes[index][1] = rows[i].endTime;
|
}
|
||||||
|
|
||||||
votes[index] = rows[i].votes;
|
|
||||||
UUIDs[index] = rows[i].UUID;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sponsorTimes.length == 0) {
|
|
||||||
res.sendStatus(404);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
organisedData = getVoteOrganisedSponsorTimes(sponsorTimes, votes, UUIDs);
|
|
||||||
sponsorTimes = organisedData.sponsorTimes;
|
|
||||||
UUIDs = organisedData.UUIDs;
|
|
||||||
|
|
||||||
if (sponsorTimes.length == 0) {
|
|
||||||
res.sendStatus(404);
|
|
||||||
} else {
|
|
||||||
//send result
|
|
||||||
res.send({
|
|
||||||
sponsorTimes: sponsorTimes,
|
|
||||||
UUIDs: UUIDs
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} catch(error) {
|
|
||||||
console.error(error);
|
|
||||||
res.send(500);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user