-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.ts
130 lines (103 loc) · 3.69 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { HookRequest, ResponseObject } from "rests";
import API from "./api.js";
import * as fs from 'fs/promises';
const TikAPI = (
apiKey
): typeof API => {
if (!apiKey) {
throw new Error("The API Key is required.");
}
const on_success = function(res: ResponseObject, req: HookRequest){
/**
* A convenient method to get the next batch of items, if the endpoint has iteration parameters (e.g cursor)
*/
const nextItems = function(){
if(!res?.json){
return null;
}
let nextCursorParams: {
cursor?: string |number,
offset?: string | number,
nextCursor?: string,
min_time?: string | number,
max_time?: string | number
} = {};
if(res.json.hasMore || res.json.has_more){
let currentCursorField = (res.json?.cursor || res.json?.offset || res.json?.nextCursor);
nextCursorParams = {
...nextCursorParams,
cursor: currentCursorField,
offset: currentCursorField,
nextCursor: currentCursorField
}
}
if(res.json.notice_lists){
if(!Array.isArray(res.json.notice_lists) || !res.json.notice_lists.length){
return null;
}
let notice_body = res.json.notice_lists[0];
if(!notice_body.has_more){
return null;
}
if(!notice_body.max_time || !notice_body.min_time){
return null;
}
nextCursorParams.max_time = notice_body.max_time;
nextCursorParams.min_time = notice_body.min_time;
}
if(res.json.nextCursor){
nextCursorParams.nextCursor = res.json.nextCursor;
}
if(!Object.keys(nextCursorParams).length){
return null;
}
return req.self({
...req.params,
...nextCursorParams
});
}
/**
* A method for downloading and saving videos.
*/
const saveVideo = async function(link: string, path: string, fetchOptions?: any){
if(!res?.json){
throw new Error("Failed saving video: Couldn't parse response JSON.")
}
let headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
};
if(res.json?.$other?.videoLinkHeaders){
headers = {
...headers,
...res.json.$other.videoLinkHeaders
}
}
const fetch = (await import("node-fetch")).default;
return fetch(link, {
'method': 'GET',
'headers': headers,
...fetchOptions
}).then((res)=>{
if(!res.ok){
return Promise.reject("Failed downloading video, received invalid response.");
}
return res.arrayBuffer();
}).then((arrayBuffer)=>{
return fs.writeFile(path, Buffer.from(arrayBuffer))
})
}
res['nextItems'] = nextItems;
res['saveVideo'] = saveVideo;
};
return API['set']({
apiKey: apiKey,
$options: {
on_success: on_success
}
});
}
TikAPI.default = TikAPI;
if(typeof module !== "undefined" && module.exports){
module.exports = TikAPI;
}
export default TikAPI;