-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
66 lines (52 loc) · 1.59 KB
/
utils.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
import { format } from "std/datetime/mod.ts";
import { dailyHours } from "./consts.ts";
import { HotWord } from "./types.ts";
function genDataListString(words: HotWord[]): string {
return words
.map((x) =>
`1. [${x.text}](https://s.weibo.com${x.url}) \`${
getCountStr(x.count)
} 🔥\``
)
.join("\n");
}
export async function genNewReadmeText(words: HotWord[]): Promise<string> {
const formatedNowTimeStr = format(new Date(), "yyyy-MM-dd HH:mm:ss");
const readmeTextStr = await Deno.readTextFile("./README.md");
return readmeTextStr.replace(
/<!-- BEGIN -->[\W\w]*<!-- END -->/,
`<!-- BEGIN -->
${genDataListString(words) || "空空如也"}
数据更新于 ${formatedNowTimeStr}
<!-- END -->`,
);
}
export function genArchiveText(words: HotWord[]): string {
const formatedNowTimeStr = format(new Date(), "yyyy-MM-dd");
return `# ${formatedNowTimeStr}\n
${genDataListString(words)}
`;
}
// 根据当前的小时数,获取热度权值
export function getCurrentRank(): number {
const currentHours = (new Date()).getHours();
// NOTE: 数值待完善
if (dailyHours.night.includes(currentHours)) {
return 0.5;
} else if (dailyHours.morning.includes(currentHours)) {
return 0.8;
} else if (dailyHours.noon.includes(currentHours)) {
return 1.2;
}
return 1;
}
// 获取热度字符串,例如:100.1K 热度
export function getCountStr(num: number): string {
const countUnit = ["", "K", "M", "B"];
let idx = 0;
while (num / 1000 >= 1 && idx < 3) {
idx++;
num = num / 1000;
}
return num.toFixed(1) + countUnit[idx];
}