mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-18 13:38:22 +03:00
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import {hashPrefixTester} from '../utils/hashPrefixTester';
|
|
import {getSegmentsByHash} from './getSkipSegments';
|
|
import {Request, Response} from 'express';
|
|
import { Category, VideoIDHash } from '../types/segments.model';
|
|
|
|
export async function getSkipSegmentsByHash(req: Request, res: Response) {
|
|
let hashPrefix = req.params.prefix as VideoIDHash;
|
|
if (!hashPrefixTester(req.params.prefix)) {
|
|
res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
|
return;
|
|
}
|
|
|
|
const categories: Category[] = req.query.categories
|
|
? JSON.parse(req.query.categories as string)
|
|
: req.query.category
|
|
? [req.query.category]
|
|
: ['sponsor'];
|
|
|
|
// Get all video id's that match hash prefix
|
|
const segments = getSegmentsByHash(req, hashPrefix, categories);
|
|
|
|
if (!segments) return res.status(404).json([]);
|
|
|
|
const output = Object.entries(segments).map(([videoID, data]) => ({
|
|
videoID,
|
|
hash: data.hash,
|
|
segments: data.segments,
|
|
}));
|
|
|
|
res.status(output.length === 0 ? 404 : 200).json(output);
|
|
}
|