File size: 1,018 Bytes
1185ec1
f62b8d3
3942037
f62b8d3
6967c22
f62b8d3
df83860
 
f62b8d3
 
 
 
df83860
f62b8d3
3942037
 
 
 
df83860
3942037
f62b8d3
3942037
f62b8d3
3942037
6967c22
 
3942037
 
 
 
 
 
f62b8d3
3942037
 
df83860
 
 
 
 
3942037
f62b8d3
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
import { VideoInfo, VideoStatus } from "@/types/general"

import { adminUsername } from "../config"

export async function getVideoIndex({
  status,
  renewCache = true,
  neverThrow = true,
}: {
  status: VideoStatus

  renewCache?: boolean
  neverThrow?: boolean
}): Promise<Record<string, VideoInfo>> {
  try {
    const response = await fetch(
      `https://huggingface.co/datasets/${adminUsername}/ai-tube-index/raw/main/${status}.json`
    , {
      cache: renewCache ? "no-store" : "default"
    })

    const jsonResponse = await response?.json()

    if (
      typeof jsonResponse === "undefined" ||
      typeof jsonResponse !== "object" ||
      Array.isArray(jsonResponse) ||
      jsonResponse === null) {
      throw new Error("index is not an object, admin repair needed")
    }

    const videos = jsonResponse as Record<string, VideoInfo>

    return videos
  } catch (err) {
    if (neverThrow) {
      console.error(`failed to get index ${status}:`, err)
      return {}
    }
    throw err
  }
}