Merge branch 'master' of https://github.com/ajayyy/SponsorBlockServer into feat/faster-segments

# Conflicts:
#	src/routes/getSkipSegments.ts
#	src/routes/getSkipSegmentsByHash.ts
#	test/cases/getSegmentsByHash.js
This commit is contained in:
Ajay Ramachandran
2020-12-24 21:27:08 -05:00
122 changed files with 5390 additions and 4643 deletions

View File

@@ -0,0 +1,31 @@
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: VideoIDHash = req.params.prefix;
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);
}