mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-03-16 16:23:26 +03:00
Add suggested chapter names
This commit is contained in:
@@ -42,6 +42,7 @@ import { getUserStats } from "./routes/getUserStats";
|
||||
import ExpressPromiseRouter from "express-promise-router";
|
||||
import { Server } from "http";
|
||||
import { youtubeApiProxy } from "./routes/youtubeApiProxy";
|
||||
import { getChapterNames } from "./routes/getChapterNames";
|
||||
|
||||
export function createServer(callback: () => void): Server {
|
||||
// Create a service (the app object is just a callback).
|
||||
@@ -172,6 +173,9 @@ function setupRoutes(router: Router) {
|
||||
// get all segments that match a search
|
||||
router.get("/api/searchSegments", getSearchSegments);
|
||||
|
||||
// autocomplete chapter names
|
||||
router.get("/api/chapterNames", getChapterNames);
|
||||
|
||||
// get status
|
||||
router.get("/api/status/:value", getStatus);
|
||||
router.get("/api/status", getStatus);
|
||||
|
||||
@@ -68,7 +68,7 @@ export class Postgres implements IDatabase {
|
||||
}
|
||||
case "all": {
|
||||
const values = queryResult.rows;
|
||||
Logger.debug(`result (postgres): ${values}`);
|
||||
Logger.debug(`result (postgres): ${JSON.stringify(values)}`);
|
||||
return values;
|
||||
}
|
||||
case "run": {
|
||||
|
||||
46
src/routes/getChapterNames.ts
Normal file
46
src/routes/getChapterNames.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Logger } from "../utils/logger";
|
||||
import { Request, Response } from "express";
|
||||
import { db } from "../databases/databases";
|
||||
import { Postgres } from "../databases/Postgres";
|
||||
|
||||
export async function getChapterNames(req: Request, res: Response): Promise<Response> {
|
||||
const description = req.query.description as string;
|
||||
const channelID = req.query.channelID as string;
|
||||
|
||||
if (!description || typeof(description) !== "string"
|
||||
|| !channelID || typeof(channelID) !== "string") {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
if (!(db instanceof Postgres)) {
|
||||
return res.sendStatus(500).json({
|
||||
message: "Not supported on this instance"
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const descriptions = await db.prepare("all", `
|
||||
SELECT "description"
|
||||
FROM "sponsorTimes"
|
||||
WHERE ("votes" > 0 OR ("views" > 100 AND "votes" >= 0)) AND "videoID" IN (
|
||||
SELECT "videoID"
|
||||
FROM "videoInfo"
|
||||
WHERE "channelID" = ?
|
||||
) AND "description" != ''
|
||||
GROUP BY "description"
|
||||
ORDER BY SUM("votes"), similarity("description", ?) DESC
|
||||
LIMIT 5;`
|
||||
, [channelID, description]) as { description: string }[];
|
||||
|
||||
if (descriptions?.length > 0) {
|
||||
return res.status(200).json(descriptions.map(d => ({
|
||||
description: d.description
|
||||
})));
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(err as string);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
|
||||
return res.status(404).json([]);
|
||||
}
|
||||
Reference in New Issue
Block a user