forked from KeJunMao/bilibili-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (68 loc) · 2.04 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
import config from "./config.ts";
import { cutString, intToString } from "./utils.ts";
import { sprintf } from "https://deno.land/x/std/fmt/printf.ts";
const biliApiUrl = `https://api.bilibili.com/x/space/arc/search?mid=${config.uid}&ps=5&pn=1`;
const gistApiUrl = `https://api.github.com/gists/${config.gistId}`;
interface VList {
comment: number;
play: number;
title: string;
bvid: string;
}
async function getVList() {
const data = await fetch(biliApiUrl);
const json = await data.json();
const vList = json.data.list.vlist as VList[];
return vList;
}
async function getRequestData() {
const vList = await getVList();
let titleContent = "";
let mdContent = "";
vList.forEach((v) => {
const title = cutString(v.title, 35);
const url = `https://www.bilibili.com/video/${v.bvid}`;
const play = intToString(v.play);
const comment = intToString(v.comment);
titleContent += sprintf("%s ▶️:%-3s 💬:%-3s", title, play, comment) + "\n";
mdContent +=
sprintf("[%s](%s) ▶️:%-3s 💬:%-3s", title, url, play, comment) + "\n\n";
});
return {
description: "My Latest BiliBili videos 👇",
files: {
latest_videos: { content: titleContent },
"latest_videos.md": { content: mdContent },
},
};
}
async function updateGist() {
const data = await getRequestData();
const response = await fetch(gistApiUrl, {
body: JSON.stringify(data),
method: "POST",
headers: {
Authorization: `token ${config.ghToken}`,
Accept: "application/json",
},
});
const status = await response.status;
if (status === 200) {
console.log('Update gist success!');
} else {
throw new Error("Update gist fail!")
}
}
async function run(func:Function){
if (!config.ghToken){
throw new Error('Please set GH_TOKEN environment variable first!')
}
if (!config.gistId){
throw new Error('Please edit GIST_ID environment variable first!')
}
if (!config.uid){
throw new Error('Please edit UID environment variable first!')
}
await func();
}
await run(updateGist)