-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopeni.ts
38 lines (33 loc) · 1.25 KB
/
openi.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
import fs from 'fs';
const config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
// print(config)
console.log(config);
class OpenIAPI {
private _token: string;
private _repos: string[];
constructor(token: string, repos: string[]) {
this._token = token;
if (!token) {
// 读取环境变量 OPENI_TOKEN
this._token = process.env.OPENI_TOKEN;
console.log("[INFO] Using token from environment variable OPENI_TOKEN");
}
this._repos = repos;
}
async get_repo_info(): Promise<any> {
let result: Record<string,string> = {};
for (const repo of this._repos) {
const api: string = `https://openi.pcl.ac.cn/api/v1/datasets/${repo}/current_repo?access_token=${this._token}&type=0`;
result[repo] = await fetch(api).then(res => res.json());
}
return result
}
async get_download_url(uuid: string): Promise<string> {
const api: string = `https://openi.pcl.ac.cn/api/v1/attachments/${uuid}?access_token=${this._token}&type=0`;
return await fetch(api, {
redirect: "manual"
}).then(res => res.headers.get("location"));
}
}
let openi = new OpenIAPI(config.token, config.repos)
export default openi;