2020-11-29 03:11:35 +08:00
|
|
|
#!/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";
|
|
|
|
|
2020-11-29 18:01:43 +08:00
|
|
|
import type { SearchWord, TopSearch } from "./types.ts";
|
2020-11-29 13:03:19 +08:00
|
|
|
import {
|
2020-11-29 18:01:43 +08:00
|
|
|
createArchive4Search,
|
|
|
|
createReadme4Search,
|
|
|
|
mergeWords,
|
2020-11-29 13:03:19 +08:00
|
|
|
} from "./utils.ts";
|
2020-11-29 03:11:35 +08:00
|
|
|
|
2020-11-29 18:01:43 +08:00
|
|
|
const response = await fetch("https://www.zhihu.com/api/v4/search/top_search");
|
2020-11-29 03:11:35 +08:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
console.error(response.statusText);
|
|
|
|
Deno.exit(-1);
|
|
|
|
}
|
|
|
|
|
2020-11-29 18:01:43 +08:00
|
|
|
const result: TopSearch = await response.json();
|
|
|
|
const words = result.top_search.words;
|
2020-11-29 03:11:35 +08:00
|
|
|
|
|
|
|
const yyyyMMdd = format(new Date(), "yyyy-MM-dd");
|
2020-11-29 18:01:43 +08:00
|
|
|
const fullPath = join("raw/zhihu-search", `${yyyyMMdd}.json`);
|
2020-11-29 03:11:35 +08:00
|
|
|
|
2020-11-29 18:01:43 +08:00
|
|
|
let wordsAlreadyDownload: SearchWord[] = [];
|
2020-11-29 03:11:35 +08:00
|
|
|
if (await exists(fullPath)) {
|
|
|
|
const content = await Deno.readTextFile(fullPath);
|
|
|
|
wordsAlreadyDownload = JSON.parse(content);
|
|
|
|
}
|
|
|
|
|
2020-11-29 18:01:43 +08:00
|
|
|
const wordsAll = mergeWords(words, wordsAlreadyDownload);
|
|
|
|
|
|
|
|
export const zhihuSearchData = wordsAll.map((x) => {
|
|
|
|
x.url = `https://www.zhihu.com/search?q=${x.query}`;
|
2020-11-29 03:11:35 +08:00
|
|
|
return x;
|
|
|
|
});
|
|
|
|
|
2020-11-29 18:01:43 +08:00
|
|
|
export async function zhihuSearch() {
|
2020-11-29 03:11:35 +08:00
|
|
|
// 保存原始数据
|
|
|
|
await Deno.writeTextFile(fullPath, JSON.stringify(wordsAll));
|
|
|
|
|
|
|
|
// 更新 README.md
|
2020-11-29 18:01:43 +08:00
|
|
|
const readme = await createReadme4Search(wordsAll);
|
2020-11-29 03:11:35 +08:00
|
|
|
await Deno.writeTextFile("./README.md", readme);
|
|
|
|
|
|
|
|
// 更新 archives
|
2020-11-29 18:01:43 +08:00
|
|
|
const archiveText = createArchive4Search(wordsAll, yyyyMMdd);
|
|
|
|
const archivePath = join("archives/zhihu-search", `${yyyyMMdd}.md`);
|
2020-11-29 03:11:35 +08:00
|
|
|
await Deno.writeTextFile(archivePath, archiveText);
|
|
|
|
}
|