-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmod.ts
111 lines (87 loc) · 3.65 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env -S deno run --unstable --allow-net --allow-read --allow-write --import-map=import_map.json
import { format } from "std/datetime/mod.ts";
import { join } from "std/path/mod.ts";
import { exists } from "std/fs/mod.ts";
import type {
// WeiboHotSearchWord,
ZhihuHotTopicHotList,
ZhihuHotTopicQuestion,
} from "./types/types.ts";
import { mergeQuestions } from "./utils/utils.ts";
// const weiboHotSearchURL = "https://s.weibo.com/top/summary";
// const zhihuHotSearchURL = "https://www.zhihu.com/api/v4/search/top_search";
const zhihuHotTopicURL =
"https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=100";
// const regexp = /<a href="(\/weibo\?q=[^"]+)".*?>(.+)<\/a>/g;
const yyyyMMdd = format(new Date(), "yyyy-MM-dd");
// 微博热搜
// const weiboHotSearchResponse = await fetch(weiboHotSearchURL);
// if (!weiboHotSearchResponse.ok) {
// console.error(weiboHotSearchResponse.statusText);
// Deno.exit(-1);
// }
// const weiboResult: string = await weiboHotSearchResponse.text();
// const matches = weiboResult.matchAll(regexp);
// const weiboWords: WeiboHotSearchWord[] = Array.from(matches).map((i) => ({
// url: `https://s.weibo.com/${i[1]}`,
// title: i[2],
// }));
// const weiboHotSearchFullPath = join("weibo-hot-search", `${yyyyMMdd}.json`);
// let weiboHotSearchWordsAlreadyDownload: WeiboHotSearchWord[] = [];
// if (await exists(weiboHotSearchFullPath)) {
// const content = await Deno.readTextFile(weiboHotSearchFullPath);
// weiboHotSearchWordsAlreadyDownload = JSON.parse(content);
// }
// 写入微博数据
// const weiboHotSearchQuesWordsAll = weiboMergeWords(
// weiboWords,
// weiboHotSearchWordsAlreadyDownload,
// );
// await Deno.writeTextFile(
// weiboHotSearchFullPath,
// JSON.stringify(weiboHotSearchQuesWordsAll),
// );
// 知乎热搜
// const zhihuHotSearchResponse = await fetch(zhihuHotSearchURL);
// if (!zhihuHotSearchResponse.ok) {
// console.error(zhihuHotSearchResponse.statusText);
// Deno.exit(-1);
// }
// const zhihuHotSearchResult: ZhihuHotSearchSearchTopSearch = await zhihuHotSearchResponse.json();
// const words = zhihuHotSearchResult.top_search.words;
// const zhihuHotSearchFullPath = join("zhihu-hot-search", `${yyyyMMdd}.json`);
// let zhihuHotSearchWordsAlreadyDownload: ZhihuHotSearchSearchWord[] = [];
// if (await exists(zhihuHotSearchFullPath)) {
// const content = await Deno.readTextFile(zhihuHotSearchFullPath);
// zhihuHotSearchWordsAlreadyDownload = JSON.parse(content);
// }
// const zhihuHotSearchWordsAll = zhihuMergeWords(words, zhihuHotSearchWordsAlreadyDownload);
// await Deno.writeTextFile(zhihuHotSearchFullPath, JSON.stringify(zhihuHotSearchWordsAll));
// 知乎热门话题
const zhihuHotTopicResponse = await fetch(zhihuHotTopicURL);
if (!zhihuHotTopicResponse.ok) {
console.error(zhihuHotTopicResponse.statusText);
Deno.exit(-1);
}
const zhihuHotTopicResult: ZhihuHotTopicHotList = await zhihuHotTopicResponse
.json();
const zhihuHotTopicQuestions: ZhihuHotTopicQuestion[] = zhihuHotTopicResult.data
.map((x) => ({
title: x.target.title,
url: `https://www.zhihu.com/question/${x.target.id}`,
}));
const zhihuHotTopicFullPath = join("zhihu-hot-topic", `${yyyyMMdd}.json`);
let zhihuHotTopicQuestionsAlreadyDownload: ZhihuHotTopicQuestion[] = [];
if (await exists(zhihuHotTopicFullPath)) {
const content = await Deno.readTextFile(zhihuHotTopicFullPath);
zhihuHotTopicQuestionsAlreadyDownload = JSON.parse(content);
}
// 保存原始数据
const zhihuHotTopicQuestionsAll = mergeQuestions(
zhihuHotTopicQuestions,
zhihuHotTopicQuestionsAlreadyDownload,
);
await Deno.writeTextFile(
zhihuHotTopicFullPath,
JSON.stringify(zhihuHotTopicQuestionsAll),
);