generated from vivid-lapin/ts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcasAPI.ts
71 lines (64 loc) · 1.8 KB
/
casAPI.ts
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 axios from "axios"
import { LiveProgram } from "./types/cas"
import { EmbeddedData } from "./types/embedded-data"
// https://github.com/SlashNephy/saya/blob/3dfd347a745dff751c8cdd17dc933943b73a1b0f/src/main/kotlin/blue/starry/saya/services/nicolive/LiveNicoliveCommentProvider.kt
export const casClient = axios.create({
baseURL: "https://api.cas.nicovideo.jp/v2",
headers: {
"x-frontend-id": "89",
},
})
export type CasMeta = {
status: number
totalCount?: number
ssId: string
}
export type LivePrograms = {
meta: CasMeta
data?: LiveProgram[]
}
export const getLivePrograms = async ({
searchWord,
}: {
searchWord: string
}) => {
return await casClient
.get<LivePrograms>("/search/programs.json", {
params: {
liveStatus: "onair",
sort: "startTime",
limit: 20,
searchWord,
searchTargets: "tagsExact",
order: "desc",
},
})
.then((data) => data.data?.data ?? [])
}
export const getEmbeddedData = async ({ liveId }: { liveId: string }) => {
const livePage = await axios.get<Document>(
`https://live.nicovideo.jp/watch/${liveId}`,
{ responseType: "document" }
)
const embeddedDataDom = livePage.data.getElementById("embedded-data")
if (!embeddedDataDom) {
throw new Error("#embedded-data not found on " + liveId)
}
const data = embeddedDataDom.getAttribute("data-props")
if (!data) {
throw new Error("data-props not found on " + liveId)
}
return JSON.parse(data) as EmbeddedData
}
export const getCommunityOnAir = async ({ comId }: { comId: string }) => {
const onair = await axios.get<{
meta: { status: number }
data: { live: { id: string } }
}>(
`https://com.nicovideo.jp/api/v1/communities/${comId.replace(
"co",
""
)}/lives/onair.json`
)
return onair.data.data.live
}