-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod.ts
80 lines (63 loc) · 2.09 KB
/
mod.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
import { format } from "std/datetime/mod.ts";
import { join } from "std/path/mod.ts";
import { exists } from "std/fs/mod.ts";
import * as utils from "./utils.ts";
import { HotWord } from "./types.ts";
async function fetchData(): Promise<HotWord[]> {
const regexp =
/<a href="(\/weibo\?q=[^"]+)".*?>(.+)<\/a>[\s]+<span>(.+)<\/span>/g;
const response = await fetch("https://s.weibo.com/top/summary");
if (!response.ok) {
console.error(response.statusText);
Deno.exit(-1);
}
const result = await response.text();
const matches = result.matchAll(regexp);
const rank = utils.getCurrentRank();
const words: HotWord[] = Array.from(matches).map((x) => ({
url: x[1],
text: x[2],
count: parseInt(x[3]) * rank,
}));
return words;
}
/**
* 处理源数据,包括去重、关键词过滤、排序、切割
* @param words 源数据
*/
function handleRawData(rawWords: HotWord[]) {
const wordTextSet: Set<string> = new Set();
const words: HotWord[] = [];
/** 去重 */
rawWords
.sort((a, b) => b.count - a.count)
.filter((w) => !w.text.includes("肖战"))
.forEach((t) => {
if (!wordTextSet.has(t.text)) {
wordTextSet.add(t.text);
words.push(t);
}
});
return words.splice(0, 50);
}
async function main() {
const currentDateStr = format(new Date(), "yyyy-MM-dd");
const rawFilePath = join("raw", `${currentDateStr}.json`);
const rawHotWords = await fetchData();
let todayRawData: HotWord[] = [];
if (await exists(rawFilePath)) {
const content = await Deno.readTextFile(rawFilePath);
todayRawData = JSON.parse(content);
}
const hotWords = handleRawData(rawHotWords.concat(todayRawData));
// 保存原始数据
await Deno.writeTextFile(rawFilePath, JSON.stringify(hotWords));
// 更新 README.md
const readme = await utils.genNewReadmeText(hotWords);
await Deno.writeTextFile("./README.md", readme);
// 更新 archives
const archiveText = utils.genArchiveText(hotWords);
const archivePath = join("archives", `${currentDateStr}.md`);
await Deno.writeTextFile(archivePath, archiveText);
}
await main();