-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
executable file
·52 lines (40 loc) · 1.63 KB
/
main.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
#!/usr/bin/env -S deno run --unstable --allow-net --allow-read --allow-write --import-map=import_map.json
// Copyright 2020 justjavac(迷渡). All rights reserved. MIT license.
import { format } from "std/datetime/mod.ts";
import { join } from "std/path/mod.ts";
import { exists } from "std/fs/mod.ts";
import type { Word } from "./types.ts";
import { createArchive, createReadme, mergeWords } from "./utils.ts";
const regexp = /<a href="(\/weibo\?q=[^"]+)".*?>(.+)<\/a>/g;
const response = await fetch("https://s.weibo.com/top/summary", {
headers: {
"Cookie": "SUB=_2AkMWJrkXf8NxqwJRmP8SxWjnaY12zwnEieKgekjMJRMxHRl-yj9jqmtbtRB6PaaX-IGp-AjmO6k5cS-OH2X9CayaTzVD",
},
});
if (!response.ok) {
console.error(response.statusText);
Deno.exit(-1);
}
const result: string = await response.text();
const matches = result.matchAll(regexp);
const words: Word[] = Array.from(matches).map((x) => ({
url: x[1],
title: x[2],
}));
const yyyyMMdd = format(new Date(), "yyyy-MM-dd");
const fullPath = join("raw", `${yyyyMMdd}.json`);
let wordsAlreadyDownload: Word[] = [];
if (await exists(fullPath)) {
const content = await Deno.readTextFile(fullPath);
wordsAlreadyDownload = JSON.parse(content);
}
// 保存原始数据
const queswordsAll = mergeWords(words, wordsAlreadyDownload);
await Deno.writeTextFile(fullPath, JSON.stringify(queswordsAll));
// 更新 README.md
const readme = await createReadme(queswordsAll);
await Deno.writeTextFile("./README.md", readme);
// 更新 archives
const archiveText = createArchive(queswordsAll, yyyyMMdd);
const archivePath = join("archives", `${yyyyMMdd}.md`);
await Deno.writeTextFile(archivePath, archiveText);