aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/videoLabels.ts
blob: 82af788cd8b85585cdee7647ec0dfcf26640ab49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Category, CategorySkipOption, VideoID } from "../types";
import { getHash } from "../../maze-utils/src/hash";
import Utils from "../utils";
import { logWarn } from "./logger";
import { asyncRequestToServer } from "./requests";

const utils = new Utils();

export interface LabelCacheEntry {
    timestamp: number;
    videos: Record<VideoID, Category>;
}

const labelCache: Record<string, LabelCacheEntry> = {};
const cacheLimit = 1000;

async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry | null> {
    // Check cache
    const cachedEntry = labelCache[hashPrefix];
    if (cachedEntry) {
        return cachedEntry;
    }

    const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`);
    if (response.status !== 200) {
        // No video labels or server down
        labelCache[hashPrefix] = {
            timestamp: Date.now(),
            videos: {},
        };
        return null;
    }

    try {
        const data = JSON.parse(response.responseText);

        const newEntry: LabelCacheEntry = {
            timestamp: Date.now(),
            videos: Object.fromEntries(data.map(video => [video.videoID, video.segments[0].category])),
        };
        labelCache[hashPrefix] = newEntry;

        if (Object.keys(labelCache).length > cacheLimit) {
            // Remove oldest entry
            const oldestEntry = Object.entries(labelCache).reduce((a, b) => a[1].timestamp < b[1].timestamp ? a : b);
            delete labelCache[oldestEntry[0]];
        }

        return newEntry;
    } catch (e) {
        logWarn(`Error parsing video labels: ${e}`);

        return null;
    }
}

export async function getVideoLabel(videoID: VideoID): Promise<Category | null> {
    const prefix = (await getHash(videoID, 1)).slice(0, 3);
    const result = await getLabelHashBlock(prefix);

    if (result) {
        const category = result.videos[videoID];
        if (category && utils.getCategorySelection(category).option !== CategorySkipOption.Disabled) {
            return category;
        } else {
            return null;
        }
    }

    return null;
}