generated from vivid-lapin/ts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.ts
110 lines (107 loc) · 2.73 KB
/
api.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import axios from "axios"
import urljoin from "url-join"
import type { EPGSProgramRecord, EPGSChannel } from "./types"
export class EPGStationAPI {
public baseUrl: URL
constructor(baseUrl: string) {
if (!baseUrl) throw new Error("EPGStation url is not provided")
this.baseUrl = new URL(baseUrl)
}
get client() {
return axios.create({
baseURL: this.baseUrl.href,
headers: {
...(this.isAuthorizationEnabled
? {
Authorization: this.authorizationToken,
}
: {}),
},
timeout: 1000 * 30,
})
}
getChannelLogoUrl({ id }: { id: number }) {
return `${this.baseUrl}/channels/${id}/logo`
}
get isAuthorizationEnabled() {
return !!(this.baseUrl.username && this.baseUrl.password)
}
get authorizationToken() {
return `Basic ${btoa(`${this.baseUrl.username}:${this.baseUrl.password}`)}`
}
async getChannels() {
const { data } = await this.client.get<EPGSChannel[]>("api/channels")
return data.map((channel) => ({
...channel,
name: channel.halfWidthName.trim() ? channel.halfWidthName : channel.name,
}))
}
async getRecords({
offset = 0,
limit = 24,
isReverse,
ruleId,
channelId,
genre,
keyword,
hasOriginalFile,
}: {
offset?: number
limit?: number
isReverse?: boolean
ruleId?: number
channelId?: number
genre?: number
keyword?: string
hasOriginalFile?: boolean
}) {
const { data } = await this.client.get<{
records: EPGSProgramRecord[]
total: number
}>("api/recorded", {
params: {
isHalfWidth: true,
offset,
limit,
isReverse,
ruleId,
channelId,
genre,
keyword,
hasOriginalFile,
},
})
return data
}
async getRecord({ id }: { id: number }) {
const { data } = await this.client.get<EPGSProgramRecord>(
`api/recorded/${id}`,
{
params: { isHalfWidth: true },
}
)
return data
}
async getThumbnailUrl({ id }: { id: number }) {
const response = await this.client.get<ArrayBuffer>(
`api/thumbnails/${id}`,
{ responseType: "arraybuffer" }
)
const objUrl = URL.createObjectURL(
new Blob([response.data], {
type: response.headers?.["content-type"] || "image/png",
})
)
return objUrl
}
async getRecordings({ offset, limit }: { offset?: number; limit?: number }) {
const { data } = await this.client.get<{
records: EPGSProgramRecord[]
total: number
}>("api/recording", { params: { isHalfWidth: true, offset, limit } })
return data
}
getVideoUrl({ videoId }: { videoId: number }) {
return urljoin(this.baseUrl.href, `api/videos/${videoId}`)
}
}