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";
|
|
|
|
// import { config } from 'dotenv/mod.ts'
|
|
|
|
|
2020-11-29 17:46:22 +08:00
|
|
|
import type { Question, ZhihuQuestionList } from "./types.ts";
|
2020-11-29 17:44:19 +08:00
|
|
|
import {
|
|
|
|
createArchive4Question,
|
|
|
|
createReadme4Question,
|
|
|
|
mergeQuestions,
|
|
|
|
} from "./utils.ts";
|
2020-11-29 03:11:35 +08:00
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
"https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=100",
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
console.error(response.statusText);
|
|
|
|
Deno.exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: ZhihuQuestionList = await response.json();
|
|
|
|
|
|
|
|
const questions: Question[] = result.data.map((x) => ({
|
|
|
|
title: x.target.title,
|
|
|
|
url: `https://www.zhihu.com/question/${x.target.id}`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const yyyyMMdd = format(new Date(), "yyyy-MM-dd");
|
|
|
|
const fullPath = join("raw/zhihu-questions", `${yyyyMMdd}.json`);
|
|
|
|
|
|
|
|
let questionsAlreadyDownload: Question[] = [];
|
|
|
|
if (await exists(fullPath)) {
|
|
|
|
const content = await Deno.readTextFile(fullPath);
|
|
|
|
questionsAlreadyDownload = JSON.parse(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
const questionsAll = mergeQuestions(questions, questionsAlreadyDownload);
|
|
|
|
|
|
|
|
export const zhihuQuestionData = questionsAll;
|
|
|
|
|
|
|
|
export async function zhihuQuestions() {
|
|
|
|
// 保存原始数据
|
|
|
|
await Deno.writeTextFile(fullPath, JSON.stringify(questionsAll));
|
|
|
|
|
|
|
|
// 更新 README.md
|
2020-11-29 17:39:59 +08:00
|
|
|
const readme = await createReadme4Question(questionsAll);
|
2020-11-29 03:11:35 +08:00
|
|
|
await Deno.writeTextFile("./README.md", readme);
|
|
|
|
|
|
|
|
// 更新 archives
|
2020-11-29 17:39:59 +08:00
|
|
|
const archiveText = createArchive4Question(questionsAll, yyyyMMdd);
|
2020-11-29 03:11:35 +08:00
|
|
|
const archivePath = join("archives/zhihu-questions", `${yyyyMMdd}.md`);
|
|
|
|
await Deno.writeTextFile(archivePath, archiveText);
|
2020-11-29 13:03:19 +08:00
|
|
|
}
|