mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-15 16:07:03 +03:00
Made auto-moderate only call API once
This commit is contained in:
@@ -84,16 +84,15 @@ function sendDiscordNotification(userID, videoID, UUID, segmentInfo) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// submission: {videoID, startTime, endTime}
|
|
||||||
// callback: function(reject: "String containing reason the submission was rejected")
|
// callback: function(reject: "String containing reason the submission was rejected")
|
||||||
// returns: string when an error, false otherwise
|
// returns: string when an error, false otherwise
|
||||||
async function autoModerateSubmission(submission, callback) {
|
async function autoModerateSubmission(videoID, segments) {
|
||||||
// Get the video information from the youtube API
|
// Get the video information from the youtube API
|
||||||
if (config.youtubeAPI !== null) {
|
if (config.youtubeAPI !== null) {
|
||||||
let {err, data} = await new Promise((resolve, reject) => {
|
let {err, data} = await new Promise((resolve, reject) => {
|
||||||
YouTubeAPI.videos.list({
|
YouTubeAPI.videos.list({
|
||||||
part: "contentDetails",
|
part: "contentDetails",
|
||||||
id: submission.videoID
|
id: videoID
|
||||||
}, (err, data) => resolve({err, data}));
|
}, (err, data) => resolve({err, data}));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -102,39 +101,44 @@ async function autoModerateSubmission(submission, callback) {
|
|||||||
} else {
|
} else {
|
||||||
// Check to see if video exists
|
// Check to see if video exists
|
||||||
if (data.pageInfo.totalResults === 0) {
|
if (data.pageInfo.totalResults === 0) {
|
||||||
return "No video exists with id " + submission.videoID;
|
return "No video exists with id " + videoID;
|
||||||
} else {
|
} else {
|
||||||
let duration = data.items[0].contentDetails.duration;
|
let duration = data.items[0].contentDetails.duration;
|
||||||
duration = isoDurations.toSeconds(isoDurations.parse(duration));
|
duration = isoDurations.toSeconds(isoDurations.parse(duration));
|
||||||
|
|
||||||
if (duration == 0) {
|
if (duration == 0) {
|
||||||
// Allow submission if the duration is 0 (bug in youtube api)
|
// Allow submission if the duration is 0 (bug in youtube api)
|
||||||
return false;
|
return false;
|
||||||
} else if ((submission.endTime - submission.startTime) > (duration / 100) * 80) {
|
}
|
||||||
// Reject submission if over 80% of the video
|
|
||||||
return "Sponsor segment is over 80% of the video.";
|
|
||||||
} else {
|
|
||||||
let overlap = false;
|
|
||||||
|
|
||||||
let response = await fetch("https://ai.neuralblock.app/api/getSponsorSegments?vid=" + submission.videoID);
|
for (const segment of segments) {
|
||||||
if (!response.ok) return false;
|
if ((segment.segment[1] - segment.segment[0]) > (duration / 100) * 80) {
|
||||||
|
// Reject submission if over 80% of the video
|
||||||
|
return "One of your submitted segments is over 80% of the video.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let overlap = false;
|
||||||
|
|
||||||
let nbPredictions = await response.json();
|
let response = await fetch("https://ai.neuralblock.app/api/getSponsorSegments?vid=" + videoID);
|
||||||
for (const nbSegment of nbPredictions.sponsorSegments) {
|
if (!response.ok) return false;
|
||||||
|
|
||||||
|
let nbPredictions = await response.json();
|
||||||
|
for (const nbSegment of nbPredictions.sponsorSegments) {
|
||||||
|
for (const segment of segments) {
|
||||||
// The submission needs to be similar to the NB prediction by 65% or off by less than 7 seconds
|
// The submission needs to be similar to the NB prediction by 65% or off by less than 7 seconds
|
||||||
// This calculated how off it is
|
// This calculated how off it is
|
||||||
let offAmount = Math.abs(nbSegment[0] - submission.startTime) + Math.abs(nbSegment[1] - submission.endTime);
|
let offAmount = Math.abs(nbSegment[0] - segment.segment[0]) + Math.abs(nbSegment[1] - segment.segment[1]);
|
||||||
if (offAmount / (nbSegment[1] - nbSegment[0]) <= 0.45 || offAmount <= 7) {
|
if (offAmount / (nbSegment[1] - nbSegment[0]) <= 0.45 || offAmount <= 7) {
|
||||||
overlap = true;
|
overlap = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (overlap) {
|
if (overlap) {
|
||||||
return false;
|
return false;
|
||||||
} else{
|
} else{
|
||||||
return "Sponsor segment doesn't have at least 65% match.";
|
return "One of your submitted segments doesn't have at least 65% match.";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,12 +204,12 @@ module.exports = async function postSkipSegments(req, res) {
|
|||||||
res.sendStatus(409);
|
res.sendStatus(409);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let autoModerateResult = await autoModerateSubmission({videoID, startTime, endTime});
|
// Auto moderator check
|
||||||
if (autoModerateResult) {
|
let autoModerateResult = await autoModerateSubmission(videoID, segments);
|
||||||
res.status(403).send("Request rejected by auto moderator: " + autoModerateResult);
|
if (autoModerateResult) {
|
||||||
return;
|
res.status(403).send("Request rejected by auto moderator: " + autoModerateResult);
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user