import type { Question, SearchWord, ToutiaoWord, Word } from "./types.ts"; /** 合并两次热门话题并根据 id 去重 */ export function mergeQuestions( words: Question[], another: Question[], ): Question[] { const obj: Record = {}; for (const w of words.concat(another)) { obj[w.url] = w.title; } return Object.entries(obj).map(([url, title]) => ({ url, title, })); } /** 合并两次关键词并根据 display_query 去重 */ export function mergeWords( words: SearchWord[], another: SearchWord[], ): SearchWord[] { const obj: Record = {}; for (const w of words.concat(another)) { obj[w.display_query] = w.query; } return Object.entries(obj).map(([display_query, query]) => ({ query, display_query, })); } /** 合并两次热门话题并根据 id 去重 */ export function mergeWords4Toutiao( words: ToutiaoWord[], another: ToutiaoWord[], ): ToutiaoWord[] { const obj: Record = {}; for (const w of words.concat(another)) { obj[w.url] = w.word; } return Object.entries(obj).map(([url, word]) => ({ url, word, })); } /** 合并两次热门话题并根据 id 去重 */ export function mergeWords4Weibo( words: Word[], another: Word[], ): Word[] { const obj: Record = {}; for (const w of words.concat(another)) { obj[w.url] = w.title; } return Object.entries(obj).map(([url, title]) => ({ url, title, })); } export async function createReadme(words: Question[]): Promise { const readme = await Deno.readTextFile("./README.md"); return readme.replace( /[\W\w]*/, createVideoList(words), ) .replace( /[\W\w]*/, createQuestionList(words), ); } export async function createReadme4Search( words: SearchWord[], ): Promise { const readme = await Deno.readTextFile("./README.md"); return readme.replace( /[\W\w]*/, createSearchList(words), ); } export async function createReadme4Weibo(words: Word[]): Promise { const readme = await Deno.readTextFile("./README.md"); return readme.replace( /[\W\w]*/, createWeiboList(words), ); } export async function createReadme4Toutiao( words: ToutiaoWord[], ): Promise { const readme = await Deno.readTextFile("./README.md"); return readme.replace( /[\W\w]*/, createTuotiaoList(words), ); } export function createVideoList(words: Question[]): string { return ` ${ words.map((x) => `1. [${x.title}](${x.url})`) .join("\n") } `; } export function createQuestionList(words: Question[]): string { return ` ${ words.map((x) => `1. [${x.title}](${x.url})`) .join("\n") } `; } export function createSearchList(words: SearchWord[]): string { return ` ${ words.map((x) => `1. [${x.display_query}](https://www.zhihu.com/search?q=${x.query})` ).join("\n") } `; } export function createWeiboList(words: Word[]): string { return ` ${ words.map((x) => `1. [${x.title}](https://s.weibo.com/${x.url})`) .join("\n") } `; } export function createTuotiaoList(words: ToutiaoWord[]): string { return ` ${ words.map((x) => `1. [${x.word}](${x.url})`) .join("\n") } `; } export function createArchive(words: Question[], date: string): string { return `# ${date}\n 共 ${words.length} 条\n ${createVideoList(words)} `; } export function createArchive4Search( words: SearchWord[], date: string, ): string { return `# ${date}\n 共 ${words.length} 条\n ${createSearchList(words)} `; } export function createArchive4Weibo(words: Word[], date: string): string { return `# ${date}\n 共 ${words.length} 条\n ${createWeiboList(words)} `; } export function createArchive4Toutiao( words: ToutiaoWord[], date: string, ): string { return `# ${date}\n 共 ${words.length} 条\n ${createTuotiaoList(words)} `; }