trending-in-one/toutiao-search.ts

57 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

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 type { ToutiaoTopSearch, ToutiaoWord } from "./types.ts";
2020-11-29 13:03:19 +08:00
import {
createArchive4Toutiao,
createReadme4Toutiao,
mergeWords4Toutiao,
} from "./utils.ts";
2020-11-29 03:11:35 +08:00
2020-11-29 13:03:19 +08:00
const response = await fetch(
"https://is-lq.snssdk.com/api/suggest_words/?business_id=10016",
);
2020-11-29 03:11:35 +08:00
if (!response.ok) {
console.error(response.statusText);
Deno.exit(-1);
}
const result: ToutiaoTopSearch = await response.json();
const words = result.data[0].words;
const yyyyMMdd = format(new Date(), "yyyy-MM-dd");
const fullPath = join("raw/toutiao-search", `${yyyyMMdd}.json`);
let wordsAlreadyDownload: ToutiaoWord[] = [];
if (await exists(fullPath)) {
const content = await Deno.readTextFile(fullPath);
wordsAlreadyDownload = JSON.parse(content);
}
2020-11-29 18:01:43 +08:00
const _words = words.map((x) => {
2022-03-10 14:13:47 +08:00
x.url = `https://so.toutiao.com/search?keyword=${x.word.replace(/(^\s+)|(\s+$)|\s+/g,'%20')}`;
2020-11-29 03:11:35 +08:00
return x;
});
2020-11-29 18:01:43 +08:00
const wordsAll = mergeWords4Toutiao(_words, wordsAlreadyDownload);
export const ToutiaoSearchData = wordsAll;
2020-11-29 03:11:35 +08:00
export async function toutiaoSearch() {
// 保存原始数据
await Deno.writeTextFile(fullPath, JSON.stringify(wordsAll));
// 更新 README.md
const readme = await createReadme4Toutiao(wordsAll);
await Deno.writeTextFile("./README.md", readme);
// 更新 archives
const archiveText = createArchive4Toutiao(wordsAll, yyyyMMdd);
const archivePath = join("archives/toutiao-search", `${yyyyMMdd}.md`);
await Deno.writeTextFile(archivePath, archiveText);
}