File size: 1,173 Bytes
f62b8d3
 
1185ec1
f62b8d3
6967c22
f62b8d3
6967c22
 
 
 
 
a3f1817
f27679f
6967c22
f62b8d3
6967c22
 
 
 
 
 
f62b8d3
6967c22
f62b8d3
6967c22
 
 
 
 
 
f62b8d3
 
6967c22
f62b8d3
6967c22
f62b8d3
6967c22
a3f1817
6967c22
 
 
 
 
a3f1817
6967c22
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
44
45
46
47
48
"use server"

import { ChannelInfo } from "@/types/general"

import { adminUsername } from "../config"

export async function getChannels({
  channelId = "",
  renewCache = true,
  neverThrow = true
}: {
  channelId?: string
  renewCache?: boolean
  neverThrow?: boolean
} = {}): Promise<ChannelInfo[]> {
  try {
    const response = await fetch(
      `https://huggingface.co/datasets/${adminUsername}/ai-tube-index/raw/main/channels.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 channelsIndex = (jsonResponse as Record<string, ChannelInfo>) || {}

    const channels = Object.values(channelsIndex)

    if (!channelId) { return channels }

    return channels.filter(channel => channel.id === channelId)
  } catch (err) {
    if (neverThrow) {
      console.error(`failed to get the channel index:`, err)
      return []
    }
    throw err
  }
}